其他分享
首页 > 其他分享> > 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()

在这里插入图片描述

标签:plot,Dancing,library,gg,ggplot2,showtext,图形,绘制
来源: https://blog.51cto.com/u_15264819/2887651