ggpubr 包是基于ggplot2包缺脉,非R數(shù)據(jù)專業(yè)人員可用ggpubr包繪制圖表

本文參考ggpubr包官方文檔https://rpkgs.datanovia.com/ggpubr/index.htmlhttp://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/
ggpubr 包是基于ggplot2包摹恨,非R數(shù)據(jù)專業(yè)人員可用ggpubr包繪制圖表

下載安裝ggpubr

install.packages("ggpubr")

或者使用以下命令

if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")

ggpubr可繪制大部分我們常用的圖形

  • 分布圖(Distribution)
    • 密度分布圖以及邊際地毯線并添加平均值線(Density plot with mean lines and marginal rug)
    • 帶有均值線和邊際地毯線的直方圖Histogram plot with mean lines and marginal rug)
  • 箱線圖和小提琴圖(Box plots and violin plots)
    • 具有不同點(diǎn)分布的箱線圖(Box plots with jittered points)
    • 小提琴圖內(nèi)添加箱線圖(Violin plots with box plots inside)
  • 條形圖(Bar plots)
    • 有序的條形圖(Ordered bar plots)
    • 偏差圖(Deviation graphs)
  • 點(diǎn)圖 (Dot charts)
    • 棒棒糖圖表(Lollipop chart)
    • 偏差圖(Deviation graphs)
    • 克利夫蘭點(diǎn)圖(Cleveland’s dot plot)

首先:

分布圖——密度分布圖

library(ggpubr)  #加載ggubr, 同時(shí)也要安裝加載ggplot2, magrittr 這兩個(gè)包
#> Le chargement a nécessité le package : ggplot2
#> Le chargement a nécessité le package : magrittr
# Create some data format 設(shè)置數(shù)值
# :::::::::::::::::::::::::::::::::::::::::::::::::::
set.seed(1234) 
# set.seed 用于設(shè)定隨機(jī)數(shù)種子, 
#一個(gè)特定的種子可以產(chǎn)生一個(gè)特定的偽隨機(jī)序列尊剔,
#這個(gè)函數(shù)的主要目的埂材,是讓你的模擬能夠可重復(fù)出現(xiàn)亦歉,
#因?yàn)楹芏鄷r(shí)候我們需要取隨機(jī)數(shù)恤浪,但這段代碼再跑一次的時(shí)候,
#結(jié)果就不一樣了肴楷,如果需要重復(fù)出現(xiàn)同樣的模擬結(jié)果的話水由,
#就可以用set.seed()。在調(diào)試程序或者做展示的時(shí)候赛蔫,
#結(jié)果的可重復(fù)性是很重要的砂客,所以隨機(jī)數(shù)種子也就很有必要。
#參考:https://blog.csdn.net/vencent_cy/article/details/50350020 

wdata = data.frame(
   sex = factor(rep(c("F", "M"), each=200)),
   weight = c(rnorm(200, 55), rnorm(200, 58)))
head(wdata, 4)        #設(shè)置一個(gè)數(shù)據(jù)框呵恢,包含sex因子鞠值,weight 向量,輸出前5行
#>   sex   weight
#> 1   F 53.79293
#> 2   F 55.27743
#> 3   F 56.08444
#> 4   F 52.65430

# Density plot with mean lines and marginal rug
#密度分布圖以及邊際地毯線并添加平均值線
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline and fill colors by groups ("sex") 按性別更改輪廓和填充顏色
# Use custom palette  使用自定義調(diào)色板
ggdensity(wdata, x = "weight",
   add = "mean", rug = TRUE,
   color = "sex", fill = "sex",
   palette = c("#00AFBB", "#E7B800"))

# ggdensity繪圖渗钉,設(shè)定x軸彤恶,添加平均線。
# rug =TRUE 是添加邊緣地毯線鳄橘,
# 如果rug=F声离,則不添加,可以看以下兩張圖的區(qū)別

image
image

分布圖——直方圖

# Histogram plot with mean lines and marginal rug
# 帶有均值線和邊際地毯線的直方圖
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline and fill colors by groups ("sex")
# Use custom color palette #使用自定義調(diào)色板
gghistogram(wdata, x = "weight",
   add = "mean", rug = TRUE,
   color = "sex", fill = "sex",
   palette = c("#00AFBB", "#E7B800"))

image

其次:

箱線圖及小提琴圖—— 具有不同點(diǎn)分布的箱線圖

