我正在使用堆积条形图来显示物种相对优势。
使用以下代码生成条形图:
RelDom <- RelDom[order(RelDom[,2]),] # rank by column A
RelDom
RelDom %>%
gather(LU, RD, -species) -> likert
likert
likert %>%
filter(LU=="A") %>%
arrange(RD) %>% .$species -> ind_order
ind_order
likert %>%
mutate(species=factor(species, levels=ind_order, ordered=TRUE)) %>%
mutate(LU=factor(LU,
levels=c("A", "C", "B"), ordered=F,
labels=c("A", "C", "B"))) -> lik
lik
tiff(file = "RD.tiff", height=10, width=20, units="in", res=300, compression="lzw")
ggplot() +
geom_hline(yintercept=0, lwd=1) +
geom_bar(data=lik, width=.75,
stat="identity", position="stack",
aes(x=species, y=RD, fill=LU)) +
annotate("text", x = 2, y=-50, label = "Old", size=8) +
annotate("text", x = 2, y=70, label = "New", size=8) +
scale_x_discrete(limits = rev(levels(lik$species)),expand=c(0,0)) +
scale_fill_manual(values=c( "darkgreen", "red","blue"),
drop=FALSE) +
scale_y_continuous(expand=c(0,0),
limits=c(-100, 200),
breaks=c(-100,-50,0,50,100),
labels=c("100","50","0","50","100")) +
coord_flip() +
xlab("Species") +
ylab("Relative Dominance") +
theme_bw() +
theme(legend.position = c(0.9, 0.1)) +
theme(legend.title = element_blank()) +
theme(legend.text = element_text(colour="black", size = 14)) +
theme(legend.background = element_rect(fill="white", size=0.5, linetype="solid", colour ="black")) +
theme(axis.title.x = element_text(vjust=0.5,face="bold", size=16),
axis.text.x = element_text(vjust=4, size=14)) +
theme(axis.title.y = element_text(angle=90, vjust=0.70, face="bold", size=18),
axis.text.y = element_text(size=14)) +
theme(panel.grid.minor=element_blank()) +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid=element_blank()) +
theme(panel.border = element_rect(size=1, color = "black")) +
theme(plot.margin = unit(c(0.2,0.9,0.3,0.2),"lines"))
dev.off()
该图如下:
现在,我希望每个蓝色列直接位于相应的红色列顶部,而不是水平堆叠蓝色和红色。这应该是蓝色和红色列只是当前宽度的一半,因此它们的组合与绿色列的宽度相匹配。
此外,我正在努力让传奇按照绿色,蓝色和红色的顺序显示。
非常感谢任何建议。
这是可重现的数据:dput(RelDom):
structure(list(species = structure(c(1L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("Sp1", "Sp10",
"Sp11", "Sp12", "Sp13", "Sp14", "Sp15", "Sp2", "Sp3", "Sp4",
"Sp5", "Sp6", "Sp7", "Sp8", "Sp9"), class = "factor"), A = c(-73.55,
-72.42, -35.62, -12.45, -8.89, -7.26, -6.6, -6.42, -6.02, -5.26,
-4.59, -4.31, -3.53, -3.25, -2), B = c(64.54, 88.06, 39.57, 14.64,
6.6, 10.55, 3.87, 7.35, 5.09, 1.88, 6.84, 10.34, 2.17, 2.36,
1.36), C = c(47.35, 78.55, 39.35, 21.96, 6.25, 7.64, 3.28, 8.94,
3, 6.04, 5.16, 3.63, 5.42, 12.34, 5.03)), .Names = c("species",
"A", "B", "C"), row.names = c(NA, 15L), class = "data.frame")
答案 0 :(得分:5)
如果我理解正确你想要蓝色和绿色的条子并排,而绿色应该与现在的位置相同。
在geom_bar
和A
之间设置两个dodge
,并且只设置一个A
和stack
。
至于图例中颜色的顺序,如果您希望图例为A
,B
,则C
会更改LU
中的级别顺序。我认为这不是你的意思,因为你故意使用这一行:
....
mutate(LU=factor(LU,
levels=c("A", "C", "B"), ordered=F,
labels=c("A", "C", "B"))) -> lik
所以我想您希望C
为blue
而B
,red
只需更改color_fill_manual
行:
scale_fill_manual(values=c( "darkgreen", "blue", "red")
library(ggplot2)
ggplot() +
geom_bar(data=lik[lik$LU!="A",], width=.75,
stat="identity", position="dodge",
aes(x=species, y=RD, fill=LU)) +
geom_bar(data=lik[lik$LU=="A",], width=.75,
stat="identity", position="stack",
aes(x=species, y=RD, fill=LU)) +
geom_hline(yintercept=0, lwd=1) +
annotate("text", x = 2, y=-50, label = "Old", size=8) +
annotate("text", x = 2, y=70, label = "New", size=8) +
scale_x_discrete(limits = rev(levels(lik$species)),expand=c(0,0)) +
scale_fill_manual(values=c( "darkgreen", "blue", "red"),
drop=FALSE) +
scale_y_continuous(expand=c(0,0),
limits=c(-100, 200),
breaks=c(-100,-50,0,50,100),
labels=c("100","50","0","50","100")) +
coord_flip() +
xlab("Species") +
ylab("Relative Dominance") +
theme_bw() +
theme(legend.position = c(0.9, 0.1)) +
theme(legend.title = element_blank()) +
theme(legend.text = element_text(colour="black", size = 14)) +
theme(legend.background = element_rect(fill="white", size=0.5, linetype="solid", colour ="black")) +
theme(axis.title.x = element_text(vjust=0.5,face="bold", size=16),
axis.text.x = element_text(vjust=4, size=14)) +
theme(axis.title.y = element_text(angle=90, vjust=0.70, face="bold", size=18),
axis.text.y = element_text(size=14)) +
theme(panel.grid.minor=element_blank()) +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid=element_blank()) +
theme(panel.border = element_rect(size=1, color = "black")) +
theme(plot.margin = unit(c(0.2,0.9,0.3,0.2),"lines"))