ggplot2组合绘制多个图形

ggplot2组合绘制多个图形

win.graph()
library(ggplot2)
library(cowplot)
library(showtext)
font_add_google("Dancing Script", "Dancing")
图1
plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
     geom_point() +
 facet_grid(cols = vars(Species)) # 按Species列分面
图2
plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) +
   geom_point(size = 2.5) +    labs(title = "dot plot") 
图3
plot.diamonds <- ggplot(diamonds, aes(clarity, fill = cut)) +
   geom_bar() +
   theme(axis.text.x = element_text(angle = 70, vjust = 0.5)) +
   labs(title = "bar plot")
组合绘图叠加
gg <- ggdraw() + 
    draw_plot(plot.iris, 0, 0.5, 1, 0.5) + draw_plot(plot.mpg, 0, 0, 0.5, 0.5) +
 # 在母图上半部,占母图比例1/2  
 # 在母图左下角,占母图比例1/4  

draw_plot(plot.diamonds, 0.5, 0, 0.5, 0.5) + 

 # 在母图右下角,占母图比例1/4 
draw_plot_label(c("A", "B", "C"), c(0, 0, 0.5), c(1, 0.5, 0.5), size = 15,colour = "cyan", family = "Dancing") # 加上标签,
浏览图形
showtext_begin()
print(gg)
showtext_end()

ggplot2组合绘制多个图形

上一篇:R可视化包ggplot2改变图例(Legend)的位置实战


下一篇:ggplot画gene structure和alternative splicing | ggbio | GenomicFeatures