ggpubr主要特点:
1. 帮助具有非高级R编程技能的研究人员轻松创建易于发布的图表。
2. 基于ggplot2为背景。
3. 可以自动将p值和显着性水平添加到箱形图,条形图,线图等。
4. 可以轻松地在同一页面上排列和注释多个图表。
5. 可以轻松更改颜色和标签等颜色参数。
Official online documentation: http://www.sthda.com/english/rpkgs/ggpubr.
## sex weight## 1 F 53.8## 2 F 55.3## 3 F 56.1## 4 F 52.7
# Histogram plot with mean lines and marginal rug# :::::::::::::::::::::::::::::::::::::::::::::::::::# Change outline and fill colors by groups ('sex')# Use custom color palettegghistogram(wdata, x = 'weight', add = 'mean', rug = TRUE, color = 'sex', fill = 'sex', palette = c('#00AFBB', '#E7B800'))
## 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
# Add p-values comparing groups # Specify the comparisons you wantmy_comparisons <>
Load and prepare data:
# Load datadata('mtcars')dfm <><><>
Change the fill color by the grouping variable “cyl”. Sorting will be done globally, but not by groups.
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 )
Sort bars inside each group. Use the argument sort.by.groups = TRUE.
The deviation graph shows the deviation of quantitative values to a reference value. In the R code below, we’ll plot the mpg z-score from the mtcars dataset.
Calculate the z-score of the mpg data:
# Calculate the z-score of the mpg datadfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))>- (dfm$mpg -mean(dfm$mpg))><><>
Create an ordered bar plot, colored according to the level of mpg:
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' )
Rotate the plot: use rotate = TRUE and sort.val = “desc”
Lollipop chart is an alternative to bar plots, when you have a large set of values to visualize.
Lollipop chart colored by the grouping variable “cyl”:
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 )
Sort in descending 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).
Deviation graph:
Use y = “mpg_z”
Change segment color and size: add.params = list(color = “lightgray”, 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')
Color y text by groups. Use y.text.col = TRUE.
我们将以TCGA数据为例,为大家直播讲解ggpur包分析TCGA数据作图流程,加我微信号了解详细,1478115560,注明听课,仅收取10元。
联系客服