【问题标题】:GGplot pie chart error: Aesthetics must be either length 1 or the same as the data (98): yGGplot饼图错误:美学必须是长度1或与数据相同(98):y
【发布时间】:2020-06-06 03:44:27
【问题描述】:

我想使用 ggplot2 在 R 中创建一个饼图。我的代码是:

year.count <- c(24, 40, 30, 4)
prop.year <- year.count / sum(year.count) * 100
ypos.year <- cumsum(prop.year) - 0.5 * prop.year

ggplot(dat, aes(x = "", y = prop.year, fill = year)) +
  geom_bar(stat = "identity", width = 1, color = "white") +
  coord_polar("y", start = 0) +
  geom_text(aes(y = ypos.year, label = year), color = "white", size = 5)

代码引用自https://www.r-graph-gallery.com/piechart-ggplot2.html。 不幸的是,我总是收到一条错误消息:

Error: Aesthetics must be either length 1 or the same as the data (98): y

我应该怎么做才能解决这个问题?赞赏!

【问题讨论】:

    标签: r ggplot2 pie-chart


    【解决方案1】:

    不清楚你想要什么。数据框dat 未定义且year 未唯一标识。这是一种可能的解释:

    year.count <- c(24, 40, 30, 4)
    prop.year <- year.count / sum(year.count) * 100
    ypos.year <- cumsum(prop.year) - 0.5 * prop.year
    dat <- data.frame("prop.year" = prop.year,
                      "ypos.year" = ypos.year,
                      "year.count" = year.count)
    ggplot(dat, aes(x = ypos.year, y = prop.year, fill = year.count)) +
      geom_bar(stat = "identity", width = 1, color = "white") +
      coord_polar("y", start = 0) +
      geom_text(aes(y = ypos.year, label = year.count), color = "white", size = 5)
    

    【讨论】:

    • 感谢您提供答案!我还发现了我没有定义 dat 数据框的问题。该问题现已修复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    相关资源
    最近更新 更多