本文主要是介绍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组合绘制多个图形的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!