簡體   English   中英

ggplot log scale y 軸搞亂了 stat_summary bar plot

[英]ggplot log scale y axis messes up stat_summary bar plot

我在 ggplot 中制作了一個帶有誤差線的條形圖。 我想制作 y 軸對數刻度,但它弄亂了條形圖,我不知道為什么。

這是一小部分數據樣本:

data <- structure(list(label = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L), .Label = c("CSF_raw", "CSF_supernatant", 
"CSF_pellet", "PC_raw", "PC_supernatant", "PC_pellet", "NC_raw", 
"NC_supernatant", "NC_pellet", "GPC", "GNC", "standard", "NC"
), class = "factor"), conc = c(`49` = 0, `50` = 22.137978797117, 
`51` = 0, `52` = 0.558357028761278, `53` = 35.0832980064114, 
`54` = 106.064331352197, `55` = 4.14765656994934, `56` = 1.16840050032707, 
`57` = 8.33529714053568, `58` = 0, `59` = 0, `60` = 0, `73` = 0, 
`74` = 0, `131` = 0.00015364395215365, `132` = 22.3641755644066, 
`133` = 0, `134` = 0.409025645808659, `135` = 40.8376939323448, 
`136` = 81.1807837353875, `137` = 6.38488684568018, `138` = 1.68909814271646, 
`139` = 7.61828609738757, `140` = 0, `141` = 0, `142` = 0, `155` = 0, 
`156` = 0.000140010408919781)), row.names = c(NA, -28L), class = c("tbl_df", 
"tbl", "data.frame"))

如果我使用法線軸 plot,它會很好地工作:

ggplot(data, aes(x=label, y=conc)) +
  stat_summary(fun = mean, geom = "bar") + 
  stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5) +
  geom_point() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

在此處輸入圖像描述

但是當我引入對數刻度時,它弄亂了 plot,條形浮動而不是從 0 開始!

ggplot(data, aes(x=label, y=conc)) +
  stat_summary(fun = mean, geom = "bar") + 
  stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5) +
  geom_point() +
  scale_y_continuous(trans='log2',
                     labels = scales::number_format(accuracy=0.01)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

在此處輸入圖像描述

如評論中所述,如果您使用log2進行轉換,這將是正確的 output 。 你可以使用pseudo_log

ggplot(data, aes(x=label, y=conc)) +
  stat_summary(fun = mean, geom = "bar") + 
  stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5) +
  geom_point() +
  scale_y_continuous(trans='pseudo_log',
                     labels = scales::number_format(accuracy=0.01)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM