简体   繁体   English

如何修改双面堆叠条形图

[英]How to modify two-sided stacked barchart

I am using a stacked barchart to display species relative dominance. 我正在使用堆叠的条形图来显示物种的相对优势。

The barchart is generated with the following code: 条形图使用以下代码生成:

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()

The figure looks as follows: 该图如下所示:

在此处输入图片说明

Now, instead of the blue and red being stacked horizontally, I would like each blue column to be directly on top of a respective red column. 现在,我不希望蓝色和红色水平堆叠,而是希望每个蓝色列都直接位于相应红色列的顶部。 This should be in a way that both the blue and red columns are only half their current width, so that combined they match the width of the green column. 这样做的方式应该是,蓝色和红色列都仅是其当前宽度的一半,因此组合起来就可以与绿色列的宽度匹配。

Also, I am struggling to get the legend to display in the order green, blue and red. 另外,我正努力使图例以绿色,蓝色和红色的顺序显示。

Many thanks in advance for any advice. 非常感谢您的任何建议。

This is the reproducible data: dput(RelDom): 这是可重现的数据: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")

if I understood correctly you would like the blue and green bars to be side by side while the green should be on the same position as now. 如果我理解正确,则您希望蓝色和绿色的条并排放置,而绿色的条应与现在位于同一位置。

Make two geom_bar one without A and dodge , and one with only A and stack . 制作两个geom_bar一个不带Adodge ,另一个不带Astack As for the order of the colors in the legend, if you would like the legend to be A , B , C change the order of levels in LU . 至于图例中颜色的顺序,如果您希望图例为ABC更改LU中色阶的顺序。 I assume this is not what you mean since you on purpose use this line: 我认为这不是您的意思,因为您故意使用此行:

....
mutate(LU=factor(LU,
                        levels=c("A", "C", "B"), ordered=F,
                        labels=c("A", "C", "B"))) -> lik 

So I guess you would like C to be blue and B , red just change the color_fill_manual line: scale_fill_manual(values=c( "darkgreen", "blue", "red") 因此,我想您希望CblueB 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"))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM