如何为barplot(ggplot2)转换y轴 [英] How to transform y-axis for barplot (ggplot2)

查看:50
本文介绍了如何为barplot(ggplot2)转换y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

样本数据 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 5.1e-03
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

说我想用这组数据绘制条形图.但是,由于值彼此之间相差几个数量级,因此很难直观地显示图形.如何更改y轴使其值为0、10 -6 ,10 -5 ,10 -4 ... 10 -1 ?

Say I want to draw a bar graph with this set of data. But because the values differ by several orders of magnitude with each other, it's difficult to appreciably visualise the graph. How do I alter the y-axis in such a way that its values are 0, 10-6, 10-5, 10-4 ... 10-1?

到目前为止,我一直在用ggplot进行尝试,但无法正常工作.谢谢!

So far I've been trying it with ggplot but I can't get it to work. Thanks!

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

dput(test) :(我不知道这件事的相关性,但无论如何在这里还是这样).

dput(test): (I don't know the relevance of this but here it is anyway.)

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, 0.0051, 6.2e-05, 0.01, 0.0025, 
0.098, 0.00087)), .Names = c("a", "b", "c"), row.names = c(NA, 
-10L), class = "data.frame")

推荐答案

要使用log10比例尺和反向钢筋,我在 aes() -log(c,10)>.为了在y轴上使用科学计数法,我使用了具有科学格式的 scale_y_continous .

To use log10 scale and reverse bars I used -log(c, 10) within aes(). To use scientific notation on y-axis I used scale_y_continous with scientific format.

library(ggplot2)
ggplot(test, aes(b, -log(c, 10), fill = a)) +
    geom_bar(stat = "identity", position = "dodge") +
    scale_y_continuous(labels = function(x) format(x, scientific = TRUE))

您可以使用 labs(y =")重命名y轴,例如: labs(y ="-log10 C")

You can rename y-axis to whatever you want with labs(y = ""), for example: labs(y = "-log10 C")

这篇关于如何为barplot(ggplot2)转换y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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