0

I am plotting boxplots using a dataset, and the plot looks like this. I would like to add a specific point aligned with each boxplot for each value of the x-axis. For now I was able to do it using boxplots again, and here is what it looks like. As you can see the new boxplot (which has only one value, so indicated only with the median line) is aligned with the previous boxplot ofr each method. I'd like to have something similar but using points (e.g in star shape) instead of boxplots for the second set of data points, here is a picture of what I would like to have.

However, when I try to merge the geom_boxplot and the geom_point figure it doesn't work as I want: as you can see the points are not aligned below or above their corresponding boxplots. Here is the code I used to generate this figure

ggplot() +
  geom_boxplot(data = df_bound_base, aes(x=m, y=FDP_bar, color=bound)) +
  geom_point(data = interp_median, aes(x=m, y=FDP_bar, color=bound, fill=bound), shape=23, size=2) 

and here are screeshots of what the data look like dataset for boxplot, dataset for points.

What do I need to change to have the points aligned with their corresponding boxplot ?

iqm
  • 1
  • 1
  • Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. Please do not post an image of data [for these reasons](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). Just include the data directly using e.g. `dput()`. – stefan May 22 '23 at 15:21
  • 1
    The issue is that `geom_point` uses `position="identity"` by default. To align the points with dodged box plots try `position = position_dodge(width = .75)`. See e.g. https://stackoverflow.com/questions/71287108/r-ggplot-geom-points-not-aligned-with-boxplot-bins – stefan May 22 '23 at 15:26

0 Answers0