nicholasflamel
nicholasflamel

Reputation: 43

Plotting the proportion of a categorial variable on the y-axis in R using ggplot with a numerical x-axis

Here's some example data:

result  age
y   20
y   20
n   20
n   20
y   21
n   21
n   21
n   21
y   22
n   22
n   22

I would like to plot the proportion of "y" as a total of "result" on the y-axis against "age" on the x-axis, displayed as a line graph and preferably also with the dot representing the proportion / frequency of y (geom_point). I don't want to display "n". Preferably using ggplot2 because I always use that.

Many thanks for any help

Upvotes: 0

Views: 793

Answers (1)

IceCreamToucan
IceCreamToucan

Reputation: 28705

You can summarize the data with dplyr and then plot the summarized data frame rather than the original data frame

library(dplyr)
library(ggplot2)

df %>% 
  group_by(age) %>% 
  summarise(p = mean(result == 'y')) %>% 
  ggplot(aes(x = age, y = p)) +
    geom_point() + 
    geom_line()

enter image description here

Upvotes: 2

Related Questions