I would like to create a bar chart in R with how many mens and how many womens vote for green topics. The percentage of women is 64% The percentage of men is 36%. I want the Title "Gender-specific vote for green topics" and the color red for female and blue for male in the pie chart. Could someone please help?
slices <- c(36,64)
lbls <- c("male", "female")
pie(slices, labels = lbls, main="gender-specific vote for green topics")
Problems with add color blue for men and red for women
CodePudding user response:
You could use ggplot
:
library(ggplot2)
ggplot(data = data.frame(x = 1, y = c(64, 36), Sex = c("Female", "Male")),
aes(x, y, fill = Sex))
geom_col()
coord_polar(theta = "y")
theme_void()
geom_text(aes(label = paste0(y, "%")), colour = "white",
position = position_stack(vjust = 0.5), size = 6)
scale_fill_manual(values = c("deepskyblue4", "tomato2"))
ggtitle("Gender-specific vote for green topics")
theme(title = element_text(size = 16, face = 2, hjust = 0.5))
CodePudding user response:
slices <- c(36,64)
lbls <- c("male", "female")
Let's see what's is behind pie()
:
?pie()
[...]
Arguments
[...]
col -- a vector of colors to be used in filling or shading the slices. If missing a set of 6 pastel colours is used, unless density is specified when par("fg") is used.
Then just entering a vector of desired colours it gives us:
pie(slices, labels = lbls, col=c("blue", "red"), main="gender-specific vote for green topics")
Created on 2022-03-23 by the reprex package (v2.0.1)