簡體   English   中英

Plot 在ggplot的對數軸上為負

[英]Plot negative on logarithmic axis in ggplot

我遵循了這個示例,它對我的數據非常有用( https://r-graphics.org/recipe-axes-axis-log )。 除了我還嘗試添加標准偏差,對於其中一個變量,標准偏差大於平均值。 該圖只繪制了頂部誤差條,而不是底部。 如何為示例 5 添加錯誤欄?

 Sample <- c(1, 2, 3, 4, 5)
 Fe <- c(418, 592, 228, 351, 23880)
 sd <- c(269, 538, 187, 236, 36577)

 df <- data.frame(Sample, Fe, sd)
 df

 (plot <- ggplot(df, aes(x = Sample, y = Fe, ymin = Fe - sd, ymax = Fe + sd)) + theme_bw() +
  geom_point(size=3, stat = "identity") + 
 geom_errorbar(aes(width = 0.1), stat = "identity") +
 scale_y_continuous(trans = log10_trans(),
                   breaks = trans_breaks("log10", function(x) 10^x),
                   labels = trans_format("log10", math_format(10^.x))) +
  annotation_logticks(sides = "l"))

你可以pmax(0, .)它:

library(ggplot2)
library(scales)

ggplot(df, aes(x = Sample, y = Fe, ymin = pmax(0, Fe - sd), ymax = Fe + sd)) +
  theme_bw() +
  geom_point(size=3, stat = "identity") + 
  geom_errorbar(aes(width = 0.1), stat = "identity") +
  scale_y_continuous(trans = log10_trans(),
                     breaks = trans_breaks("log10", function(x) 10^x),
                     labels = trans_format("log10", math_format(10^.x))) +
  annotation_logticks(sides = "l")

ggplot,最后一個誤差線達到或低於 0

或者您可以使用較小的數字並查看該錯誤欄的下端,盡管這可能表明當前值不是...

ggplot(df, aes(x = Sample, y = Fe, ymin = pmax(1, Fe - sd), ymax = Fe + sd)) +
  theme_bw() +
  geom_point(size=3, stat = "identity") + 
  geom_errorbar(aes(width = 0.1), stat = "identity") +
  scale_y_continuous(trans = log10_trans(),
                     breaks = trans_breaks("log10", function(x) 10^x),
                     labels = trans_format("log10", math_format(10^.x))) +
  annotation_logticks(sides = "l")

ggplot,最后一個誤差線在水平 0 軸上方停止

當 y 軸試圖調整時,使用更小的值將繼續壓縮其他誤差。

暫無
暫無

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

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