0

I am using the following code to produce the scatter diagram of a Redundancy Analysis (RDA). The plot is for only one species and I am conducting this analysis for two other species (I am not showing the code for the other two species as it is basically the same).

rda.plot.sap <- ggplot(df1, aes(x=RDA1, y=RDA2)) + 
  geom_point(aes(shape = df1[,"Enclos"], color = df1[,"Type_enclos"]), size = 2) +
  geom_hline(yintercept=0) + 
  geom_vline(xintercept=0) + 
  coord_fixed() + 
  scale_shape_manual(values = c(1, 19)) + 
  scale_color_manual(values=c('#999999','#E69F00'))  

rda.plot.sap <- rda.plot.sap + 
  geom_segment(data=df2, 
               aes(x=0, xend=RDA1, y=0, yend=RDA2), 
               color="red", arrow=arrow(length=unit(0.01,"npc")), size = 0.8) +
  geom_text(data=df2, 
            aes(x=RDA1, y=RDA2, label=rownames(df2), 
                hjust=0.5*(1-sign(RDA1)) + hjust_sap_x,
                vjust=0.5*(1-sign(RDA2) + vjust_sap_x)), 
            color="red", size=5)

rda.plot.sap <- rda.plot.sap + 
  geom_segment(data=df3, 
               aes(x=0, xend=RDA1, y=0, yend=RDA2), 
               color="blue", arrow=arrow(length=unit(0.01,"npc")), size = 0.8)+ 
  geom_text(data=df3, 
            aes(x=RDA1, y=RDA2, label=rownames(df3), 
                hjust=0.5*(1-sign(RDA1)),
                vjust=0.5*(1-sign(RDA2))), 
            color="blue", size=5)

rda.plot.sap <- rda.plot.sap + 
  theme(panel.background = element_blank(), 
        axis.title = element_text(size = 20), 
        axis.line.x = element_line(color="black", size = 1),
        axis.line.y = element_line(color="black", size = 1),
        axis.text = element_text(size = 15), 
        legend.title = element_blank(), 
        legend.text = element_text(size = 15),
        legend.key=element_blank(),
        legend.position = c(0.15, 0.9)) + 
  xlim(c(-0.6, 0.4))  

rda.plot.sap <- rda.plot.sap + 
  xlab(paste("RDA1 (", var.rda1, " % - p = ", p.rda1, ")", sep = "")) + 
  ylab(paste("RDA2 (", var.rda2, " % - p = ", p.rda2, ")", sep = ""))

The code works perfectly fine, and I obtain three separate plots without any error or warnings. The problem is that when I try to assemble these three plots using the function plot_grid of the cowplot package:

final_plot <- plot_grid(rda.plot.sap, rda.plot.epi, rda.plot.het, 
                        nrow = 1, ncol = 3, labels = c("A", "B", "C"))  

I always get the same simple error :

"Error: Aesthetics must be either length 1 or the same as the data (27): shape, colour".

Even stranger, after getting this error, if I want to run again the code of one of the individual plots (of one species only), I get the same error.

This is my first post so I hope I described the problem accurately enough. I am at a loss to understand what is going on here, so thanks in advance to whoever can help.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • 1
    Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung May 16 '19 at 17:01
  • 1
    It's not really possible to answer your question without a fully reproducible example. However, whenever you're feeding data into an `aes()` statement (as in `aes(shape = df1[,"Enclos"], color = df1[,"Type_enclos"])`) that's a sign that you're fundamentally misunderstanding how ggplot2 aesthetic mapping works. I would start with reading up on that, e.g.: https://r4ds.had.co.nz/data-visualisation.html#aesthetic-mappings – Claus Wilke May 16 '19 at 22:14

2 Answers2

0

I'm not sure why, but removing the labels argument from plot_grid() usually fixes this. (You just need to add the labels to each plot individually with geom_text() or ggtitle().)

Lisa DeBruine
  • 818
  • 9
  • 10
0

According to the comment found for this gist, the issue has to do with custom theme not setting the required aesthetics for plot_grid to use for the labels. See the following fix:

final_plot <- plot_grid(rda.plot.sap, rda.plot.epi, rda.plot.het, 
                        nrow = 1, ncol = 3, labels = c("A", "B", "C"),
                        label_fontfamily = "Times", label_colour = "black")  
Mosquite
  • 588
  • 1
  • 4
  • 15