简体   繁体   English

如何将 y 轴更改为对数刻度?

[英]How to change y-axis to log-scale?

This question is related to a previous post .这个问题与之前的帖子有关

Say I have this set of data test :说我有这组数据test

   a b       c
1  a x      NA
2  b x 5.1e-03
3  c x 2.0e-01
4  d x 6.7e-05
5  e x      NA
6  f y 6.2e-05
7  g y 1.0e-02
8  h y 2.5e-03
9  i y 9.8e-02
10 j y 8.7e-04

> dput(test)
structure(list(a = structure(1:10, .Label = c("a", "b", "c", 
"d", "e", "f", "g", "h", "i", "j"), class = "factor"), b = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("x", "y"), class = "factor"), 
c = c(NA, 0.0051, 0.2, 6.7e-05, NA, 6.2e-05, 0.01, 0.0025, 
0.098, 0.00087)), .Names = c("a", "b", "c"), row.names = c(NA, 
-10L), class = "data.frame")

Plotting it regularly with ggplot will give this graph:用 ggplot 定期绘制它会得到这个图:

测试的ggplot

> ggplot(test,  aes(fill=a,y=c,x=b)) + 
  geom_bar(position="dodge",stat="identity")

How do I set the y-axis as log scale (eg, 0, 10 -6 , 10 -5 , 10 -4 ... 10 0 ) so that the height of the bars won't be too far apart without directly log-transforming the data?如何将 y 轴设置为对数刻度(例如 0, 10 -6 , 10 -5 , 10 -4 ... 10 0 ),以便在没有直接记录的情况下,条形的高度不会相距太远- 转换数据? Also, how do I accomplish this in such a way that it still shows the NA values as zeroes in the graph?另外,我如何以这样的方式实现这一点,它仍然在图中将NA值显示为零?

I also tried the scale_y_log10() function but the bars go from top to bottom.我也尝试了scale_y_log10()函数,但条形图从上到下。 I would like them not to be that way.我希望他们不要那样。

在此处输入图片说明

Thank you!谢谢!

You can use geom_segment instead rather than geom_bar to specify you want a bar from 0 to test$c value.您可以使用geom_segment而不是geom_bar来指定您想要一个从0test$c值的条。 A warning will be issued as we are still using scale_y_log10() .由于我们仍在使用scale_y_log10()因此将发出警告。

We need to create a segment from each test$a so aes(x=a, xend=a) , and use facet_wrap to separate test$b x and y instead.我们需要从每个test$a创建一个段aes(x=a, xend=a) ,并使用facet_wrap来分隔test$b xy

gg <- ggplot(test) + 
  geom_segment(aes(colour=a, y=0, yend=c, x=a, xend=a), size=10) +
  scale_y_log10() + facet_wrap(~b, scales="free_x") + 
  ylab("log10 value") + xlab("")
gg

I am not a fan of replacing NA with 0 , a missing value is not 0 .我不喜欢用0替换NA ,缺失值不是0 Rather just label NA .而只是标记NA

test$c_label <- test$c
test$c_label[is.na(test$c)] <- "NA"

gg + geom_label(data=subset(test, is.na(test$c)), aes(x=a, y=0.00001, label=c_label), size=5)

While this might be a work-around, I complete agree with @dww's comment - "You should not use a log scale with bar plots. The base at log(0) is impossible to plot. Choice of a different base value is arbitrary and can be used to make the bars look as similar or as different as you wish depending on the value chosen. This is a form of misleading graph. Use a dot plot, or something else instead if you really need log scale."虽然这可能是一种变通方法,但我完全同意 @dww 的评论——“你不应该在条形图中使用对数刻度。无法绘制 log(0) 处的基数。选择不同的基值是任意的,并且可用于根据选择的值使条形看起来像您希望的那样相似或不同。这是一种误导性图表。如果您确实需要对数刻度,请使用点图或其他东西。”

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM