簡體   English   中英

ggplot 更改欄 colors 和 R 中的圖例標題

[英]ggplot change bar colors and legend titles in R

所以我將三個不同的圖映射到一個 plot 上。讓圖例標題正確花了一些時間,但現在我似乎無法在不弄亂標題的情況下更改條形圖的 colors。 我希望 colors 欄更簡單(灰度或黑色/紅色)並且我想更改圖例標題。 我通常可以更改我想要的一件事,但我最終會失去其他有用的功能謝謝!

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)) 

在此處輸入圖像描述

由於您沒有提供任何數據,我無法檢查我的代碼,但這應該可以滿足您的需求:

  1. 在我看來,在 geom 中設置標簽不是一個好主意。 只需使用簡單的占位符,如“fill1”或...

  2. 要分配正確的 colors 和標簽,請使用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)) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM