0

I am not having any luck with geom_violin in ggplot today. Any help?

I have 10 years worth of running times in a local race. I want to plot the spread of only the top 10 times for each year. Ex: 2018: 14:51-17:29, 2017: 15:03-18:22 etc

Everytime I run the code, it gives me the distribution of the top 10 over the 10 years (100 data points), instead of each year broken out (10 data points each year).

Code:

violin <- 5kAthletes %>%
  filter(Category=="Top10")

(^^^that pairs it properly for me^^^

vplot<-violin %>%
  ggplot(aes(x=Year, y=Minutes, fill=Year)) +
  geom_violin() +
  xlab("Year") +
  theme(legend.position = "none") +
  xlab("")  
vplot
Phil
  • 7,287
  • 3
  • 36
  • 66

1 Answers1

0

You have to add group = Year to the aesthetics, i.e. ggplot(aes(x=Year, y=Minutes, fill=Year, group = Year)). Using the gapminder dataset as example data:

library(ggplot2)
library(dplyr)
library(gapminder)

df <- gapminder::gapminder %>% 
  group_by(year) %>% 
  top_n(10, lifeExp)

vplot <- df %>%
  ggplot(aes(x=year, y=lifeExp, fill=year, group = year)) +
  geom_violin() +
  xlab("Year") +
  theme(legend.position = "none") +
  xlab("")  
vplot

Created on 2020-04-02 by the reprex package (v0.3.0)

stefan
  • 90,330
  • 6
  • 25
  • 51