繁体   English   中英

如何为barplot(ggplot2)转换y轴

[英]How to transform y-axis for barplot (ggplot2)

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

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

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

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, 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) aes() 为了在y轴上使用科学计数法,我使用scale_y_continous带有科学格式的scale_y_continous

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")

暂无
暂无

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

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