Reputation: 97
Long story short, I ran a bunch of stochastic simulations for each of 15 groups, and have one integer per group that I need to add to each violin in the plot, and can't seem to figure out how to do it. Here's a reproducible example:
# Making data
df <- data.frame(c(rep(1,10), rep(2,10), rep(3,10)), sample.int(100, 30), c(rep(85,10), rep(60,10), rep(55,10)))
colnames(df) <- c("Group", "Data", "Extra")
# Grouping data
df$Group <- as.factor(df$Group)
# Plotting
Violin2 <- ggplot(data = df, aes(x = Group, y = Data))+
geom_violin(aes(fill = Group, color = Group))+
stat_summary(aes(y = Data), fun=mean, geom="point", color = "navyblue", shape = 17, size = 3)+
stat_summary(aes(y = Data), fun=median, geom="point", color = "black", shape = 16, size = 3)
#geom_point(aes(y = Extra, color = "#00BB66", shape = 16, size = 3)+
Violin2
So here, I'm saying that within the df, there are three groups: 1, 2, and 3, that are applied to the "Data" column. What I need to add, are the integers from the "Extra" column of the df, as single points on each violin (so the three integers would be 85, 60, and 55).
I initially tried to add a geom_point layer, and thought Extra would be grouped by Group, just as Data was, but that didn't work (Error: Discrete value supplied to continuous scale).
I've been searching around on here a lot, and can't find a solution, so any advice would be greatly appreciated! Thanks so much in advance for any help! :)
This is the data:
Upvotes: 1
Views: 557
Reputation: 907
So it's actually just one more line of code - you can stitch different geom's together in ggplot and it makes it really easy to do exactly what you're talking about. Just add
geom_point(aes(y = Data)) +
So the whole code would look like this
ggplot(data = df, aes(x = Group, y = Data))+
geom_violin(aes(fill = Group, color = Group))+
geom_point(aes(y = Extra), size = 2, colour = "red") +
stat_summary(aes(y = Data), fun=mean, geom="point",
color = "navyblue", shape = 17, size = 3)+
stat_summary(aes(y = Data), fun=median, geom="point",
color = "black", shape = 16, size = 3)
I've coloured the points red and made them bigger but you can change that. That gives:
Upvotes: 1
Reputation: 4184
Your example is working perfectly. The only thing to update is to not use constant value for color arg inside aes. You could use it like that only outside the aes.
# Making data
library(ggplot2)
df <- data.frame(c(rep(1,10), rep(2,10), rep(3,10)), sample.int(100, 10), c(rep(85,10), rep(60,10), rep(55,10)))
colnames(df) <- c("Group", "Data", "Extra")
# Grouping data
df$Group <- as.factor(df$Group)
# Plotting
Violin2 <- ggplot(data = df, aes(x = Group, y = Data))+
geom_violin(aes(fill = Group, color = Group))+
stat_summary(aes(y = Data), fun=mean, geom="point", color = "navyblue", shape = 17, size = 3)+
stat_summary(aes(y = Data), fun=median, geom="point", color = "black", shape = 16, size = 3) +
geom_point(aes(y = Extra))
Violin2
Created on 2021-06-08 by the reprex package (v2.0.0)
Upvotes: 1