# Load data  加載ToothGrowth數(shù)據(jù)瘫怜,
# 它描述了維生素C對(duì)豚鼠牙齒生長(zhǎng)的影響术徊。 
# 使用三種劑量水平的維生素C(0.5mg,1mg和2 mg)
# 和兩種遞送方法[橙汁(OJ)或抗壞血酸(VC)]中的每一種
data("ToothGrowth")
df <- ToothGrowth
head(df, 4)  #輸出前4行
#>    len supp dose
#> 1  4.2   VC  0.5
#> 2 11.5   VC  0.5
#> 3  7.3   VC  0.5
#> 4  5.8   VC  0.5

# Box plots with jittered points
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline colors by groups: dose
# Use custom color palette
# Add jitter points and change the shape by groups
 p <- ggboxplot(df, x = "dose", y = "len",
                color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
                add = "jitter", shape = "dose")
 p

image
 # Add p-values comparing groups  每組添加p值
 # Specify the comparisons you want 設(shè)置你想比較的任意組
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
p + stat_compare_means(comparisons = my_comparisons)+   stat_compare_means(label.y = 50)
# Add pairwise comparisons p-value 添加成對(duì)p值
         # Add global p-value   添加全局p值   

image

箱線圖及小提琴圖—— 小提琴圖內(nèi)添加箱線圖

# Violin plots with box plots inside
#小提琴圖內(nèi)添加箱線圖
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change fill color by groups: dose 以計(jì)量填充
# add boxplot with white fill color 
ggviolin(df, x = "dose", y = "len", fill = "dose",
         palette = c("#00AFBB", "#E7B800", "#FC4E07"), #顏色隨機(jī)
         add = "boxplot", add.params = list(fill = "white"))+ #添加內(nèi)部箱線圖
  stat_compare_means(comparisons = my_comparisons, label = "p.signif")+ 
# Add significance levels  添加顯著水平
  stat_compare_means(label.y = 50)                        
              # Add global the p-value  添加全局影響因子

image

第三

條形圖—— 有序的條形圖

# Load data   加載內(nèi)置數(shù)據(jù)集 mtcars
data("mtcars")
dfm <- mtcars
# Convert the cyl variable to a factor  將cyl 變量轉(zhuǎn)換為因子
dfm$cyl <- as.factor(dfm$cyl)
# Add the name colums    添加name列
dfm$name <- rownames(dfm) 
# Inspect the data
head(dfm[, c("name", "wt", "mpg", "cyl")])
#>                                name    wt  mpg cyl
#> Mazda RX4                 Mazda RX4 2.620 21.0   6
#> Mazda RX4 Wag         Mazda RX4 Wag 2.875 21.0   6
#> Datsun 710               Datsun 710 2.320 22.8   4
#> Hornet 4 Drive       Hornet 4 Drive 3.215 21.4   6
#> Hornet Sportabout Hornet Sportabout 3.440 18.7   8
#> Valiant                     Valiant 3.460 18.1   6

ggbarplot(dfm, x = "name", y = "mpg",
          fill = "cyl",               # change fill color by cyl
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "desc",          # Sort the value in dscending order
          sort.by.groups = FALSE,     # Don't sort inside each group 
不按組排序
          x.text.angle = 90           # Rotate vertically x axis texts
          )

image
ggbarplot(dfm, x = "name", y = "mpg",
          fill = "cyl",               # change fill color by cyl
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "asc",           # Sort the value in dscending order
          sort.by.groups = TRUE,      # Sort inside each group 按組排序
          x.text.angle = 90           # Rotate vertically x axis texts
          )

image

條形圖—— 偏差圖

# Calculate the z-score of the mpg data
dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)
dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"), 
                     levels = c("low", "high"))
# Inspect the data
head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])
#>                                name    wt  mpg      mpg_z mpg_grp cyl
#> Mazda RX4                 Mazda RX4 2.620 21.0  0.1508848    high   6
#> Mazda RX4 Wag         Mazda RX4 Wag 2.875 21.0  0.1508848    high   6
#> Datsun 710               Datsun 710 2.320 22.8  0.4495434    high   4
#> Hornet 4 Drive       Hornet 4 Drive 3.215 21.4  0.2172534    high   6
#> Hornet Sportabout Hornet Sportabout 3.440 18.7 -0.2307345     low   8
#> Valiant                     Valiant 3.460 18.1 -0.3302874     low   6

ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",           # change fill color by mpg_level
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "asc",           # Sort the value in ascending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = "MPG z-score",
          xlab = FALSE,
          legend.title = "MPG Group"
          )

image
Rotate the plot: use rotate = TRUE and sort.val = “desc”  轉(zhuǎn)換角度
ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",           # change fill color by mpg_level
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "desc",          # Sort the value in descending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = "MPG z-score",
          legend.title = "MPG Group",
          rotate = TRUE,
          ggtheme = theme_minimal()
          )

