scatterplot with count numbers in ggscatter
1
0
Entering edit mode
10 months ago

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))

scatterplot in R

R ggscatter • 953 views
ADD COMMENT
0
Entering edit mode

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 scatter which would be ok, but I loose the colours. Maybe someone hs an idea how to get them back?

ADD REPLY
1
Entering edit mode

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.

ADD REPLY
2
Entering edit mode
10 months ago
zau saa ▴ 150

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.

ADD COMMENT

Login before adding your answer.

Traffic: 1782 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6