1

I'm trying to create a rather simple bar graph, and add the values on top of each bar for clarity (rather than using a scaled axis).

This is my code:

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()

and this is what I'm getting:

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

I'm trying to fix two things by tweaking all sorts of stuff in the geom_text() function, but I'm getting nowhere. First, I can't seem to get the values to be on the correct positions, as I want them to be in the middle of their respective bars, not on the position of the grouping variable. Second, somehow my colour scheme gets flipped in the geom_text, even though it seems to me like I'm using the same variable I used to define my colour fill. Any ideas?

Here's the dataframe I used:

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")
  • Post the `PSS.diff.means` data.frame (you can use `dput(PSS.diff.means)` and then paste the result of that) – arvi1000 Jan 23 '15 at 20:05
  • Your first question is probably a duplicate of [this](http://stackoverflow.com/q/26660525/324364). For the second, you set a manual scale for fill but not for color. – joran Jan 23 '15 at 20:05
  • Thanks @joran second part worked out fine. For the first, I tried adding group=Control to the aes() fuction in geom_text, but that didn't work (couldn't find the variable, not even when I specifically added what dataframe to look in). – Wouter Durnez Jan 23 '15 at 20:28
  • @arvi1000, dataframe was added as requested. Thanks for that function! – Wouter Durnez Jan 23 '15 at 20:29
  • The answer there plainly states that you need to dodge the text. If ggplot isn't seeing a variable then something else is wrong that you haven't shown us. – joran Jan 23 '15 at 20:30
  • I added the dataframe. There's not much else to it, I think. Can't think of anything I'm not showing you right now. – Wouter Durnez Jan 23 '15 at 20:37

1 Answers1

0

This works just fine for me, just as I described, and as it outlined in the duplicate I pointed to:

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

joran
  • 169,992
  • 32
  • 429
  • 468
  • Thanks! Two mistakes I made: 1) I used position = "dodge" rather than the position_dodge function (didn't know that would be different, thought the function was just to tweak it a bit) 2) The group thing was unnecessary, and it was still a part of my code. – Wouter Durnez Jan 23 '15 at 21:09