簡體   English   中英

重置每個方面的顏色ggplot2 R

[英]Reset color in each facet ggplot2 R

我有三個變量的頻率計數,並希望在餅圖中顯示頻率計數。 我嘗試了ggplot並使用了以下代碼:

library(ggplot2)
df = data.frame(var = rep(c('a','b','c'),each = 3),
            class = letters[1:9],
            count = rep(1:3, 3))

ggplot(df, aes(x = '', y = count, fill = class)) + 
  geom_bar(width = 0.5, stat = 'identity') + 
  coord_polar('y', start = 10) + facet_wrap(~var) + 
  theme(legend.position = 'none')

我得到以下圖: 在此處輸入圖片說明

但是,我想要這樣的東西:

在此處輸入圖片說明

如何重置每個面板中的顏色?

您必須引入一個在每個方面都相同的虛擬變量:

df = data.frame(var = rep(c('a','b','c'),each = 3),
                class = letters[1:9],
                dummy = rep(letters[1:3], 3),
                count = rep(1:3, 3))

ggplot(df, aes(x = '', y = count, fill = dummy)) + 
  geom_bar(width = 0.5, stat = 'identity') + 
  coord_polar('y', start = 10) + facet_wrap(~var)

在此處輸入圖片說明

我刪除了theme(legend.position = 'none')行,因此該圖實際上看起來像您創建的圖。

暫無
暫無

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

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