I have a data frame like below
+--------+-----------+-----+
| make | model | cnt |
+--------+-----------+-----+
| toyota | camry | 10 |
| toyota | corolla | 4 |
| honda | city | 8 |
| honda | accord | 13 |
| jeep | compass | 3 |
| jeep | wrangler | 5 |
| jeep | renegade | 1 |
| accura | x1 | 2 |
| accura | x3 | 1 |
+--------+-----------+-----+
I need to create a pie ( yes really) of the percentage share for each make.
I do the following as of now.
library(ggplot2)
library(dplyr)
df <- data.frame(Make=c('toyota','toyota','honda','honda','jeep','jeep','jeep','accura','accura'),
Model=c('camry','corolla','city','accord','compass', 'wrangler','renegade','x1', 'x3'),
Cnt=c(10, 4, 8, 13, 3, 5, 1, 2, 1))
dfc <- df %>%
group_by(Make) %>%
summarise(volume = sum(Cnt)) %>%
mutate(share=volume/sum(volume)*100.0) %>%
arrange(desc(volume))
bp <- ggplot(dfc[c(1:10),], aes(x="", y= share, fill=Make)) +
geom_bar(width = 1, stat = "identity")
pie <- bp + coord_polar("y")
pie
This gives me the following pie chart which is pretty neat.
However I need to enhance this with the following things - like in the image below.
- add percentage labels
- order the pies in desc order of
share
- remove lables like 0/100, 25
- add a title
levels
offactor(share)
according to the order you like. 4. duplicated question. – Andre Elricogeom_text
to get what you want where you want. Google "piechart r" -> images -> click on the image of a piechart you like. There is probably code you can use. – Andre Elrico