ggpubr主要特点: 1. 帮助具有非高级R编程技能的研究人员轻松创建易于发布的图表。 2. 基于ggplot2为背景。 3. 可以自动将p值和显着性水平添加到箱形图,条形图,线图等。 4. 可以轻松地在同一页面上排列和注释多个图表。 5. 可以轻松更改颜色和标签等颜色参数。 Official online documentation: http://www./english/rpkgs/ggpubr. library(ggpubr)# Create some data format# :::::::::::::::::::::::::::::::::::::::::::::::::::set.seed(1234)wdata = data.frame( sex = factor(rep(c('F', 'M'), each=200)), weight = c(rnorm(200, 55), rnorm(200, 58)))head(wdata, 4) ## sex weight## 1 F 53.8## 2 F 55.3## 3 F 56.1## 4 F 52.7 # Density plot with mean lines and marginal rug# :::::::::::::::::::::::::::::::::::::::::::::::::::# Change outline and fill colors by groups ('sex')# Use custom paletteggdensity(wdata, x = 'weight', add = 'mean', rug = TRUE, color = 'sex', fill = 'sex', palette = c('#00AFBB', '#E7B800')) # 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')) Box plots and violin plots# Load datadata('ToothGrowth')df <> ## 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 <> # Add p-values comparing groups # Specify the comparisons you wantmy_comparisons <> # Violin plots with box plots inside# :::::::::::::::::::::::::::::::::::::::::::::::::::# Change fill color by groups: dose# add boxplot with white fill colorggviolin(df, x = 'dose', y = 'len', fill = 'dose', palette = c('#00AFBB', '#E7B800', '#FC4E07'), add = 'boxplot', add.params = list(fill = 'white'))+ stat_compare_means(comparisons = my_comparisons, label = 'p.signif')+ # Add significance levels stat_compare_means(label.y = 50) # Add global the p-value Bar plotsDemo data setLoad and prepare data: # Load datadata('mtcars')dfm <><><> ## name wt mpg cyl## Mazda RX4 Mazda RX4 2.62 21.0 6## Mazda RX4 Wag Mazda RX4 Wag 2.88 21.0 6## Datsun 710 Datsun 710 2.32 22.8 4## Hornet 4 Drive Hornet 4 Drive 3.21 21.4 6## Hornet Sportabout Hornet Sportabout 3.44 18.7 8## Valiant Valiant 3.46 18.1 6 Ordered bar plotsChange 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. 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 ) Deviation graphsThe 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))><><> ## name wt mpg mpg_z mpg_grp cyl## Mazda RX4 Mazda RX4 2.62 21.0 0.151 high 6## Mazda RX4 Wag Mazda RX4 Wag 2.88 21.0 0.151 high 6## Datsun 710 Datsun 710 2.32 22.8 0.450 high 4## Hornet 4 Drive Hornet 4 Drive 3.21 21.4 0.217 high 6## Hornet Sportabout Hornet Sportabout 3.44 18.7 -0.231 low 8## Valiant Valiant 3.46 18.1 -0.330 low 6 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” 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() ) Dot chartsLollipop chartLollipop 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 )
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 ) Deviation graph:
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') Cleveland’s dot plotColor 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 我们将以TCGA数据为例,为大家直播讲解ggpur包分析TCGA数据作图流程,加我微信号了解详细,1478115560,注明听课,仅收取10元。 |
|