0

I have the following dummy data frame:

structure(list(CIP = c(8, 16, 8, 0.25, 0.06, 0.5, 0.12, 0.06, 
8, 0.25, 16, 0.12, 0.12, 0.25, 0.25, 0.5, 0.25, 0.25, 0.25, 0.25, 
0.12, 0.5, 0.5, 0.25, 8, 0.25, 16, 0.25, 0.25, 0.12, 0.5, 0.25, 
0.25, 0.25, 0.5, 0.25, 16, 0.5, 16, 0.25, 0.25, 1, 0.12, 0.25, 
0.12, 0.12, 0.12, 0.25, 0.25, 16), group = c(18L, 22L, 16L, 18L, 
14L, 10L, 11L, 18L, 18L, 18L, 18L, 18L, 23L, 23L, 20L, 20L, 23L, 
11L, 18L, 11L, 11L, 7L, 14L, 18L, 18L, 15L, 21L, 8L, 23L, 12L, 
1L, 23L, 12L, 22L, 18L, 18L, 17L, 20L, 18L, 16L, 14L, 14L, 14L, 
22L, 22L, 18L, 23L, 18L, 18L, 18L), gene = c("parC", "marR", 
"parC", "parC", "parE", "parC", "gyrA", "marR", "parE", "parC", 
"gyrA", "marR", "gyrA", "gyrB", "marR", "gyrA", "parC", "gyrA", 
"parE", "gyrA", "gyrA", "marR", "marR", "parE", "parC", "parC", 
"parC", "gyrB", "marR", "marR", "none", "parC", "marR", "parC", 
"gyrA", "marR", "gyrA", "marR", "marR", "gyrA", "gyrA", "gyrA", 
"marR", "parC", "marR", "marR", "parE", "marR", "gyrA", "parC"
), value = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, -50L
), class = c("tbl_df", "tbl", "data.frame"))

I want to create a violin plot that doesn't trim the edges, but still is within the given range of the data. With trim = TRUE, the edges of the violin plot goes outside the min and max values 0.015 and 16, see below.

Plot:

library(ggplot2)

factor_breaks <- c(0.015, 0.03, 0.06, 0.12, 0.25, 0.5, 1, 2, 4, 8, 16)

factor_levels <- c("0.015", "0.03", "0.06", "0.12", "0.25", "0.5", "1", "2", "4", 
                   "8", "16")

ggplot(test_df, aes(factor(group), CIP, fill = factor(group)))+
  geom_violin(trim = FALSE)+
  scale_y_continuous(trans = "log10",
                     labels = factor_levels,
                     breaks = factor_breaks)+
  guides(fill = FALSE)+
  theme_light()+
  theme(axis.title = element_blank(),
        axis.text.y = element_text(size = 8),
        axis.ticks.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.y = element_blank())

enter image description here

I have seen the question and answer posted here, but I cannot use this solution due to the already specified y-axis settings in scale_y_continuous. Due to the data not being truly numerical (categorical measurements), I have to log transform it for the violin plot to work properly. When I then try to use the ylim() function it will simply override the settings in scale_y_continuous.

Is there any way to do this without using trim = TRUE?

Haakonkas
  • 961
  • 9
  • 26
  • Would adding `coord_cartesian(ylim = your.desired.range)` to your plot work? – Z.Lin Sep 07 '18 at 11:39
  • @Z.Lin Sort of, the only thing is that cuts off the tops of the violin plots, so it doesn't look very neat... – Haakonkas Sep 07 '18 at 11:43
  • So what would your ideal chart look like? Also note that while your example is largely reproducible, `factor_breaks` and `factor_levels` are not defined. – Z.Lin Sep 08 '18 at 04:54
  • @Z.Lin Sorry about that, the questiion has been updated. The ideal plot would be that geom_violin doesn't stretch each plot so far out of the data range - it makes it look a lot bigger than it really is, but also without creating the stunted edges with trim = TRUE – Haakonkas Sep 08 '18 at 13:36

0 Answers0