对于范围变化不定的多个图形,如何至少有2个y轴中断?

时间:2019-08-16 06:32:41

标签: r ggplot2

我正在绘制不同细胞因子水平的浓度,这些浓度在范围上有很大差异。来自scales::pretty_breaks在y轴上的结果是不可取的。有两个问题。

设置n=2时,某些图形仅显示1个刻度,而不显示2个。

y轴刻度文本未正确对齐其图形。

dput()输出:

structure(list(Group = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
19L, 19L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 
25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L), Cytokine = 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3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"T+C"), class = "factor"), 
    Replicate = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L
    ), .Label = c("R1", "R2", "R3"), class = "factor"), CC = c(10.72, 
    11.22, 9.85, 10.55, 11.19, 10.32, 4.61, 11.47, 9.98, 21.26, 
    15.79, 15.79, 38.99, 36.52, 29.03, 11.47, 15.79, 10.73, 64.62, 
    65.26, 68.5, 15.79, 18.56, 12.94, 0, 0, 0, 0, 0, 0, 1.1, 
    11.74, 4.23, 2.44, 2.89, 3.33, 232.19, 265.2, 235.07, 69, 
    62.14, 64.89, 368.75, 392.6, 369.79, 93.46, 97.5, 80.59, 
    42.01, 39.91, 42.23, 40.67, 42.61, 37.33, 103.81, 44.19, 
    103.81, 243.55, 537.84, 124.58, 2173.46, 2548.97, 2185.7, 
    963.54, 1003.78, 850.73, 3775.81, 3599.65, 3677.04, 1056.31, 
    1222.84, 1043.28, 895.51, 896.82, 889.58, 926.93, 904.52, 
    907.48, 909.06, 936.17, 905.15, 928.47, 938.09, 865.52, 6024.45, 
    6204.68, 5986.87, 5019.31, 4750.06, 5062.42, 6624.21, 7237, 
    6808.59, 5490.35, 5790.21, 5210.26, 0, 0, 0, 0, 0, 0, 44.35, 
    44.35, 43.84, 53.25, 56.1, 55.63, 2927.83, 2978.39, 3205.55, 
    1976.48, 2001.29, 2029, 4563.38, 4556.54, 4694.97, 2470.94, 
    2479.96, 2396.81, 1.82, 2.32, 1.82, 2.19, 1.46, 1.42, 2.28, 
    2.32, 2.48, 0.4, 0.68, 0.31, 141.01, 149.87, 126.84, 64.72, 
    61.14, 63.1, 191.72, 189.05, 176.85, 80.3, 82.16, 70.85), 
    RK = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 
    2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 
    1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 
    2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 
    1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 
    2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 
    1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 
    2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 
    1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 
    2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("0", 
    "6"), class = "factor")), row.names = c(NA, -144L), class = "data.frame")

图形代码:

ggplot(cytokineMain.df, aes(Condition, CC, fill=factor(RK)))+
  geom_bar(position = "dodge", stat="summary", fun.y="mean", colour="black")+

  facet_wrap(vars(Cytokine), ncol=2, scales="free_y")+

  scale_x_discrete(breaks=c("Ctrl", "T", "C", "T+C"))+
  scale_y_continuous(breaks = scales::pretty_breaks(2),expand = expand_scale(mult = c(.05, .25)))+

  scale_fill_brewer(palette="Paired")+

  theme_bw()+
  theme(strip.background = element_rect(fill="white"))+
  theme(
    plot.title = element_text(hjust = 0.5),
    panel.grid.major.x = element_blank(),
    axis.title.x = element_blank(),
    axis.text.x = element_text(),
    axis.text.y = element_text(angle = 90),
    axis.ticks = element_blank(),
    axis.title.y = element_blank(),
    legend.position = "none"
    )

Sample image

我希望所有图和y轴文本都具有更好的y轴折点,以使其与其对应的图正确对齐。

谢谢您的帮助。

0 个答案:

没有答案