I have a question regarding pie charts in R. I want to have a pie chart for used operating systems. However, there are many systems which have a share of under 1% and I want to exclude them from the diagram. Of course I could delete them from the dataframe before plotting, but I was wondering if there is a better alternative within the ggplot functions to plot only the top three operating systems.
In the following the dataframe as input and the code I am using:
Dataframe:
operatingSystem | sessionsPercent
Android | 0.620
iOS | 0.360
Windows | 0.010
Blackberry | 0.001
...
Code:
p <- ggplot(df, aes(x="", y=sessions, fill = operatingSystem))
p + geom_bar(width = 1, stat = "identity") +
geom_text(aes(label = percent(data$sessions)), position = position_stack(vjust = 0.5), color = "white", size = 8) +
coord_polar(theta="y", direction = -1) +
theme_void()
Does anybody has an idea?
There is no need to delete these rows from your dataframe, you could just run your ggplot commands over a subset of your data. BTW, I think you mean some OS could have a share under 1%, not under 0%? Also, please do not name a data.frame "data", because it could mess up some functions from the utils package. Better to use df for example.
I think you can try this:
library(ggplot2)
library(scales)
df <- read.table(text = 'operatingSystem sessionsPercent
Android 0.620
iOS 0.360
Windows 0.010
Blackberry 0.001', header = TRUE)
p <- ggplot(df[df$sessionsPercent %in% head(df$sessionsPercent, 3),], aes(x="", y=sessionsPercent, fill = operatingSystem))
p + geom_bar(width = 1, stat = "identity") +
geom_text(aes(label = percent(head(df$sessionsPercent, 3))), position = position_stack(vjust = 0.5), color = "white", size = 8) +
coord_polar(theta="y", direction = -1) +
theme_void()
Percentages do not add up to 100% now, but if you want that, you could divide the argument inside the percent() command by 0.99 (since total percentage of 3 OS with highest percentages is 99%).