image

最后

點(diǎn)圖——棒棒糖點(diǎn)圖

ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "ascending",                        # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           ggtheme = theme_pubr()                        # ggplot2 theme
           )

image

設(shè)置參數(shù)

  • Sort in decending order. sorting = “descending”.
  • Rotate the plot vertically, using rotate = TRUE.
  • Sort the mpg value inside each group by using group = “cyl”.
  • Set dot.size to 6.
  • Add mpg values as label. label = “mpg” or label = round(dfm$mpg).
ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "descending",                       # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           rotate = TRUE,                                # Rotate vertically
           group = "cyl",                                # Order by groups
           dot.size = 6,                                 # Large dot size
           label = round(dfm$mpg),                        # Add mpg values as dot labels
           font.label = list(color = "white", size = 9, 
                             vjust = 0.5),               # Adjust label parameters
           ggtheme = theme_pubr()                        # ggplot2 theme
           )

image

**點(diǎn)圖——偏差圖

  • Use y = “mpg_z”
  • Change segment color and size: add.params = list(color = “l(fā)ightgray”, size = 2)

ggdotchart(dfm, x = "name", y = "mpg_z",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "descending",                       # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           add.params = list(color = "lightgray", size = 2), # Change segment color and size
           group = "cyl",                                # Order by groups
           dot.size = 6,                                 # Large dot size
           label = round(dfm$mpg_z,1),                        # Add mpg values as dot labels
           font.label = list(color = "white", size = 9, 
                             vjust = 0.5),               # Adjust label parameters
           ggtheme = theme_pubr()                        # ggplot2 theme
           )+
  geom_hline(yintercept = 0, linetype = 2, color = "lightgray")

image

Color y text by groups. Use y.text.col = TRUE.

ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "descending",                       # Sort value in descending order
           rotate = TRUE,                                # Rotate vertically
           dot.size = 2,                                 # Large dot size
           y.text.col = TRUE,                            # Color y text by groups
           ggtheme = theme_pubr()                        # ggplot2 theme
           )+
  theme_cleveland()                                      # Add dashed grids

image

以上 。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鲸湃,一起剝皮案震驚了整個(gè)濱河市赠涮,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌暗挑,老刑警劉巖世囊,帶你破解...
    沈念sama閱讀 206,126評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異窿祥,居然都是意外死亡株憾,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嗤瞎,“玉大人墙歪,你說我怎么就攤上這事”雌妫” “怎么了虹菲?”我有些...
    開封第一講書人閱讀 152,445評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)掉瞳。 經(jīng)常有香客問我毕源,道長(zhǎng),這世上最難降的妖魔是什么陕习? 我笑而不...
    開封第一講書人閱讀 55,185評(píng)論 1 278
  • 正文 為了忘掉前任霎褐,我火速辦了婚禮,結(jié)果婚禮上该镣,老公的妹妹穿的比我還像新娘冻璃。我一直安慰自己,他們只是感情好损合,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評(píng)論 5 371
  • 文/花漫 我一把揭開白布省艳。 她就那樣靜靜地躺著,像睡著了一般嫁审。 火紅的嫁衣襯著肌膚如雪跋炕。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 48,970評(píng)論 1 284
  • 那天律适,我揣著相機(jī)與錄音枣购,去河邊找鬼。 笑死擦耀,一個(gè)胖子當(dāng)著我的面吹牛棉圈,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播眷蜓,決...
    沈念sama閱讀 38,276評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼分瘾,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了吁系?” 一聲冷哼從身側(cè)響起德召,我...
    開封第一講書人閱讀 36,927評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎汽纤,沒想到半個(gè)月后上岗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,400評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蕴坪,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評(píng)論 2 323
  • 正文 我和宋清朗相戀三年肴掷,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了敬锐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 37,997評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡呆瞻,死狀恐怖台夺,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情痴脾,我是刑警寧澤颤介,帶...
    沈念sama閱讀 33,646評(píng)論 4 322
  • 正文 年R本政府宣布,位于F島的核電站赞赖,受9級(jí)特大地震影響滚朵,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜前域,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評(píng)論 3 307
  • 文/蒙蒙 一辕近、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧话侄,春花似錦亏推、人聲如沸学赛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽盏浇。三九已至变丧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間绢掰,已是汗流浹背痒蓬。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評(píng)論 1 260
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留滴劲,地道東北人攻晒。 一個(gè)月前我還...
    沈念sama閱讀 45,423評(píng)論 2 352
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像班挖,于是被迫代替她去往敵國和親鲁捏。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容