简体   繁体   English

ggplot2 使用 coord_polar()_error 观察到

[英]ggplot2 using the coord_polar()_error observed

I'm trying draw a pie chart with ggplot2, using coord_polar.我正在尝试使用 coord_polar 用 ggplot2 绘制饼图。 However I'm getting an error message as below.但是,我收到如下错误消息。

Error in unique.default(x, nmax = nmax) : unique() applies only to vectors unique.default(x, nmax = nmax) 中的错误:unique() 仅适用于向量

Below is the code which I was using.Can someone please advise what's causing this error下面是我使用的代码。有人可以告诉我是什么导致了这个错误

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)

Thanks and regards, Rahim谢谢和问候,拉希姆

Say your data frame is like this:假设你的数据框是这样的:

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

You don't need to tabulate, just provide the category as argument to fill = :您不需要制表,只需提供类别作为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")

在此处输入图片说明

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

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