多面饼图ggplot2-不完整,仅线条

时间:2018-11-14 18:48:11

标签: r ggplot2 charts

dat <- data.frame(table(total$Name, total$Color))
# adds the Count column
names(dat) <- c("Name","Color","Count")

NAME    COLOR    Count
John    Green    1
Joe     Green    12
Jane    Green    32  
Jill    Green    34
John    Blue     2
Joe     Blue     4
Jane    Blue     23  
Jill    Blue     12
John    Red      4
Joe     Red      42
Jane    Red      36  
Jill    Red      15 

如何按名称创建一组多面饼图?当我尝试时:

e <- ggplot(data=dat, aes(x=Name, fill=Color)) 
e + geom_bar(position = "fill") + 
facet_wrap(~Name) +
coord_polar()

它产生的饼图只是线条。任何人都可以识别错误或提供任何解决方案吗?

1 个答案:

答案 0 :(得分:2)

  1. x中的aes设置为静态值。我选择了0。
  2. 添加theta =“ y” to coord_polar`以告诉它以角度使用哪个轴。
  3. 添加scale_fill_identity(),因为您已将颜色直接映射到填充值中。
  4. 添加了theme_void()以摆脱混乱的轴等。
library(tidyverse)

dat <- tribble(~Name,~Color,~Count,
"John",    "Green",    1,
"Joe" ,   "Green",   12,
"Jane",    "Green",    32, 
"Jill",    "Green",    34,
"John",    "Blue",   2,
"Joe" ,   "Blue",   4,
"Jane",    "Blue",   23,  
"Jill",    "Blue",   12,
"John",    "Red",   4,
"Joe" ,   "Red",   42,
"Jane",    "Red",   36,  
"Jill",    "Red",   15)

ggplot(data=dat, aes(x = 0, y = Count, fill=Color)) + 
  geom_col(position = "fill") + 
  facet_wrap(~Name) +
  coord_polar(theta = "y") +
  scale_fill_identity() +
  theme_void()

reprex package(v0.2.1)于2018-11-14创建