简体   繁体   中英

ggplot change bar colors and legend titles in R

So I'm mapping three different plots onto one plot. Getting the legend titles to be correct took some time but now I can't seem to change the colors of the bar plots without messing up the titles. I would like the bar colors to be simpler (greyscale or black/red) and I'd like to change the legend titles. I can usually change one thing I want but I end up losing other features that are helpful Thanks!

ggplot(data = media_impact_by_state) +
  #plot the deviation from state mean for believe CC is
  geom_bar(aes(x = reorder(GeoName,happening - mean(happening)),
               y = happening - mean(happening),
               fill = "Believe in Climate Change"),
               stat = 'identity') +
  scale_color_manual(values = "black",label = "Boo") +
  #plot the deviation from the mean for distrust of climate scientists
  geom_bar(aes(x= GeoName,
               y= trustclimsciSSTOppose - mean(trustclimsciSSTOppose),
               fill = "Do Not Trust Climate Scientists"),
           stat = 'identity') +
  #plot the difference in belief and and trust
  geom_point(aes(x = GeoName,
                 y = (happening - mean(happening))- 
                    (trustclimsciSSTOppose - mean(trustclimsciSSTOppose)),
                    color = "(Belief) - (Distrust)")) +
  scale_color_manual(values = "black") +
  labs(x = "State",
       y = "% Deviation from Mean") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) 

在此处输入图像描述

As you provided no data I could not check my code but this should do what you are looking for:

  1. In my opinion it's not a good idea to set the labels inside the geom. Simply use easy placeholders like "fill1" or...

  2. To assign the right colors and labels make use of named vectors inside scale_fill_manual .

    ggplot(data = media_impact_by_state) +
      geom_bar(aes(x = reorder(GeoName,happening - mean(happening)),
                   y = happening - mean(happening), fill = "fill1"),
               stat = 'identity') +
      scale_color_manual(values = "black", label = "Boo") +
      geom_bar(aes(x= GeoName, y= trustclimsciSSTOppose - mean(trustclimsciSSTOppose), fill = "fill2"),
               stat = 'identity') +
      geom_point(aes(x = GeoName, y = (happening - mean(happening)) - 
                       (trustclimsciSSTOppose - mean(trustclimsciSSTOppose)),
                     color = "(Belief) - (Distrust)")) +
      scale_color_manual(values = "black") +
      scale_fill_manual(values = c(fill1 = "red", fill2 = "black"),
                        labels = c(fill1 = "Believe in Climate Change", fill2 = "Do Not Trust Climate Scientists")) +
      labs(x = "State",
           y = "% Deviation from Mean") +
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM