使用已经为geom_violin指定的scale_y_continous调整y轴范围

时间:2018-09-07 09:42:35

标签: r ggplot2

我有以下虚拟数据帧:

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"))

我想创建一个不修剪边缘但仍在给定数据范围内的小提琴图。如果trim = TRUE,则小提琴图的边缘将超出最小值和最大值0.015和16,请参见下文。

图:

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

我已经看到问题和答案发布在here上,但是由于已经在scale_y_continuous中指定了y轴设置,因此无法使用此解决方案。由于数据不是真正的数字(分类测量),因此我必须对数据进行对数转换,以使小提琴图正常工作。然后,当我尝试使用ylim()函数时,它将仅覆盖scale_y_continuous中的设置。

是否有任何方法可以不使用trim = TRUE?

0 个答案:

没有答案