简体   繁体   English

ggplot2和R中的简单饼图问题

[英]Simple pie chart problem in ggplot2 and R

I'm trying to create a very simple pie chart using ggplot2.我正在尝试使用 ggplot2 创建一个非常简单的饼图。 The proportions are incorrect as the data I have for three "type" categories is: "M-types" = 7, "N-types" = 151, "E-types" = 57 (see below for output of dput() to generate ).比例不正确,因为我对三个“类型”类别的数据是:“M-types”= 7、“N-types”= 151、“E-types”= 57(见下文 output 的 dput() 到产生 )。

N and Chronotype are my column headers as defined by colnames(). N 和 Chronotype 是我的列标题,由 colnames() 定义。 Here is the core of my code:这是我的代码的核心:

pie = ggplot(df, aes(x="", y=N, fill=Chronotype))+
  geom_bar(width = 1, stat = "identity")

pie = pie + 
  coord_polar("y", start=0)

错误的饼图

Data from dput():来自 dput() 的数据:

structure(list(N = structure(c(3L, 1L, 2L), .Label = c("151", 
    "57", "7"), class = "factor"), Chronotype = structure(c(2L, 3L, 
    1L), .Label = c("E-type", "M-type", "N-type"), class = "factor")), class = "data.frame", row.names = c(NA, 
    -3L))

Thanks to Jon, I just made the y data numeric:感谢 Jon,我刚刚将 y 数据设为数字:

pie = ggplot(df, aes(x="", y=as.numeric(as.character(N)), fill=Chronotype))+
  geom_col() #(width = 1, stat = "identity")

pie = pie + 
  coord_polar("y", start=0)

pie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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