scatterplot with count numbers in ggscatter
I made a scatterplot showing correlations between 2 groups. Several participants have the same datapoints, so I got fewer dots than participants. Is there a way to put a number in the plot next to every dot indicating how many they represent?
Here is my code and plot:
ggscatter(df, x = "Change", y = "T0",
color="Group",palette = c("red", "blue"),
add = "reg.line", conf.int = TRUE,
add.params = list(color = "brown"),
cor.coef = TRUE, cor.method = "spearman",
cor.coef.coord = c(2,5),
cor.coef.size = 5,
ylim = c(0, 5),
xlab = "x", ylab = "y") +
scale_x_continuous(n.breaks=7) +
theme(legend.position = "right", legend.justification = "top", legend.title = element_blank())+
theme(axis.line=element_line(size=1))+
theme(text = element_text(size = 13))
• 1,981 views
•
link
1 answer
What about dealing with the data frame before plotting? Merge points with the same value and use a column to record their counts and then you could set colour and szie in geom_point. To obtain the fitted curve, set the parameter data to the original data frame in geom_smooth.
• 0 views
•
link
Log in to answer this question.
It appears to be quit tricky. When I add the lines: geom_count() + scale_size_area(breaks = c(1,2,3,4,5)) to the end, I get
which would be ok, but I loose the colours.
Maybe someone hs an idea how to get them back?
The reason it looks like this is because you're plotting the circles with the desired size on top of your colored dots. You can even see this at (4,3) and (2,1). See if you can add a color aesthetic within
scale_size_area, or better yet, modify your input dataframe so that it includes the number of participants represented by each dot.