0
votes
  1. I'm trying to make a split barplot with different colors for the x-axis labels. I want the x-axis labels (e.g. Vogesella) to show up as the correct color when they are plotted on the graph according to the host species.
  2. As you can see, none of the Vogesella x-axis labels are correct, and they are defaulting to black. I tried using vectorized input to element_text, but I get a warning saying that it isn't officially supported. Is there some workaround I can do to make this possible?
  3. Here is the code that I used to make the simplified barplot.

System Info: platform x86_64-apple-darwin15.6.0
arch x86_64
os darwin15.6.0
system x86_64, darwin15.6.0
status
major 3
minor 6.3
year 2020
month 02
day 29
svn rev 77875
language R
version.string R version 3.6.3 (2020-02-29) nickname Holding the Windsock

 df %>%
      ggplot( aes(x = genus, y = avg_rel_abund, fill = host_species))+
      geom_bar(stat = "Identity") +
      facet_grid(cols = vars(host_species), scales = "free") +
      scale_fill_brewer(type = "qual", palette = 1) +
      theme(axis.title.x = element_blank(),
      axis.text.x = element_text(angle = 70, hjust = 1, size = 10, 
      color = df$color),
      axis.title.y = element_text(size = 10),
      axis.text.y = element_text(size = 10)) +
      guides(fill = guide_legend(size = 10)) +
      labs( y = "Relative Abundance (Genus)", title = "Day 3 Genera 
      Present in Host Phycospheres", fill = "Host Species") +
      scale_y_continuous("Average Relative Abundance") +
      scale_x_reordered()

Dataset:


    df <- structure(list(genus = structure(c(3L, 1L, 4L, 2L, 5L), .Label = c("Vogesella___Coelastrum", 
    "Vogesella___Scenedesmus", "Vogesella___Chlorella", "Vogesella___Monoraphidium", 
    "Vogesella___Selenastrum"), class = "factor"), host_species = structure(c(1L, 
    2L, 4L, 3L, 5L), .Label = c("Chlorella", "Coelastrum", "Scenedesmus", 
    "Monoraphidium", "Selenastrum"), class = "factor"), avg_rel_abund = c(0.0476201763676562, 
    0.0723617033127999, 0.0254124767307784, 0.0615452700310325, 0.0124210779718639
    ), time = c("D3", "D3", "D3", "D3", "D3"), color = c("#7FC97F", 
    "#BEAED4", "black", "#FDC086", "#386CB0")), row.names = c(NA, 
    -5L), class = c("tbl_df", "tbl", "data.frame"))

Simplified Barplot

Simplified dataframe

1
Welcome to stack overflow. Could you please make your question reproducible: include a minimal dataset in the form of an object for example if a data frame as df <- data.frame(…) where … is your variables and values, in your case agg_genus_data_D3_color or use dput(agg_genus_data_D3_color). These links should be of help: minimal reproducible example and How to AskPeter
I'm a tad confused here. Should I add the output of dput(agg_genus_data_D3_color) to my question?Dylan Baker
Yes, this will make your question reproducible so that potential answers can be tested and verified.Peter
To be honest you would be better off making a much simpler toy dataset to illustrate your problem which seems to be colouring the x axes labels the same colours as your bars. If I understand you correctly. All the other information in your graph, is just distracting clutter to addressing the question, hence the request for a minimal reproducible example.Peter
I apologize for the length of the dataset. I used droplevels() to make it shorter. If it needs to be smaller I can try subsetting or something. Alright give me a moment to shorten itDylan Baker

1 Answers

0
votes

This is one way to do it using geom_col rather than facet_grid. You can adjust the text at the head of the bars and or the plot size to make the text fit.

library(ggplot2)
library(forcats)

ggplot(df, aes(x = fct_inorder(genus, host_species), y = avg_rel_abund, fill = host_species))+
  geom_col() +
  geom_text(aes(label = host_species, y = Inf), vjust = 1, size = 3)+
  scale_fill_brewer(type = "qual", palette = 1) +
  theme(axis.title.x = element_blank(),
        axis.text.x = element_text(angle = 70, hjust = 1, size = 10, 
                                   colour = df$color),
        axis.title.y = element_text(size = 10),
        axis.text.y = element_text(size = 10)) +
  guides(fill = guide_legend(size = 10)) +
  labs(y = "Average Relative Abundance",
       title = "Day 3 Genera\nPresent in Host Phycospheres",
       fill = "Host Species")

Created on 2020-05-29 by the reprex package (v0.3.0)