簡體   English   中英

添加圖例並在分組的條形圖上更改顏色

[英]Add Legend and change Colour on grouped bar plot

我創建了這樣的情節;

library("ggplot2")    
ggplot(data = diamonds) + 
      geom_bar(mapping = aes(x = color, y = ..prop.., group = 2)) + 
      scale_y_continuous(labels=scales::percent) +
      facet_grid(~cut)

現在,我想為變量“ color”添加圖例,也想更改條形的顏色。 該圖正是我想要的樣子,並且如果可能的話,我不想更改數據集的結構,只需添加圖例並更改顏色即可。

我找不到適合這種“百分比”樣式圖形的示例。

ggplot(data = diamonds, aes(x = color, y = ..prop.., group = cut)) + 
  geom_bar(aes(fill = factor(..x.., labels = LETTERS[seq(from = 4, to = 10 )]))) + 
  labs(fill = "color") + 
  scale_y_continuous(labels = scales::percent) + 
  facet_grid(~ cut)

在此處輸入圖片說明

暫無
暫無

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

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