[R] combining geom_boxplot and geom_point with jitter

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sat Sep 4 18:22:14 CEST 2021


Hello,

The problem is that you have two grouping aesthetics, color and shape.
In geom_point make the group explicit:


p <- ggplot(my_data, aes(x = Diet, y = value, color = Software))
p <- p + geom_boxplot(outlier.shape = NA)

p + geom_point(
   mapping = aes(shape = NMP_cat, group = Software),
   position = position_jitterdodge()
)


Hope this helps,

Rui Barradas

Às 09:54 de 02/09/21, Ivan Calandra escreveu:
> Dear useRs,
> 
> I'm having a problem to combine geom_boxplot and geom_point with jitter. 
> It is difficult to explain but the code and result should make it clear 
> (the example dataset is long so I copy it at the end of the email):
> 
> p <- ggplot(my_data, aes(x = Diet, y = value, color = Software))
> p <- p + geom_boxplot(outlier.shape = NA)
> p <- p + geom_point(mapping = aes(shape = NMP_cat), position = 
> position_jitterdodge())
> print(p)
> 
> As you can see in the resulting plot, the points with different shapes 
> are dodged across the boxplot categories (colors). I'd like the three 
> shapes per color to be restricted in one boxplot color, with jitter of 
> course to better visualize the points.
> 
> Does that make sense?
> 
> I have played with the arguments of position_jitterdodge(), but it seems 
> to me that the problem is that the shape aesthetic is not in the 
> geom_boxplot() call (but I don't want it there, see below).
> 
> For background information, the column used for shape gives some sort of 
> "quality" to the points; that's why I want to show the points 
> differently, so that it can easily be seen whether "good" points plot in 
> the same area as the "bad" points.
> Because I'm doing facet plots with other variables, I do not want to 
> separate these categories in the boxplots - the resulting plots would be 
> overcrowded.
> 
> Thank you for the help.
> Ivan
> 
> ---
> 
> my_data <- structure(list(Diet = c("Dry lucerne", "Dry lucerne", "Dry 
> lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", 
> "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry 
> lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", 
> "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry 
> lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", 
> "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry 
> lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", 
> "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne",
> "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry 
> lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", "Dry lucerne", 
> "Dry lucerne", "Dry grass", "Dry grass", "Dry grass", "Dry grass", "Dry 
> grass", "Dry grass", "Dry grass", "Dry grass", "Dry grass", "Dry grass", 
> "Dry grass", "Dry grass", "Dry grass", "Dry grass", "Dry grass", "Dry 
> grass", "Dry grass", "Dry grass", "Dry grass", "Dry grass", "Dry grass", 
> "Dry grass", "Dry grass", "Dry grass", "Dry grass", "Dry grass", "Dry 
> grass", "Dry grass", "Dry grass", "Dry grass", "Dry grass", "Dry grass", 
> "Dry grass", "Dry grass", "Dry grass", "Dry grass", "Dry grass", "Dry 
> grass", "Dry grass", "Dry grass", "Dry grass", "Dry grass", "Dry grass", 
> "Dry grass", "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", 
> "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", 
> "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", 
> "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", 
> "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", 
> "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", 
> "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", 
> "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", 
> "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", "Dry bamboo", 
> "Dry bamboo", "Dry bamboo",
> "Dry bamboo", "Dry bamboo"), Software = c("ConfoMap", "Toothfrax", 
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", 
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax",
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", 
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", 
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", 
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", 
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", 
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", 
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", 
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", 
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax",
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", 
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", 
> "ConfoMap", "Toothfrax", "ConfoMap", "Toothfrax", "ConfoMap", 
> "Toothfrax", "ConfoMap", "Toothfrax"), NMP_cat = structure(c(1L, 1L, 1L, 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
> 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
> 3L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 
> 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
> 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
> 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c("0-5%", "5-10%", 
> "10-20%", "20-100%"), class = c("ordered", "factor")), name = 
> structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
> .Label = c("Asfc", "Smfc", "HAsfc9", "HAsfc81", "epLsar", "NewEplsar"), 
> class = "factor"), value = c(16.00716636, 12.925787, 14.05932485, 
> 11.999816, 15.12321532, 12.711474, 12.79565826, 10.900949, 15.90481161, 
> 12.836045, 16.22778102, 13.565995, 14.71354945, 12.384152, 16.61354777, 
> 13.714165, 15.91399496, 12.983796, 19.44739619, 15.173215, 16.13761798, 
> 12.932798, 14.7332952, 12.10277, 10.78710961, 8.762726, 10.16027362, 
> 8.040399, 14.53444662, 11.527896, 17.38120685, 13.78922, 11.26840546, 
> 9.426558, 24.01797992, 18.398553, 13.7435699, 11.44385, 14.391873, 
> 10.757141, 22.39390393, 18.176262, 11.60322022, 9.969118, 11.6099975, 
> 10.059618, 11.86282935, 10.280864, 16.22473644, 13.562839, 12.46350165, 
> 10.629406, 23.9347534, 19.062174, 19.58121507, 15.910959, 13.99145447, 
> 11.352648, 14.38942328, 11.821431, 23.4733371, 18.549503, 13.08142223, 
> 10.735494, 17.09293046, 13.012834, 28.80020878, 22.447105, 25.74460885, 
> 19.76834, 14.29106582, 12.233774, 12.03005024, 10.364224, 12.58953574, 
> 10.30257, 18.07111578, 14.416143, 20.85562751, 16.524047, 21.06132234, 
> 15.744758, 15.24052683, 11.891487, 11.62446752, 9.14325, 11.75704705, 
> 10.358542, 13.65568703, 11.766129, 16.98137759, 12.594787, 11.6560954, 
> 10.32073, 15.46708251, 13.199232, 13.20110131, 11.060226, 16.13986173, 
> 13.564802, 25.45656859, 20.071231, 24.84006178, 19.335892, 14.4723856, 
> 11.994841, 12.07940958, 9.470493, 13.93630412, 11.489685, 21.84464295, 
> 17.806018, 17.4383111, 14.478338, 20.55074297, 16.254467, 30.15238714, 
> 24.193768, 32.8541897, 25.769585, 32.06966759, 24.507185, 20.53975772, 
> 15.951186, 11.54494952, 9.676342, 13.56490524, 11.456356, 13.58242208, 
> 10.919419, 13.55260161, 11.356056, 38.58113502, 31.087536, 23.6753536, 
> 18.749955, 26.38707155, 20.877856, 26.18252748, 20.758242)), row.names = 
> c(NA, -140L), class = c("tbl_df", "tbl", "data.frame"))
>



More information about the R-help mailing list