【问题标题】:R: Change position of x-axis in ggplot2R:改变ggplot2中x轴的位置
【发布时间】:2015-08-18 20:30:00
【问题描述】:

我正在尝试在 ggplot2 中用 y 轴上的对数刻度绘制一个闪避的条形图。但是当我将scale_y_log10() 添加到我的代码中时,R 将 x 轴围绕 1.0 而不是 0 居中。如何将 x 轴移动到 y = 0 以使我的数据

这是我的代码:

snag_means
        year LMU  mean.BA.ha    
1       1911   1  0.11219519  
2       1911   2  0.11921667  
3       1911   3  0.07807921  
4       1911   4  0.04811538  
5       1911   5  0.15444167  
6       1911   6  0.02894872 
7  2005-2007   1  5.48898500 
8  2005-2007   2  5.56879565 
9  2005-2007   3  7.51420667 
10 2005-2007   4  2.74502500 
11 2005-2007   5 10.16307419 
12 2005-2007   6  2.91587692 

snag_mean_BA <- ggplot(data = snag_means, 
                       aes(x = LMU, y = mean.BA.ha, fill = year, width = .8)) + 
  geom_bar(stat = "identity", position = "dodge") + 
  labs(title = "Mean Snag Basal Area by Topography", 
       y = expression("Mean Basal Area (" ~ m^{2} ~ ha^{-1} ~ ")"), 
       x = "LMU", fill = "Year") + 
  scale_fill_manual(breaks = c("1911", "2005-2007"), values = c("gray31", "gray65")) + 
  scale_x_continuous(breaks = seq(0, 6, by = 1)) + 
  scale_y_log10()

图表snag_mean_BA

【问题讨论】:

  • 对于任何x &lt; 1log(x) &lt; 0,所以也许您不想使用对数刻度。但是,您可以重新调整数据并制作自定义 y 标记
  • 这最终得到了我需要做的最好的 - 我通过将我的数据乘以 100 然后添加了scale_y_log10(breaks = c(1, 2, 5, 10, 25, 50, 100, 250, 500, 1000))
  • 添加您的评论作为答案(是的,you can answer your own questions

标签: r ggplot2 scale axis logarithm


【解决方案1】:

你可以在最后取出scale_y_log10(),在aes里面用y = log10(mean.BA.ha)对之前的数据进行日志转换

ggplot(data = snag_means, aes(x = LMU, y = log10(mean.BA.ha), fill = year, width = .8)) +
  geom_bar(stat = "identity", position = "dodge") + 
  labs(title = "Mean Snag Basal Area by Topography", 
       y = expression("Mean Basal Area (" ~ m^{2} ~ ha^{-1} ~ ")"), 
       x = "LMU", fill = "Year") + 
  scale_fill_manual(breaks = c("1911", "2005-2007"), values = c("gray31", "gray65")) + 
  scale_x_continuous(breaks = seq(0, 6, by = 1)) 

【讨论】:

    【解决方案2】:

    如果您缩放数据以使所有值都不小于 1,您可以获得(我认为的)预期结果。

    snag_means$mean.BA.ha <- snag_means$mean.BA.ha + 1
    
    snag_mean_BA <- ggplot(data = snag_means, aes(x = LMU, y = 
    mean.BA.ha, fill = year, width = .8)) + geom_bar(stat = "identity", 
    position = "dodge") + labs(title = "Mean Snag Basal Area by
    Topography", y = expression("Mean Basal Area (" ~ m^{2} ~ ha^{-1} ~ 
    ")"), x = "LMU", fill = "Year") + scale_fill_manual(breaks = 
    c("1911", "2005-2007"), values = c("gray31", "gray65")) + 
    scale_x_continuous(breaks = seq(0, 6, by = 1)) + scale_y_log10()
    
    snag_mean_BA
    

    【讨论】:

      【解决方案3】:

      也许你可以通过使用scale_y_log10()scale_y_sqrt()得到你想要的输出:

      snag_mean_BA2 <- ggplot(data=snag_means, aes(x=LMU, y=mean.BA.ha, fill=year, width=0.8)) +
        geom_bar(stat='identity', position='dodge') + 
        scale_fill_manual(breaks=as.character(unique(snag_means$year)), values=c('gray31','gray65')) +
        scale_x_continuous(breaks=seq(0,6,by=1)) +
        scale_y_sqrt()
      print(snag_mean_BA2)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-31
        • 1970-01-01
        • 2016-01-16
        • 2022-10-13
        • 2018-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多