在条形图中获取x轴坐标?

时间:2012-06-29 18:56:12

标签: r ggplot2

我正在尝试将每个条形的值添加到我的条形图中。我想在它的中间写下每个柱的值。我知道如何在Y轴上找到中点,但我不知道如何在x轴上获得中点。如果我有一些如何获得每个柱的起点和终点,我们可以找到中点,但我不知道如何。任何人都可以指导我如何解决这个问题? 这是我的代码:

G3.AllLevels.data$pos = G3.AllLevels.data$ProfChange*0.5

ggplot(data=G3.AllLevels.data, aes(x=ProfLevel, y=ProfChange, fill=TRTstatus)) + 
   geom_bar(position=position_dodge(), colour="black") + 
   scale_fill_manual(values=c(rgb(198, 64, 5, maxColorValue=255), 
                     rgb(33, 80, 186, maxColorValue=255))) +
   ylab("Change in %Students in Each Level") + 
   xlab("Achievement Levels") + 
   opts(axis.text.x  = theme_text(size=12), 
        axis.title.x = theme_text(size=14),
        axis.title.y = theme_text(angle = 90, size=14))

1 个答案:

答案 0 :(得分:3)

您不需要数字坐标,只要您使用正确的标签构建第二个数据框:

p <- ggplot(diamonds, aes(clarity, fill=cut)) + 
        geom_bar(position="dodge")

lab_dat <- unique(diamonds[,c("cut","clarity")])
lab_dat$y <- 4000
lab_dat$lab <- with(lab_dat,paste(cut,clarity,sep = "-"))

p + geom_text(data = lab_dat,aes(y=y,label = lab),
                angle = 90,size = 2,position = position_dodge(width = 1))

enter image description here