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):
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 withoutA
anddodge
, and one with onlyA
andstack
. As for the order of the colors in the legend, if you would like the legend to beA
,B
,C
change the order of levels inLU
. I assume this is not what you mean since you on purpose use this line:So I guess you would like
C
to beblue
andB
,red
just change thecolor_fill_manual
line:scale_fill_manual(values=c( "darkgreen", "blue", "red")