Home > front end >  How to make one plot's fill color match another plots line color in GGplot2
How to make one plot's fill color match another plots line color in GGplot2

Time:03-25

I have a dataset where I'm trying to make 3 graphs for the same variable (Den Volume) grouped by Species Group. I factorize the Species Group Column to control the order of the display, but the color order is not preserved between line and fill.

Here's the code snippet:

ggthemr("flat")

cts_table_Complete$SppGroups <- 
  factor(cts_table_Complete$SppGroups, 
         levels = c("None", 
                    "Arachnids",
                    "Herptiles (excl. CTS)", 
                    "Kangaroo Rat", 
                    "Rodents")
         ) #reorder so "None" is first    

vol_gg_bar <- ggplot(cts_table_Complete, aes(x = Den.Volume.in3, 
                                             fill = SppGroups))  
  scale_x_continuous(limits = c(0,200))  
  geom_bar(stat = 'bin', alpha = 0.8)  
  theme(legend.position = "left")  
  labs(fill = "Species Group", 
       x = "Den Volume (Cubic Inches)",
       y = "Number of Observations")


vol_gg_dense <- ggplot(cts_table_Complete, aes(x = Den.Volume.in3, 
                                               color = SppGroups))  
  scale_x_continuous(limits = c(0, 200))  
  theme(legend.position = "left")  
  labs(fill = "Species Group", 
       x = "Den Volume (Cubic Inches)",
       y = "Porportion of Observations")  
  geom_density(alpha = 0.6, size = 2)  
  scale_color_discrete(aesthetics = c("colour"))
  

grid.arrange(vol_gg_bar, vol_gg_dense, ncol=2)

Unfortunately the bar chart's fill comes out like this:

bar chart fill

and the lines on the density chart come out like this:

enter image description here

I'd like to change the lines to match the fill, so because I also have a box plot using fill colors and I'd like all 3 to match.

CodePudding user response:

The issue is that ggthemr("flat") overrides the default ggplot2 color palettes. However, looks like that does not work in all instances.

But according to the docs

To avoid this and keep using ggthemr colours in these instances, please add a scale_colour_ggthemr_d() layer to your ggplot call.

Hence, adding scale_colour_ggthemr_d() instead of scale_color_discrete fixes your issue:

Using mtcars as example data:

library(ggthemr)
#> Loading required package: ggplot2
library(ggplot2)
library(gridExtra)

ggthemr("flat")

vol_gg_bar <- ggplot(mtcars, aes(mpg, fill = factor(am)))  
  scale_x_continuous(limits = c(0,200))  
  geom_bar(stat = 'bin', alpha = 0.8)  
  theme(legend.position = "left")  
  labs(fill = "Species Group", 
       x = "Den Volume (Cubic Inches)",
       y = "Number of Observations")


vol_gg_dense <- ggplot(mtcars, aes(mpg, colour = factor(am)))  
  scale_x_continuous(limits = c(0, 200))  
  theme(legend.position = "left")  
  labs(fill = "Species Group", 
       x = "Den Volume (Cubic Inches)",
       y = "Porportion of Observations")  
  geom_density(alpha = 0.6, size = 2)  
  scale_colour_ggthemr_d()

grid.arrange(vol_gg_bar, vol_gg_dense, ncol=2)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Warning: Removed 4 rows containing missing values (geom_bar).

  • Related