0

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?

Bianca
  • 1
  • 2
  • Yep. You are right. ConnLines works only with base R plots. But if you are looking for a ggplot2 approach have a look at https://stackoverflow.com/questions/57263260/how-to-use-geom-bar-for-making-connected-bar-plot-in-ggplot2 or https://stackoverflow.com/questions/64375913/how-to-use-geom-bar-to-connect-stacked-bar-proportions-if-name-categorial-for-ba/64376210#64376210 – stefan Feb 04 '21 at 17:46
  • Thanks @stefan ! – Bianca Feb 04 '21 at 18:31

0 Answers0