【问题标题】:Negative Labels hidden by bar in ggplot: geom_barggplot中被bar隐藏的负标签:geom_bar
【发布时间】:2012-12-03 15:44:41
【问题描述】:

这是一个扩展问题:How to put labels over geom_bar for each bar in R with ggplot2

我的数据有一些负面影响,因此标签侵入了条形图。我该如何调整这个?我需要动态读取标签位置。

谢谢!

growth<-data.frame(ElapsedTimeMonths=c(3,6,12,24),MedianGrowth=c(-0.011,-0.002,0.014,0.052))
g<-ggplot(growth, aes(x=factor(ElapsedTimeMonths),y=MedianGrowth))
g<-g+geom_bar(position="dodge", stat="identity")
g<-g+scale_y_continuous(labels = percent)
g<-g+geom_text(aes(label=as.character(100*MedianGrowth)), position=position_dodge(width=0.9), vjust=-0.25)
g

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    图形参数设置可以是向量,以便不同 形状(此处为几何文本)可以有不同的外观。

    您需要将 vjust 作为向量,相反符号为 MedianGrowth

    你的数据

    growth <- data.frame(ElapsedTimeMonths=c(3,6,12,24),
                       MedianGrowth=c(-0.011,-0.002,0.014,0.052),
                      )
    

    我创建了 vjust 向量

     vvjust <- rep(1, length(growth$MedianGrowth))
     vvjust[growth$MedianGrowth > 0 ] <- -0.25
     [1]  1.00  1.00 -0.25 -0.25
    

    然后我开始绘制

    g<- ggplot(growth, aes(x=factor(ElapsedTimeMonths),y=MedianGrowth))
    g<- g+ geom_bar(stat='identity',position='dodge')
    g<- g+ scale_y_continuous(labels = percent)
    g<- g+ geom_text(aes(label=as.character(100*MedianGrowth)), 
                     position=position_dodge(width=0.9), vjust=vvjust)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2018-09-01
      相关资源
      最近更新 更多