2019-05-20

copy from: http://www.reibang.com/p/3862faf22c14

# 清空環(huán)境變量
rm(list=ls()) 
# 安裝、載入包
install.packages("ggpubr")
library(ggpubr)
install.packages("ggsci")
library(ggsci)
# 加載數(shù)據(jù)集非凌,并查看
data("ToothGrowth")
str(ToothGrowth)
head(ToothGrowth)
image.png

image.png

ggboxplot函數(shù)

ggboxplot為ggpubr包的函數(shù)呛占。
ggboxplot(data, x, y, color = "black", bxp.errorbar = FALSE)

data a data frame
x character string containing the name of x variable
y character vector containing one or more variables to plot
color outline color
palette the color palette to be used for coloring or filling by groups. Allowed values include "grey" for grey color palettes; brewer palettes e.g. "RdBu", "Blues", ...; or custom color palette e.g. c("blue", "red"); and scientific journal palettes from ggsci R package, e.g.: "npg", "aaas", "lancet", "jco", "ucscgb", "uchicago", "simpsons" and "rickandmorty".
bxp.errorbar logical value. If TRUE, shows error bars of box plots.
#從lancet調(diào)色板提取9個(gè)顏色
pal = pal_lancet("lanonc")(9)
#選取前3個(gè)顏色
colors = pal_lancet("lanonc")(9)[1:3]
my_comparisons = list(c("0.5","1"),c("1","2"), c("0.5","2"))
p = ggboxplot( ToothGrowth , x = "dose", y = "len", color = "dose" , palette =colors, bxp.errorbar = T) +
stat_compare_means(comparisons =my_comparisons) +stat_compare_means(method = "anova" , label.x = 1.7, label.y = 45)
p
p + stat_boxplot(geom ="errorbar", color = colors, width =0.4, size =1) + geom_boxplot(aes(color = dose))
p

stat_compare_means

  • ggpubr包的函數(shù)
  • Add mean comparison p-values to a ggplot, such as box blots, dot plots and stripcharts.
  • stat_compare_means(method = NULL, comparisons = NULL, label.x = NULL, label.y = NULL)
method a character string indicating which method to be used for comparing means.
comparisons A list of length-2 vectors. The entries in the vector are either the names of 2 values on the x-axis or the 2 integers that correspond to the index of the groups of interest, to be compared.
label.x,label.y numeric Coordinates (in data units) to be used for absolute positioning of the label. If too short they will be recycled.

stat_boxplot

  • ggplot2包函數(shù)晌姚,A box and whiskers plot (in the style of Tukey)
  • The boxplot compactly displays the distribution of a continuous variable. It visualises five summary statistics (the median, two hinges and two whiskers), and all "outlying" points individually.
  • stat_boxplot(geom ="errorbar", color = colors, width =0.4, size =1) + geom_boxplot(aes(color = dose))

結(jié)果展示:


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末屿储,一起剝皮案震驚了整個(gè)濱河市鹊奖,隨后出現(xiàn)的幾起案子店展,更是在濱河造成了極大的恐慌养篓,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件赂蕴,死亡現(xiàn)場(chǎng)離奇詭異柳弄,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)概说,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)碧注,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人糖赔,你說(shuō)我怎么就攤上這事萍丐。” “怎么了放典?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵逝变,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我奋构,道長(zhǎng)骨田,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任声怔,我火速辦了婚禮态贤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘醋火。我一直安慰自己悠汽,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布芥驳。 她就那樣靜靜地躺著柿冲,像睡著了一般。 火紅的嫁衣襯著肌膚如雪兆旬。 梳的紋絲不亂的頭發(fā)上假抄,一...
    開(kāi)封第一講書(shū)人閱讀 51,578評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼宿饱。 笑死熏瞄,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的谬以。 我是一名探鬼主播强饮,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼为黎!你這毒婦竟也來(lái)了邮丰?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤铭乾,失蹤者是張志新(化名)和其女友劉穎剪廉,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體炕檩,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡妈经,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了捧书。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡骤星,死狀恐怖经瓷,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情洞难,我是刑警寧澤舆吮,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站队贱,受9級(jí)特大地震影響色冀,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜柱嫌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一锋恬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧编丘,春花似錦与学、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至抑片,卻和暖如春卵佛,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工截汪, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留疾牲,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓挫鸽,卻偏偏與公主長(zhǎng)得像说敏,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子丢郊,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355