2

I am trying to depict the mean crude protein content (y-axis) of 10 plants by growth stage (x-axis) and then facet by each individual species. There are 10 species I want to depict in the fashion, and then on each facet I also want to show the data for 2 "benchmark" species for comparison. The data for the 10 faceted species are in a separate dataframe from the 2 benchmark species but all column headers are the same between both dataframes.

ggplot() +
   geom_point(data = ten_spp_growth_stage, aes(x = growth_stage_corrected, y = mean_CP), group = 1, size = 2) + 
   geom_line(data = ten_spp_growth_stage, aes(x = growth_stage_corrected, y = mean_CP), group = 1) +
   geom_errorbar(data = ten_spp_growth_stage, aes(x = growth_stage_corrected, ymin = mean_CP - CP_se, ymax = mean_CP + 
    CP_se), width = 0.2) + 
   geom_point(data = grass_CP_df, aes(x = growth_stage_corrected, y = mean_CP, group = 1, col = sci_name_corrected)) +
   geom_line(data = grass_CP_df, aes(x = growth_stage_corrected, y = 
mean_CP, group = 1, col = sci_name_corrected)) +  
   theme(legend.position = "bottom") + ylab("Mean crude protein (%) ± SEM") + xlab("Growth stage") +
   theme_bw() + theme(panel.grid.minor = element_blank()) + 
   theme(legend.title = element_blank(), legend.position = "bottom", 
axis.text.x = element_text(size = 9, angle = 45, color = "black", hjust = 1), axis.text.y = element_text(size = 12, color="black"), axis.title.x = element_text(size = 12, color="black"), axis.title.y = element_text(size = 12, color="black")) +
   facet_wrap(facets = ~ reorder(sci_name_corrected, desc(mean_CP)), nrow = 2)

Facetted figure by species.

So far I have made the facetted figure but am unable to get the 2 curves for the "benchmark" species to repeat themselves within each facet. Rather, they are showing up as additional, separate facets (the last two species that are colored blue and red). What I want is for the blue and red curves to both be repeated in the facet for each of the other 10 species not show up as facets 11 and 12.

stefan
  • 90,330
  • 6
  • 25
  • 51
  • 2
    Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data(-sets) or some fake data best shared via `dput()`. – stefan Jun 10 '23 at 07:30
  • 1
    This said. The issue is most likely that `grass_CP_df` contains the a column with the facetting variable `sci_name_corrected`. Hence, your benchmark lines are treated as separate categories and you get a facet panel for each. In that case simply rename `sci_name_corrected` in `grass_CP_df` to e.g. `sci_name_corrected1`. – stefan Jun 10 '23 at 07:34
  • 1
    @dput(df) would help all the people who can provide the solution instead of recreating data again – PesKchan Jun 10 '23 at 08:30
  • Being new to SO, I'm not sure how to accept a comment as the answer but Stefan's comment re: changing the column header in the second dataframe to something different from that in the first dataframe did the trick. Thank you! – Autumn Smart Jun 12 '23 at 14:29

0 Answers0