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
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", rotate = FALSE, xlab = FALSE, legend.title = "MPG Group")
棒棒糖图 lollipop chart
1 2 3 4 5 6 7 8 9 10 11 12
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
1 2 3 4 5 6 7 8 9 10 11 12 13
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")
散点图scatterplot
1 2 3 4 5 6 7 8 9 10
df <- datasets::iris head(df) ggscatter(df, x = 'Sepal.Width', y = 'Sepal.Length', palette = 'jco', shape = 'Species', add = 'reg.line', color = 'Species', conf.int = TRUE)
添加回归线的系数
1 2 3 4 5 6 7 8 9
ggscatter(df, x = 'Sepal.Width', y = 'Sepal.Length', palette = 'jco', shape = 'Species', add = 'reg.line', color = 'Species', conf.int = TRUE)+ stat_cor(aes(color=Species),method = "pearson", label.x = 3)
ggscatter(dfm, x = "wt", y = "mpg", color = "cyl", palette = "jco", size = "qsec", alpha = 0.5)+ scale_size(range = c(0.5, 15)) # Adjust the range of points size
连线图 lineplot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
p1 <- ggbarplot(ToothGrowth, x = "dose", y = "len", add = "mean_se", color = "supp", palette = "jco", position = position_dodge(0.8))+ stat_compare_means(aes(group = supp), label = "p.signif", label.y = 29) p2 <- ggline(ToothGrowth, x = "dose", y = "len", add = "mean_se", color = "supp", palette = "jco")+ stat_compare_means(aes(group = supp), label = "p.signif", label.y = c(16, 25, 29)) cowplot::plot_grid(p1, p2, ncol = 2, align = "hv")
添加边沿图 marginal plots
1 2 3 4 5 6 7 8 9
library(ggExtra) p <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width", color = "Species", palette = "jco", size = 3, alpha = 0.6) ggMarginal(p, type = "boxplot")
library(cowplot) # Main plot pmain <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+ geom_point()+ ggpubr::color_palette("jco")
# Marginal densities along x axis xdens <- axis_canvas(pmain, axis = "x")+ geom_density(data = iris, aes(x = Sepal.Length, fill = Species), alpha = 0.7, size = 0.2)+ ggpubr::fill_palette("jco")
# Marginal densities along y axis # Need to set coord_flip = TRUE, if you plan to use coord_flip() ydens <- axis_canvas(pmain, axis = "y", coord_flip = TRUE)+ geom_boxplot(data = iris, aes(x = Sepal.Width, fill = Species), alpha = 0.7, size = 0.2)+ coord_flip()+ ggpubr::fill_palette("jco")
p1 <- insert_xaxis_grob(pmain, xdens, grid::unit(.2, "null"), position = "top") p2 <- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right") ggdraw(p2)
# Density plot of "Sepal.Length" #:::::::::::::::::::::::::::::::::::::: density.p <- ggdensity(iris, x = "Sepal.Length", fill = "Species", palette = "jco") # Draw the summary table of Sepal.Length #:::::::::::::::::::::::::::::::::::::: # Compute descriptive statistics by groups stable <- desc_statby(iris, measure.var = "Sepal.Length", grps = "Species") stable <- stable[, c("Species", "length", "mean", "sd")] # Summary table plot, medium orange theme stable.p <- ggtexttable(stable, rows = NULL, theme = ttheme("mOrange")) # Draw text #:::::::::::::::::::::::::::::::::::::: text <- paste("iris data set gives the measurements in cm", "of the variables sepal length and width", "and petal length and width, respectively,", "for 50 flowers from each of 3 species of iris.", "The species are Iris setosa, versicolor, and virginica.", sep = " ") text.p <- ggparagraph(text = text, face = "italic", size = 11, color = "black") # Arrange the plots on the same page ggarrange(density.p, stable.p, text.p, ncol = 1, nrow = 3, heights = c(1, 0.5, 0.3))
注释table在图上
1 2 3 4 5 6 7 8 9 10 11
density.p <- ggdensity(iris, x = "Sepal.Length", fill = "Species", palette = "jco")