install.packages("cowplot")
library(cowplot)
library(ggplot2) # 需提前安装:install.packages("ggplot2")
set.seed(1112)
mydata <- diamonds[sample(nrow(diamonds), 500), ]
summary(mydata)
set.seed(1)
p1 <- ggplot(mydata, aes(cut, carat, colour = cut)) +
geom_boxplot() +
geom_jitter(width = 0.2) +
theme(legend.position = "none")
p1
p2 <- ggplot(mydata, aes(carat, price, colour = cut)) +
geom_jitter() +
theme_cowplot() +
theme(legend.position = "none")
p2
p3 <- ggplot(mydata, aes(carat)) +
geom_histogram(colour = "grey") +
theme_half_open()
p3
p3_1 <- ggplot(mydata, aes(price)) +
geom_histogram(colour = "grey") +
theme_half_open() +
background_grid() +
theme(axis.text.x = element_text(angle = 90))
p3_1
p4 <- ggplot(mydata, aes(carat, fill = cut)) +
geom_density(colour = "grey", alpha = 0.6) +
theme_minimal_grid()
p4
plot_grid(p2, p3, p1)
plot_grid(p2, p3, p1,
ncol = 3,
labels = c("A", "B", "C"),
rel_widths = c(1, 1, 1.2))
p2 +
draw_label("Diamonds", x = 2, y = 5000,
color = "pink2", size = 50, angle = 30)
ggdraw(p2) + # A图
draw_plot(p3 + panel_border() , # 添加B图
x = 0.6, y = 0.1, # 图片位置
width = 0.35, height = 0.3) + # 图片大小
draw_plot(p3_1 + panel_border() , # 添加C图
x = 0.1, y = 0.65,
0.35, 0.3) +
draw_plot_label(
c("A", "B", "C"),
x = c(0, 0.65, 0.15),
y = c(1, 0.45, 0.99),
size = 12)
好啦,今天的内容就到这里。如果有帮助,记得分享给需要的人!
参考文献
[1]. https://wilkelab.org/cowplot/articles/themes.html
公众号的线上课程
1. 《R语言和统计新手课程》
2. 《回归:从入门到进阶》
3. 《线性混合模型和纵向数据分析》
【通过公众号菜单栏--线上课程】
统计咨询
公众号核心成员的成果发表