Reputation: 1
I'm trying draw a pie chart with ggplot2, using coord_polar. However I'm getting an error message as below.
Error in unique.default(x, nmax = nmax) : unique() applies only to vectors
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
Upvotes: 0
Views: 443
Reputation: 46968
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 =
:
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")
Upvotes: 0