如何将y轴更改为对数比例? [英] How to change y-axis to log-scale?

查看:176
本文介绍了如何将y轴更改为对数比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与以前的

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

如何将y轴设置为对数刻度(例如,0,10 <-6>,<10> -5 ,10 <-4> ... 10 0 ),这样条形的高度不会太远而不直接对数据进行对数转换?另外,我该如何完成这一工作,以便它仍然在图中显示 NA 值为零?



我也试过了 scale_y_log10()函数,但是这些条从上到下。我希望他们不要那样。





<您可以使用 geom_segment 来代替,而不是使用> geom_bar 指定您想要一个从 0 test $ c scale_y_log10(),所以会发出警告。

我们需要从每个 test $ a 所以 aes(x = a,xend = a),并使用 facet_wrap 来分隔 test $ b x y 来代替。

  gg <-ggplot(test)+ 
geom_segment(aes(color = 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

我不是用 0 NA 的粉丝$ c>,缺少的值不是 0 。而不是标签 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)

虽然这可能是一种解决方法,但我完全同意@ dww的评论 - 你不应该使用带有条形图的对数刻度,log(0)的基准是不可能绘图的。选择不同的基准值是任意的,可以用来使条形图看起来与您所希望的相似或不同,具体取决于所选的值。这是一种误导性图形,如果您确实需要对数刻度,则可以使用点图或其他方式。




This question is related to a previous post.

Say I have this set of data 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(test,  aes(fill=a,y=c,x=b)) + 
  geom_bar(position="dodge",stat="identity")

How do I set the y-axis as log scale (e.g., 0, 10-6, 10-5, 10-4 ... 100) so that the height of the bars won't be too far apart without directly log-transforming the data? Also, how do I accomplish this in such a way that it still shows the NA values as zeroes in the graph?

I also tried the scale_y_log10() function but the bars go from top to bottom. 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. A warning will be issued as we are still using 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.

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. Rather just label 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."

这篇关于如何将y轴更改为对数比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