Search code examples
rggplot2color-scheme

apply ggplot color scale gradient to portion of data


I have a question about applying ggplot's color scale gradient. I have dataset where the response variable is a continuous variable including both positive and negative numbers, and the independent variable is a number of independent sites. I'm trying to plot the data in such a way that I can plot all of the data in the background and then apply a color scale gradient to response data that covers the negative range of the data. This is what I have so far with a example dataset that mimics the structure of my actual dataset.

 tr_sim <- data.frame(site_id = seq(1,100,1), estimated_impact = 
    rnorm(100,18,200), impact_group = rep(c(1,2),each = 50))

    rng_full <- range(tr_sim$estimated_impact)
    #produce plot showing the full range of impacts across all sites and then 
    over the subsetted sites

    impact_plot_full <- ggplot(data = tr_sim, aes(x = factor(site_id, levels = 
    site_id[order(estimated_impact)]), y = estimated_impact)) +
    geom_bar(stat = "identity",width = 1, fill = "grey80") 

    impact_plot_full 

    impact_plot_full + 
   geom_bar(stat = "identity", width = 1, position = "stack", aes(y  = 
   estimated_impact[impact_group == 1])) +
  scale_fill_gradient2(low="firebrick", mid="yellow", high = "green4") +
  labs(y = "Estimated Impact ($/week)", x = "Total number of sites with estimate 
  is 100", title = "Sites with the greatest impact after coverage loss") +
  theme(axis.text.x = element_blank()) +
  scale_y_continuous(breaks = 
  round(seq(rng_full[1],rng_full[2],by=100),digits=0)) 

I can plot all of the data in the background in gray and I'm attempting to plot the negative range of the response data on top of this. I get the error that 'aesthetics must be either length 1 or the same as the data(100), y,x'. I know this is occurring because the negative data is not the same length as the entire dataset, but I can not figure out a way to do this. Any suggestions would be greatly appreciated.

Thank you, Curtis


Solution

  • You need to subset the data and use fill in the aes() for geom_bar.

    impact_plot_full + 
    geom_bar(data = subset(tr_sim, estimated_impact < 0), 
             stat = "identity",
             aes(y = estimated_impact, fill = estimated_impact)) + 
    scale_fill_gradient2(low = "firebrick", mid = "yellow", high = 
    "green4") +
    theme(axis.text.x = element_blank()) +
    xlab("site_id")
    

    enter image description here

    Hope this is what You are looking for.