我使用 ggplot 和 stat_summary 制作了一個帶有對數刻度 y 軸的條形圖。當我使用 stat_summary 的文本選項添加平均值時,它會計算平均值的對數。當我使用 exp() 進行轉換時,它會將所有 0 值轉換為 1。如果不計算單獨表中的平均值,有沒有辦法讓 stat_summary 插入真正的平均值?
這是資料的一個小樣本:
data <- structure(list(grp = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 2L, 2L, 5L, 5L, 5L, 5L, 2L, 2L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("CSF",
"PC", "NC", "GPC", "standard", "GNC", "QNC"), class = "factor"),
conc = c(`12` = 1.19059894121078, `24` = 0.454058692317884,
`36` = 0.0413144216376913, `48` = 0.0507049983112112, `59` = 39.1536357996621,
`60` = 0.00441419111181165, `71` = 41.4765704973005, `72` = 0.00496996589355793,
`83` = 3.76511709296646, `95` = 6.42853693807243, `7` = 0,
`8` = 0, `12` = 0.493144754137073, `24` = 0.502322800043954,
`36` = 0.0435760888522108, `48` = 0.0490446109926358, `55` = 1.06095474567196,
`56` = 1.09402053731166, `59` = 49.2350172116792, `60` = 0.00521072932352904,
`71` = 52.0902986576751, `72` = 0.00541517396336663, `83` = 4.81558360939545,
`95` = 5.29312800593463, `12` = 0.769326373641471, `24` = 0.619179395297563,
`36` = 0.38693332536866, `48` = 0.398836652989904, `59` = 0.312909059784019,
`60` = 0.375482647786516, `71` = 0.281116313862495, `72` = 0.366649808288524,
`83` = 0.719520752952596, `95` = 1.52458935147364), mda_label = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("none", "mda_20"), class = "factor")), row.names = c(NA,
-34L), class = c("tbl_df", "tbl", "data.frame"))
使用 stat_summary geom="text" 繪圖會產生對數(平均值):
ggplot(data, aes(x=grp, y=conc, colour=mda_label, fill=mda_label))
stat_summary(fun = mean, geom = "bar", position = position_dodge())
stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5,
position = position_dodge(width=0.9))
stat_summary(aes(label=round(..y..,2)), fun=mean, geom="text", vjust = -0.5,
position = position_dodge(width=0.9))
geom_point(position = position_dodge(width=0.9), pch=21, colour="black")
scale_y_continuous(trans='pseudo_log',
labels = scales::number_format(accuracy=0.01),
expand = expansion(mult = c(0, 0.1)))
theme(axis.text.x = element_text(angle = 45, hjust = 1))

我可以使用 exp() 將其轉換,但這也會將所有 0 值更改為 1。
ggplot(data, aes(x=grp, y=conc, colour=mda_label, fill=mda_label))
stat_summary(fun = mean, geom = "bar", position = position_dodge())
stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5,
position = position_dodge(width=0.9))
stat_summary(aes(label=round(exp(..y..),2)), fun=mean, geom="text", vjust = -0.5,
position = position_dodge(width=0.9))
geom_point(position = position_dodge(width=0.9), pch=21, colour="black")
scale_y_continuous(trans='pseudo_log',
labels = scales::number_format(accuracy=0.01),
expand = expansion(mult = c(0, 0.1)))
theme(axis.text.x = element_text(angle = 45, hjust = 1))

有辦法嗎??
uj5u.com熱心網友回復:
您可以使用ifelse:
ggplot(data, aes(x=grp, y=conc, colour=mda_label, fill=mda_label))
stat_summary(fun = mean, geom = "bar", position = position_dodge())
stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5,
position = position_dodge(width=0.9))
stat_summary(aes(label = ifelse(..y.. == 0, 0, round(exp(..y..),2))),
fun=mean, geom="text", vjust = -0.5,
position = position_dodge(width=0.9))
geom_point(position = position_dodge(width=0.9), pch=21, colour="black")
scale_y_continuous(trans='pseudo_log',
labels = scales::number_format(accuracy=0.01),
expand = expansion(mult = c(0, 0.1)))
theme(axis.text.x = element_text(angle = 45, hjust = 1))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/436087.html
下一篇:如何使ggplot中的顏色更詳細
