我需要一個刻面的boxplot。圖的x軸是一個定量變量,我想在該圖上反映這些信息。橫座標的尺度在各個方面之間是非常不同的。ggplot2:與不同比例尺的刻面拼接時出現錯誤的boxplot寬度
我的問題是,對於大規模的方面,盒子的寬度非常小。
一個可能的解釋是盒子的寬度對於所有facet是相同的,而理想情況下應該由每個facet的xlims單獨確定。
我將不勝感激,兩個輸入:
- 你認爲這是一個錯誤,應該報告?
- 你有解決方案嗎?
在此先感謝!備註:將橫座標轉換爲分類變量可能是一種解決方案,但它並不完美,因爲它會導致某些信息的丟失。
最小工作例如:
library(tidyverse)
c(1:4,7) %>%
c(.,10*.) %>% # Create abscissa on two different scales
lapply(FUN = function(x) {tibble(x = x, y = rnorm(50), idx = ifelse(test = x<8, yes = 'A', no = 'B'))}) %>% # Create sample (y) and label (idx)
bind_rows() %>%
ggplot(aes(x = x, y = y, group = x)) +
geom_boxplot() +
facet_wrap(~idx, scales = 'free')
結果:
繁瑣的解決辦法是重新繪製從頭箱線圖,但這不是很滿意:
draw_boxplot = function(locations, width, ymin, lower, middle, upper, ymax, idx){
local_df = tibble(locations = locations, width = width, ymin = ymin, lower = lower, middle = middle, upper = upper, ymax = ymax, idx = idx)
ggplot(data = local_df) +
geom_rect(aes(xmin = locations - width/2, xmax = locations + width/2, ymin = lower, ymax = upper), fill = 'white', colour = 'black') +
geom_segment(aes(x = locations - width/2, xend = locations + width/2, y = middle, yend = middle), size = 0.8) +
geom_segment(aes(x = locations, xend = locations, y = upper, yend = ymax)) +
geom_segment(aes(x = locations, xend = locations, y = lower, yend = ymin)) +
facet_wrap(~idx, scales = 'free_x')
}
make_boxplot = function(to_plot){
to_plot %>%
cmp_boxplot %>%
(function(x){
draw_boxplot(locations = x$x, width = x$width, ymin = x$y0, lower = x$y25, middle = x$y50, upper = x$y75, ymax = x$y100, idx = x$idx)
})
}
cmp_boxplot = function(to_plot){
to_plot %>%
group_by(idx) %>%
mutate(width = 0.6*(max(x) - min(x))/length(unique(x))) %>% #hand specified width
group_by(x) %>%
mutate(y0 = min(y),
y25 = quantile(y, 0.25),
y50 = median(y),
y75 = quantile(y, 0.75),
y100 = max(y)) %>%
select(-y) %>%
unique()
}
c(1:4,7) %>%
c(.,10*.) %>%
lapply(FUN = function(x) {tibble(x = x, y = rnorm(50), idx = ifelse(test = x<8, yes = 'A', no = 'B'))}) %>%
bind_rows() %>%
make_boxplot
結果:
一般箱形圖是/應該用於分類變量而不是數值... –
嘗試:'ggplot(aes(x = as.factor(x),y = y))' – missuse
感謝missuse,但我認爲您的建議等於:「將橫座標轉換爲分類變量可能是一個解決方案,但它並不完美,因爲它會導致一些信息的丟失。「 – konkam