我有一个数据,其中一个分类变量有3个水平。现在分别对每一个水平分别进行作图,并用
下面是我的数据以及代码:
如果图形显示不了的话,点击这个网址
gridExtra
包中的grid.arrange函数进行组合。每一个图形都有相同的图例,组合后为了减少重复,减去两个图例,即中间图形保留图例,两边的图形不显示图例。但是这样一来,组合后的三张图形比例不是一致。问各位高手怎么解决?下面是我的数据以及代码:
#Data building ####
BXTgrowth <- data.frame(tissue = rep(c("Body", "PEM", "GAC"), each = 6),
time = rep(c("25", "27", "35"), each = 2, times = 3),
kcl = rep(c("con", "tre"), times = 9),
mean = c(32.3, 33.5, 42.3, 48.6, 71.1, 78.4,
226, 241, 186, 212, 202, 231,
100, 103, 145, 156, 258, 270),
se = c(0.99, 0.87, 1.8, 1.2, 3.3, 3.3,
9.26, 7.82, 9.77, 2.72, 7.23, 10.06,
8.10, 6.47, 7.41, 7.80, 11.50, 13.95))
#Three figurs
p1 <- BXTgrowth %>%
filter(tissue == "Body") %>%
ggplot(aes(x = time, y = mean, fill = kcl)) +
geom_bar(position = "dodge", stat = "identity", colour = "black") +
labs(x = "Times", y = "Weight of body (g)") +
scale_x_discrete(labels = c("Eday 25", "Eday 27", "Day 7")) +
scale_fill_grey(guide = guide_legend(title = NULL),
labels = c("Control group", "Treatment group")) +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
position = position_dodge(0.9), width = 0.2) +
annotate(geom = "text", x = c(2, 3),
y = c(48.6 + 5, 78.4 + 5), label = "*") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "none",
axis.title = element_text(family = "Times New Roman", size = 15),
legend.text = element_text(family = "Times New Roman", size = 12),
axis.text = element_text(family = "Times New Roman", size = 8))
p2 <- BXTgrowth %>%
filter(tissue == "PEM") %>%
ggplot(aes(x = time, y = mean, fill = kcl)) +
geom_bar(position = "dodge", stat = "identity", colour = "black") +
labs(x = "Times", y = "Weight of PEM (mg)") +
scale_x_discrete(labels = c("Eday 25", "Eday 27", "Day 7")) +
scale_fill_grey(guide = guide_legend(title = NULL),
labels = c("Control group", "Treatment group")) +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
position = position_dodge(0.9), width = 0.2) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "top",
axis.title = element_text(family = "Times New Roman", size = 15),
legend.text = element_text(family = "Times New Roman", size = 12),
axis.text = element_text(family = "Times New Roman", size = 8))
p3 <- BXTgrowth %>%
filter(tissue == "GAC") %>%
ggplot(aes(x = time, y = mean, fill = kcl)) +
geom_bar(position = "dodge", stat = "identity", colour = "black") +
labs(x = "Times", y = "Weight of GAC (mg)") +
scale_x_discrete(labels = c("Eday 25", "Eday 27", "Day 7")) +
scale_fill_grey(guide = guide_legend(title = NULL),
labels = c("Control group", "Treatment group")) +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
position = position_dodge(0.9), width = 0.2) +
annotate(geom = "text", x = c(2, 3),
y = c(156 + 5, 270 + 5), label = "*") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "none",
axis.title = element_text(family = "Times New Roman", size = 15),
legend.text = element_text(family = "Times New Roman", size = 12),
axis.text = element_text(family = "Times New Roman", size = 8))
ps <- grid.arrange(grobs = list(p1, p2, p3), ncol =3)
ps
如果图形显示不了的话,点击这个网址