在箱线图中的 y 轴上添加中断
Posted
技术标签:
【中文标题】在箱线图中的 y 轴上添加中断【英文标题】:Adding breaks on y-axis in box-plot 【发布时间】:2021-11-23 22:27:59 【问题描述】:如何在 y 轴上添加中断(隐藏 4000 到 6000)以更好地显示其他箱形图的可变性?
有没有办法在 y 轴上也添加一个 sing 来表示间隙?
ggplot(df, aes(x=reorder(class, percent), y=percent, fill=class)) +
geom_boxplot()
这是数据:
df <-
structure(list(Sector = c("coal", "crops", "electricity", "energy intensive industries",
"forestry", "livestock", "oil and gas", "refined oil", "transportation",
"coal", "crops", "electricity", "energy intensive industries",
"forestry", "livestock", "oil and gas", "refined oil", "transportation",
"coal", "crops", "electricity", "energy intensive industries",
"forestry", "livestock", "oil and gas", "refined oil", "transportation",
"coal", "crops", "electricity", "energy intensive industries",
"forestry", "livestock", "oil and gas", "refined oil", "transportation",
"coal", "crops", "electricity", "energy intensive industries",
"forestry", "livestock", "oil and gas", "refined oil", "transportation",
"coal", "crops", "electricity", "energy intensive industries",
"forestry", "livestock", "oil and gas", "refined oil", "transportation"
), percent = c(152.85, 16.53, 31.531, 113.515, 27.303, 82.995,
75.215, 147.322, -0.13, 0.576, 113.84, -1.106, 73.221, 2.333,
1979.688, 95.781, 69.708, -0.871, 96.653, 143.812, 31.335, 80.239,
61.854, 97.244, 243.102, 448.092, -0.05, 96.653, 143.812, 31.386,
68.289, 61.854, 97.244, 2020.017, 322.76, -40.72, 1118.54, 484.989,
58.757, 1203.812, 0.001, 544.68, 3545.212, 6912.517, 0.731, 1449.567,
143.812, 1.086, 495.693, 239.69, 97.244, 456.202, 79.635, -0.083
), class = structure(c(6L, 7L, 2L, 4L, 3L, 5L, 9L, 8L, 1L, 6L,
7L, 2L, 4L, 3L, 5L, 9L, 8L, 1L, 6L, 7L, 2L, 4L, 3L, 5L, 9L, 8L,
1L, 6L, 7L, 2L, 4L, 3L, 5L, 9L, 8L, 1L, 6L, 7L, 2L, 4L, 3L, 5L,
9L, 8L, 1L, 6L, 7L, 2L, 4L, 3L, 5L, 9L, 8L, 1L), .Label = c("transportation",
"electricity", "forestry", "energy intensive industries", "livestock",
"coal", "crops", "refined oil", "oil and gas"), class = "factor")), row.names = c(NA,
-54L), class = c("tbl_df", "tbl", "data.frame"))
【问题讨论】:
任何表明在 y 轴上的步骤会有跳跃的东西。 【参考方案1】:您有多种变体可供选择。第一个选项是限制 Y 轴的范围。你失去的是你不会看到任何异常值,所以损失很小。
df %>% ggplot(aes(x=reorder(class, percent), y=percent, fill=class)) +
geom_boxplot()+
ylim(0,4000)
第二个选项是缩放 Y 轴,例如与log10
。虽然我同意,但在预缩放轴上阅读此类箱线图会有点困难。
df %>% ggplot(aes(x=reorder(class, percent), y=percent, fill=class)) +
geom_boxplot()+
scale_y_continuous(trans = 'log10')+
annotation_logticks(sides="l")
最后一个选项是创建自己的缩放函数。我创建了一个超过 2000 的函数。
library(scales)
fspec = function(x) ifelse(x<2000, x, 2000+(x-2000)/10)
fspec_1 = function(x) ifelse(x<2000, x, 2000+(x-2000)*10)
specTrans = trans_new(name = "specialTras",
transform = fspec,
inverse = fspec_1,
breaks = c(0, 1000, 2000, 3000, 4000, 5000, 6000))
df %>% ggplot(aes(x=reorder(class, percent), y=percent, fill=class)) +
geom_boxplot()+
coord_trans(y = specTrans)
【讨论】:
以上是关于在箱线图中的 y 轴上添加中断的主要内容,如果未能解决你的问题,请参考以下文章