0

I'm having issues constructing multiple density plots so that the density is calculated relative to ALL the plots instead of relative to individual plots. I am trying to calculate strikes on fighters during the course of a fight. But, the 'Total' strikes column is not visually displaying density as the greatest number of underlying points. The photo below visualizes what I am trying to do and the issue I am having. In the picture, Round 1 looks like the fighter had more strikes to the head than in the Total Round, where he actually has the most strikes to the head. However, you wouldn't know that based on the coloring, which is being calculated based on individual plots and not all the plots grouped together.

Code is below. df35 is the data frame with all the fighter data, including round, fighter, and x- and y-coordinates to plot the strikes.

d <- ggplot(df35, aes(x,y)) + 
  stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE, bins=50) + 
  annotation_raster(mma_winner, xmin=250, xmax=525, ymin=150, ymax=450) +
  xlim(250,525) + ylim(150,500) +
  facet_grid(fighter ~ round) +
  theme_classic() +
  scale_fill_distiller(palette = 'RdYlBu', limits = c(0,55e-05))

d + geom_point(data = df46, aes(x,y), color = "red", alpha = 1/2, size = 2)

Example images here: enter image description here

Image with underlying data points here: enter image description here

Any help is greatly appreciated!!

  • You should redo the levels of `round`. See their (wrong) order in the panels' titles. Set them as `df35$round <- factor(as.character(df35$round), levels = c("One", "Two", "Three", "Total"))` – Rui Barradas Jan 26 '20 at 22:18
  • Thanks. I've got it ordered correctly now. – Marissa Fahlberg Jan 27 '20 at 15:25
  • I understand now your question (I deleted my answer as it was not adressing the real problem). It seems that `stat_density_2d` isn't reflecting your data as it should. Try reading the documentation on this function here: https://ggplot2.tidyverse.org/reference/geom_density_2d.html. From there I would try `stat_density_2d(aes(fill = stat(nlevel), geom = "raster", contour = FALSE, bins=50)`. Let me know if it works to put it as an answer. – David Jorquera Jan 27 '20 at 15:38
  • This returned an error, "Error in FUN(X[[i]], ...) : object 'nlevel' not found". I did modify your code a little bit so that it said `stat_density_2d(aes(fill = stat(nlevel)), geom = "raster", contour = FALSE, bins = 50)` so that there was an extra ) after stat(nlevel) but unfortunately didn't work. I'll read the documentation again but still haven't been able to figure it out. – Marissa Fahlberg Jan 27 '20 at 16:02
  • Maybe include an interaction term: `ggplot(df35, aes(x,y, group = interaction(round, fighter)))`. – Rui Barradas Jan 27 '20 at 18:29

0 Answers0