ggplot:在Stacked Barplots中设置`geom_text`的正确位置

时间:2015-04-11 09:17:44

标签: r ggplot2

我有一个如下所示的数据框

    Quarter X2013        X2014    Total.Result
1   Qtr 1   77282.13    66421.10    143703.2
2   Qtr 2   69174.64    76480.13    145654.8
3   Qtr 3   65238.47    79081.74    144320.2
4   Qtr 4   65429.73    109738.82   175168.5

我通过融合数据框生成了一个条形图,如下所示。但问题在于如流动图片所示注释条形值。

dfm <- melt(qtr2, id.vars = c('Quarter', 'Total.Result'))

#ggplot statement
{
  ggplot(dfm, aes(x = factor(Quarter), y = value, fill = variable) ) +
  geom_bar(stat="identity", position = 'dodge') + theme_bw() +
    geom_text(aes(label=value,angle = 90), vjust=0)+
    ylab("Sales ($)") +
    xlab("Quarter")+
    ggtitle(label1) + 
    theme(plot.title = element_text(size=20,lineheight=.8, face="bold"))+
    scale_fill_discrete(name="Years",
                        breaks=c("X2013", "X2014"),
                        labels=c("2013", "2014")) +
    theme(legend.background = element_rect(fill="gray90", 
                                           size=.5, linetype="dotted"))+
    theme(axis.title.x = element_text(face="bold", colour="#990000", size=20),
          axis.text.x  = element_text(angle=90, vjust=0.5, size=16))+
    theme(axis.title.y = element_text(face="bold", colour="#990000", size=20),
          axis.text.y  = element_text(angle=90, vjust=0.5, size=16))+
    scale_y_continuous(labels=dollar)
}

输出图 enter image description here 如何为geom_text设置正确的位置和字体样式?


要导入R

的样本数据的.csv文件
"Quarter","2013","2014","Total Result"
"Qtr 1", 77282.13, 66421.10, 143703.23
"Qtr 2", 69174.64, 76480.13, 145654.77
"Qtr 3", 65238.47, 79081.74, 144320.21
"Qtr 4", 65429.73, 109738.82, 175168.55

1 个答案:

答案 0 :(得分:1)

这应该有效,但我没有使用angle

ggplot(dfm,aes(factor(Quarter),value,fill=variable)) +  
   geom_bar(stat="identity",position=position_dodge()) + 
   geom_text(aes(y=value,ymax=value,label=value),
        position=position_dodge(width=0.9),vjust=0.9) +
   ylab("Sales ($)") +
   xlab("Quarter")+
   ggtitle("label1") + 
   theme_bw() +
   theme(plot.title = element_text(size=20,lineheight=.8, face="bold"),
        legend.background = element_rect(fill="gray90", size=0.5, linetype="dotted"),
        axis.title.x = element_text(face="bold", colour="#990000", size=20),
        axis.text.x  = element_text(angle=90, vjust=0.5, size=16),
        axis.title.y = element_text(face="bold", colour="#990000", size=20),
        axis.text.y  = element_text(angle=90, vjust=0.5, size=16))