在箱形图中的 y 轴上添加中断
Adding breaks on y-axis in box-plot
如何在 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 轴的范围。你失去的是你不会看到任何异常值,所以损失很小。
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 轴上添加一个中断(隐藏 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 轴的范围。你失去的是你不会看到任何异常值,所以损失很小。
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)