Reputation: 21
I have data from Male(1) and Female(2) with responses on Likert Scale. I am trying to calculate the percentage for each one of them (ex. Male with response 1 out of all 226 respondents). Thus, the percentage would need to represent % out of all 226, so that I can see the percentage of each respondent.
The barplots are quite pretty, but I cannot turn the y scale into percentage (it is currently set on values).
This is my input:
barplot(table(Short$Gen,Short$optq18), beside=T, cex.names=0.7,
legend.text=c("Male", "Female"), args.legend=list(x=3.5,y=60,cex=0.8),
col=c("bisque1", "cyan4"),
xlab = "It is important to control monthly expenses",
ylab = "Percentage",
)
text(1.4,5, "0,44%", cex=0.6)
text(2.4,4, "0%", cex=0.6)
text(4.6,8.5, "2,21%", cex=0.6)
text(5.6,7, "1,76%", cex=0.6)
text(7.5,18, "6,63%", cex=0.6)
text(8.5,26.5, "10,17%", cex=0.6)
text(10.6,37, "15%", cex=0.6)
text(11.6,47.5, "19,4%", cex=0.6)
text(13.5,41, "16,8%", cex=0.6)
text(14.6,63, "17,43%", cex=0.6)
And the barplots:
And a sample from the data:
where Country has 3 levels, Gender has 2, optq has 5 (1,2,3,4,5) Likert scale
Upvotes: 1
Views: 1438
Reputation: 16121
# example data
dt = data.frame(Gen = c(1,1,1,1,1,2,2,2,2,2),
optq18 = c(1,2,2,2,3,2,1,1,1,3))
library(tidyverse)
dt %>%
group_by(optq18) %>% # for each question number
count(Gen) %>% # count gender type
mutate(Prc = round(n/sum(n),2), # get percentage of gender up to 2 decimal points
Gen = ifelse(Gen == 1, "Male", "Female")) %>% # update variable
ungroup() %>% # forget the grouping
mutate(Prc_text = paste0(Prc*100, "%")) %>% # update how percentages appear
ggplot(aes(factor(optq18), Prc, fill=Gen))+ # plot
geom_bar(position = "dodge", stat = "identity")+ # add bars
geom_text(aes(label=Prc_text), position = position_dodge(width=1), size=8)+ # add percentages on top of bars
xlab("")+ # no x axis title
ylab("Percentage")+ # y axis title
labs(caption = "It is important to control monthly expenses")+ # add your comment
theme(plot.caption = element_text(hjust = 0.5))+ # put comment in the middle
scale_fill_manual(values=c("Female"="green", "Male"="orange")) # pick your colours
Upvotes: 1