[英]ggplot2 - Error while generating a horizontal boxplot with custom whiskers using geom_boxplot
[英]Add horizontal bars to whiskers on percentile based boxplot
我的數據和圖為:
new_df <- structure(list(Group = c("k__Fungi; p__Ascomycota; c__Eurotiomycetes; o__Chaetothyriales; f__Chaetothyriaceae; g__unidentified",
"k__Fungi; p__Ascomycota; c__Eurotiomycetes; o__Chaetothyriales; f__Chaetothyriaceae; g__unidentified",
"k__Fungi; p__Ascomycota; c__Eurotiomycetes; o__Chaetothyriales; f__Chaetothyriaceae; g__unidentified",
"k__Fungi; p__Ascomycota; c__Eurotiomycetes; o__Chaetothyriales; f__Chaetothyriaceae; g__unidentified"
), Percentile_0 = c(1, 1, 1, 1), Percentile_25 = c(1, 17.75,
8, 99.5), Percentile_50 = c(1, 48, 32, 215.5), Percentile_75 = c(3,
93, 51.25, 343.75), Percentile_100 = c(28, 337, 104, 788), Type = c("T1",
"T2", "T3", "T4")), row.names = c(NA, -4L), class = "data.frame")
#plot
ggplot(data = new_df, aes(x =Group, group = Type, fill = Type)) +
geom_boxplot(
stat = "identity",
aes(
ymin = Percentile_0,
lower = Percentile_25,
middle = Percentile_50,
upper = Percentile_75,
ymax = Percentile_100
)
) +
theme_classic()
我想在這個線程說明添加水平晶須這里 。
由於您已經計算了晶須末端的值,因此可以直接使用geom_errorbar()
,而不必像給出的鏈接中那樣通過stat_boxplot()
來使用。
您將需要顯式躲避誤差線以匹配默認的箱形圖躲避。
geom_errobar()
所需的美觀geom_errobar()
是ymin
和ymax
。 我將它們放在geom_errorbar()
層中。 由於將它們同時用於箱線圖和錯誤欄,因此可以將它們移至全局aes()
以避免重復。
ggplot(data = new_df, aes(x = Group, group = Type, fill = Type)) +
geom_errorbar(aes(ymin = Percentile_0,
ymax = Percentile_100),
width = 0.5,
position = position_dodge(width = 0.9) ) +
geom_boxplot(
stat = "identity",
aes(
ymin = Percentile_0,
lower = Percentile_25,
middle = Percentile_50,
upper = Percentile_75,
ymax = Percentile_100
)
) +
theme_classic()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.