How can I create a pie chart of a single column in R language? Tried searching on google for it but I cant find any thing related to a single column.
CodePudding user response:
Suppose your data frame looks like this:
df <- data.frame(single_column = c(21, 48, 15, 16))
df
#> single_column
#> 1 21
#> 2 48
#> 3 15
#> 4 16
Then you can do:
library(ggplot2)
ggplot(df, aes(x = 1, y = single_column, fill = factor(single_column)))
geom_col()
geom_text(position = position_stack(vjust = 0.5),
aes(label = single_column))
coord_polar(theta = "y")
theme_void()
theme(legend.position = "none")
CodePudding user response:
This comes from Quick-R's website:
# Pie Chart with Percentages
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie(slices,labels = lbls, col=rainbow(length(lbls)),
main="Pie Chart of Countries")
Hope it works for your needs.