【问题标题】:ggplot invalid argument to unary operatorggplot 对一元运算符无效的参数
【发布时间】:2018-04-24 13:00:02
【问题描述】:

我在运行 ggplot 饼图时收到以下错误消息...知道可能是什么问题吗?

代码是:

ggplot(pie_unrated, aes(x = "FEBRUARY IBG UNRATED Book COMPOSITION", y = prop, 
fill = ProductDetails)) + geom_bar(width = 1, 
stat = "identity", color = "white") 
+ coord_polar(theta = "y", start = 0) + ggpubr::fill_palette("jco") 
+theme_void()

我的错误信息:

 Error in +coord_polar(theta = "y", start = 0) : 
    invalid argument to unary operator

>

 dput(head(pie_unrated)
structure(list(RatingStatus = c("UNRATED", "UNRATED", "UNRATED", 
"UNRATED", "UNRATED", "UNRATED"), ProductDetails = structure(c(1L, 
2L, 6L, 7L, 9L, 10L), .Label = c("ACB", "Bonds", "Cash and Short Term", 
"Deposit with Banks", "LBD", "LC", "LG", "loan", "Loan", "OD", 
"Treasury Bonds"), class = "factor"), counts = c(10L, 1L, 21L, 
102L, 758L, 126L), prop = c(1, 0.1, 2.1, 10, 74.5, 12.4), lab.ybos = c(0.5, 
1.05, 2.15, 8.2, 50.45, 93.9)), .Names = c("RatingStatus", "ProductDetails", 
"counts", "prop", "lab.ybos"), row.names = c(NA, -6L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), vars = "RatingStatus", drop = TRUE, indices = list(
    0:5), group_sizes = 6L, biggest_group_size = 6L, labels = structure(list(
    RatingStatus = "UNRATED"), row.names = c(NA, -1L), class = "data.frame", vars = "RatingStatus", drop = TRUE, .Names = "RatingStatus"))

【问题讨论】:

  • 它在那里 -- 在 coord_polar 之前
  • 你是对的,但ggplot2 不喜欢代码行开头的+
  • 啊啊好吧——明白了。非常感谢! ggplot 是个挑剔的
  • 明确地说,这是 R 的东西而不是 ggplot 的东西。如果你在一行有1,在下一行有+2,你不会得到3。(但是,你也不会得到一个错误,因为+2本身就是有效的语法,不像+ coord_polar()它不能独立存在。)

标签: r ggplot2 pie-chart


【解决方案1】:
library(ggpubr)

ggplot(pie_unrated, 
     aes(x = "FEBRUARY IBG UNRATED Book COMPOSITION", y = prop, fill = ProductDetails)) + 
geom_bar(width = 1,stat = "identity", color = "white") + 
coord_polar(theta = "y", start = 0) + ggpubr::fill_palette("jco") + theme_void()

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多