【问题标题】:ggplot2 using the coord_polar()_error observedggplot2 使用 coord_polar()_error 观察到
【发布时间】:2021-11-06 01:51:47
【问题描述】:

我正在尝试使用 coord_polar 使用 ggplot2 绘制饼图。但是我收到如下错误消息。

unique.default(x, nmax = nmax) 中的错误: unique() 仅适用于向量

以下是我使用的代码。有人可以告知导致此错误的原因

freqtable <- table(worms$Vegetation)
df <- as.data.frame.table(freqtable)
colnames(df)<-c("Vegetation","Frequency")
pie<- ggplot(df, aes(x = "", fill = factor(class))) +
geom_bar(width = 1) +  theme(axis.line = element_blank(),plot.title = element_text(hjust=0.5)) +
labs(fill="class",x=NULL, y=NULL, title="Pie Chart of Vegetation", caption="Source: Worms")
pie+coord_polar(theta = "y", start=0)

感谢和问候, 拉希姆

【问题讨论】:

    标签: r ggplot2 polar-coordinates


    【解决方案1】:

    假设你的数据框是这样的:

    worms = data.frame(Vegetation=sample(letters[1:4],100,replace=TRUE))
    
    as.data.frame.table(table(worms$Vegetation))
      Var1 Freq
    1    a   22
    2    b   30
    3    c   21
    4    d   27
    

    您不需要制表,只需将类别作为参数提供给fill =

    ggplot(worms, aes(x = "", fill = Vegetation)) +
    geom_bar(width = 1) +  
    theme(axis.line = element_blank(),plot.title = element_text(hjust=0.5)) + 
    coord_polar(theta = "y", start=0) +
    labs(fill="class",x=NULL, y=NULL, title="Pie Chart of Vegetation", caption="Source: Worms")
    

    【讨论】:

    • 成功了!非常感谢您的支持。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多