'Im trying to create a line graph in R using ggplot
I am new to R and programming in general, I have a data frame similar to this but with a lot more rows:
yes_no <- c('Yes','No','No','Yes','Yes','No','No','No','Yes','Yes','No','Yes','No','Yes','No','Yes','No','Yes','No','Yes')
age <- c('1','1','2','3','4','5','1','2','2','3','1','5','5','5','1','4','4','2','5','3')
data<- data.frame(yes_no,age)
I am trying to create a line graph using ggplot where the x-axis is the age and the y axis is the percentage of yes for a specific age.
I am not too sure how to create the percentage
any advice? thank you!
Solution 1:[1]
data %>%
group_by(age, yes_no) %>%
mutate(k = n()) %>%
ungroup(yes_no) %>%
mutate(n = n(),
p = 100*k/n) %>%
unique() %>%
ungroup() %>%
complete(age, yes_no,
fill = list(k = 0, n = 0, p = 0)) %>%
ggplot(aes(x=age, y =p, group = yes_no, color = yes_no)) +
geom_line() +
ylim(c(0,100))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

