geom_text错放了我的文字

时间:2015-01-23 19:55:55

标签: r ggplot2

我尝试创建一个相当简单的条形图,并在每个条形图的顶部添加值以便清晰(而不是使用缩放轴)。

这是我的代码:

pbias <- ggplot(PSS.diff.means, aes(x=Control, y=PSS, ymax=37, fill=Modality)) +
          theme_bw() +
          theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                axis.line = element_blank(), axis.text.y=element_blank(), axis.text.x=element_blank(), axis.ticks=element_blank(), axis.title = element_blank(),
                legend.justification = 'left') +
          geom_bar(position = "dodge", stat='identity') +
          scale_fill_manual(values=c("lightblue","orange")) +
          coord_cartesian() +
          ggtitle("Normalized PSS values indicate threat bias per condition")

pbias + geom_text(aes(label=round(PSS,2), colour=Modality), hjust=-.2) + coord_flip()

这就是我得到的:

http://i.stack.imgur.com/MxiIQ.png

我试图通过调整geom_text()函数中的各种东西来修复两件事,但我无处可去。首先,我似乎无法将值放在正确的位置,因为我希望它们位于各自条形的中间,而不是分组变量的位置。其次,我的颜色方案在geom_text中被翻转,尽管在我看来我喜欢用我用来定义颜色填充的相同变量。有什么想法吗?

这是我使用的数据框:

structure(list(Modality = structure(c(1L, 2L, 1L, 2L), contrasts = structure(c(-1,1), .Dim = c(2L, 1L), .Dimnames = list(c("0", "1"), NULL)), .Label = c("Visual","Tactile"), class = "factor"), Control = structure(c(1L, 1L,2L, 2L), contrasts = structure(c(-1, 1), .Dim = c(2L, 1L), .Dimnames = list(c("0", "1"), NULL)), .Label = c("Comparison", "Pain control"), class = "factor"), PSS = c(8.22627487231047, 1.37218085266906,5.93659638506416, 33.4255762835254)), .Names = c("Modality","Control", "PSS"), row.names = c(NA, -4L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

这对我来说很合适,正如我所描述的那样,正如我在其中所指出的那样,我指出:

pbias + 
    geom_text(aes(label=round(PSS,2), colour=Modality),position = position_dodge(0.9)) + 
    scale_color_manual(values=c("lightblue","orange")) + 
    coord_flip()

enter image description here