我的数据框如下:
thin50 <- structure(list(yta = c(1102L, 1102L, 1102L, 1103L, 1103L, 1105L,
1105L, 1105L, 1107L, 1107L, 1107L, 1108L, 1108L, 1108L, 1109L,
1109L, 1110L, 1110L, 1110L, 1113L, 1113L, 1102L, 1102L, 1102L,
1103L, 1103L, 1105L, 1105L, 1105L, 1107L, 1107L, 1107L, 1108L,
1108L, 1108L, 1109L, 1109L, 1110L, 1110L, 1110L, 1113L, 1113L,
1102L, 1102L, 1102L, 1103L, 1103L, 1105L, 1105L, 1105L, 1107L,
1107L, 1107L, 1108L, 1108L, 1108L, 1109L, 1109L, 1110L, 1110L,
1110L, 1113L, 1113L), avd = c(6L, 7L, 1L, 2L, 5L, 2L, 3L, 5L,
2L, 7L, 3L, 3L, 1L, 4L, 1L, 2L, 2L, 5L, 6L, 3L, 2L, 6L, 7L, 1L,
2L, 5L, 2L, 3L, 5L, 2L, 7L, 3L, 3L, 1L, 4L, 1L, 2L, 2L, 5L, 6L,
3L, 2L, 6L, 7L, 1L, 2L, 5L, 2L, 3L, 5L, 2L, 7L, 3L, 3L, 1L, 4L,
1L, 2L, 2L, 5L, 6L, 3L, 2L), beh = c("50%", "50%", "UN", "50%",
"UN", "50%", "50%", "UN", "50%", "50%", "UN", "50%", "UN", "UN",
"50%", "UN", "50%", "50%", "UN", "50%", "UN", "50%", "50%", "UN",
"50%", "UN", "50%", "50%", "UN", "50%", "50%", "UN", "50%", "UN",
"UN", "50%", "UN", "50%", "50%", "UN", "50%", "UN", "50%", "50%",
"UN", "50%", "UN", "50%", "50%", "UN", "50%", "50%", "UN", "50%",
"UN", "UN", "50%", "UN", "50%", "50%", "UN", "50%", "UN"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Htveg",
"dod", "sumut"), class = "factor"), value = c(313, 299.6, 353.7,
267.8, 392.9, 191.3, 191.4, 353.4, 238, 226.2, 419.6, 273, 410,
370.6, 317, 515.1, 142.5, 160, 341, 324.5, 447.9, 12.7, 22.6,
101.6, 32.5, 38.9, 17.1, 24.8, 0, 5.2, 10.6, 12.3, 13.5, 29.5,
34.4, 13.9, 19.4, 26.2, 28, 7.6, 15.6, 44.5, 89.6, 92.6, 0, 86.4,
0, 100.4, 119.9, 0, 121.1, 112.9, 0, 90.1, 0, 0, 59.6, 0, 79.6,
83.1, 0, 115.3, 0)), row.names = c(NA, -63L), .Names = c("yta",
"avd", "beh", "variable", "value"), class = "data.frame")
我做了一个条形图:
ggplot(aes(x = paste(yta, beh, sep = ":"), y = value, fill = variable), data = thin50) + stat_summary(fun.y = mean, geom = "bar", position = "stack") +
scale_fill_grey() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(axis.line = element_line(colour = "black"),
axis.text = element_text(color = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position = "none",
text = element_text(size=14, color = "black", vjust = 0.5)) +
labs(x = "Experiment : treatment", y = expression(m^3) )
我得到这个数字:
如何更改浅灰色的y轴的顺序?
答案 0 :(得分:2)
如果您想更改堆叠的顺序,您需要先更改因子级别的顺序,例如:
thin50$variable <- factor(thin50$variable, levels=c("Htveg", "sumut", "dod"))
如果您希望颜色保持相同的顺序,或者它们在因子级别保持不变,那么我并不完全清楚,因此您可以保留scale_fill_grey()
或添加{{ 1}}:
scale_fill_manual()
答案 1 :(得分:1)
如果你的意思是切换着色:
添加以下内容:
+ scale_fill_grey(start=.6, end=.1)
以下列方式:
ggplot(aes(x = paste(yta, beh, sep = ":"), y = value, fill = variable), data = thin50) + stat_summary(fun.y = mean, geom = "bar", position = "stack") +
scale_fill_grey() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(axis.line = element_line(colour = "black"),
axis.text = element_text(color = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position = "none",
text = element_text(size=14, color = "black", vjust = 0.5)) +
labs(x = "Experiment : treatment", y = expression(m^3) ) +
scale_fill_grey(start=.6, end=.1)
输出:
如果您想简单地在y轴上颠倒图形而不改变颜色或值,您可以添加:
+ scale_y_reverse()
如下:
ggplot(aes(x = paste(yta, beh, sep = ":"), y = value, fill = variable), data = thin50) + stat_summary(fun.y = mean, geom = "bar", position = "stack") +
scale_fill_grey() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(axis.line = element_line(colour = "black"),
axis.text = element_text(color = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position = "none",
text = element_text(size=14, color = "black", vjust = 0.5)) +
labs(x = "Experiment : treatment", y = expression(m^3) ) +
scale_y_reverse()
输出:
我不再收到聊天回复,所以我不知道这是不是你想要的。
但这里是条形图间隔的图表:
这是通过以下代码实现的:
ggplot(thin50,aes(x = paste(yta, beh, sep = ":"), y = value, fill=factor(variable))) + #stat_summary(fun.y = mean, geom = "bar", position = "stack") +
scale_fill_grey() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(axis.line = element_line(colour = "black"),
axis.text = element_text(color = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(), # this line is not needed. Only included as a fix for legend/key random line
legend.position = "none",
text = element_text(size=14, color = "black", vjust = 0.5)) +
geom_bar(position=position_dodge(.9), stat="identity", colour="black") +
labs(x = "Experiment : treatment", y = expression(m^3) )
上面代码的重要部分是:
+ geom_bar(position=position_dodge(.9), stat="identity", colour="black")
还有一些重叠的酒吧,但这是因为你没有平均所有的东西和&amp;因此有重复的变量。如果使用聚合函数,它将负责这种冗余。