1

I am trying to create violin plots with boxplots within each violin plot. Currently, the boxplots are being created based on the x variable while combining the fill grouping. I would for there to be a boxplot for each of the fill groups at each of the x variables.

Thanks for the help!

ggplot(aes(y = HGT.Diff,
           x = `Platform`,
           fill = Metric,
           color = Metric),
       data = dta_compile) +
  geom_violin(draw_quantiles = c(0.5)) +
  geom_boxplot(width = 0.1, fill = "grey", color = "black") +
  ylim(0,1) +
  labs(title = "Comparing Ground Filters",
       x = "Flight Date", 
       y = "Absolute Difference to Manual HGT Measurmenets (m)") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=.4)) +
  facet_grid(`Flight.Date` ~ `GroundFilter`)

Current Violin Plot of above script

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. That will make it easier to help you. – MrFlick Sep 27 '18 at 16:31
  • Try removing `fill = "grey"` from your `geom_violin`line. You may also need to tweak the dodge width in either `geom_boxplot` or `geom_violin` to align the overlap between the two layers. – Z.Lin Sep 27 '18 at 16:36
  • @Z.Lin is there a way to find out what the dodge setting of the violins are and set the boxplots to the same setting? – Steven Anderson Sep 27 '18 at 16:56

1 Answers1

4

Got the desired outcome by including dodge setting to the violins and boxplots. Thank you for the help!

ggplot(aes(y = HGT.Diff,
       x = `Platform`,
       fill = Metric,color=Metric),
       data = dta_compile) +
geom_violin(position=position_dodge(),draw_quantiles=c(0.5)) +
geom_boxplot(width=0.1,color="black",position = position_dodge(width =0.9))+
ylim(0,1) +
labs(title="Comparing Ground Filters",
     x="Flight Date", 
     y="Absolute Difference to Manual HGT Measurmenets (m)") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=.4)) +
facet_grid(`Flight.Date`~`GroundFilter`)

Revised Violin Plot

steveb
  • 5,382
  • 2
  • 27
  • 36