I am trying to draw connection lines between several bars in a stacked barplot (ggplot2
) in R. For this, I found the ConnLines
function from the DescTools package. I aim for a similar result as shown in the documentation.
taxa_bars <- ggplot(taxa_new,
aes(x=Sample, y=Rel_abund, fill=Kingdom, width=0.5)) +
geom_bar(position="fill", stat="identity", color="black") +
facet_wrap(~Individual, ncol=4) +
ConnLines(taxa_new, beside=FALSE) +
theme_bw() +
theme(axis.title=element_text(size=12, face="bold", color = "black"),
axis.text=element_text(size=11, face="bold", color = "black"),
axis.text.x = element_text(angle = 45, vjust=1.1, hjust=1.1),
legend.title = element_text(size=6, face = "bold", color = "black"),
legend.text = element_text(size=6, face = "bold", color = "black"),
legend.key.size = unit(3, "mm"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
strip.background = element_rect(fill="white", color="white"),
strip.text = element_text(face="bold", color="black")) +
labs(x = "", y = "Abundance [%]", fill = "Taxa")
Throws the following error:
Error in barplot.default(..., plot = FALSE) : 'height' must be a vector or a matrix
taxa_new
is a grouped data frame, since ggplot demands that. Converting it to a matrix within ConnLines
does not do the job either. The plot works fine without the ConnLines
. I assume that ConnLines
is not compatible with ggplot2
and only works with barplot
?