0

I have an R dataframe data (made with dplyr) that I'm trying to plot with ggplot():

require(dplyr)
data <- structure(list(gGroup = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L), .Label = c("MC", "R", "UC"), class = "factor"), 
    Episode = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L), .Label = c("Morning", "Day", "Night", "24 hour"
    ), class = "factor"), variable = c("HF", "HF", "LF", "LF", 
    "HF", "HF", "LF", "LF", "HF", "HF", "LF", "LF"), parameter = c("RR", 
    "RT", "RR", "RT", "RR", "RT", "RR", "RT", "RR", "RT", "RR", 
    "RT"), mean = c(3.90575222833804, 4.24572828952087, 5.14491629837998, 
    3.88189313775535, 4.02908403079823, 3.91129824615597, 4.73913642980089, 
    3.63973850905423, 4.66445796048274, 4.21723744674943, 5.57765585365275, 
    4.01444148455851), sd = c(1.09129154084895, 1.43102672123806, 
    1.17782114274004, 1.33381488706382, 1.33497319178289, 1.22259231099975, 
    1.33329948427898, 1.09625319168102, 1.19876558625356, 1.73746797295816, 
    1.05862249404741, 1.91144835753868), se = c(0.199241664579179, 
    0.261268538538247, 0.215039736195078, 0.243520167060353, 
    0.471984298305965, 0.432251656867227, 0.471392553343098, 
    0.387584032867524, 0.215304655178374, 0.312058460044998, 
    0.190134212775724, 0.343306259564318)), .Names = c("gGroup", 
"Episode", "variable", "parameter", "mean", "sd", "se"), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), drop = TRUE, indices = list(
    0:1, 2:3, 4:5, 6:7, 8:9, 10:11), group_sizes = c(2L, 2L, 
2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(
    gGroup = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("MC", 
    "R", "UC"), class = "factor"), Episode = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("Morning", "Day", "Night", 
    "24 hour"), class = "factor"), variable = c("HF", "LF", "HF", 
    "LF", "HF", "LF")), .Names = c("gGroup", "Episode", "variable"
), class = "data.frame", row.names = c(NA, -6L)))

Currently I'm using the following code to plot:

require(ggplot2)
require(ggthemes)
pd <- position_dodge(width=0.9)
p <- ggplot(data, aes(x = gGroup, y = mean, fill = variable)) +
    facet_grid(parameter~Episode) +
    geom_bar(stat="identity", position=pd) +
    geom_errorbar(aes(ymin = mean-se, ymax = mean+se), width = .3, position=pd) +
    theme_hc() + scale_fill_hc() +
    labs(y = "Logit transform of spectral power (m/s2), mean±SE", x= NULL)

ann_text <- data.frame(gGroup = "MC", mean = 6, variable = "LF", parameter = "RR", Episode = "Day")
p + geom_text(aes(ymax = 6.5, width = .2), data = ann_text, label="*", position=pd)

This gives me the following plot: example plot

I'm quite satisfied with the result, but as you can see the asterisk isn't aligned correctly. I looked it up online, and I read this and this and the manual. Everyone I see the suggestions to use position=position_dodge(width=0.9), but this doesn't help for me. I tried hjust to maybe move the asterisk to the right position, but that's of no use either. Funny thing is that my error bars are aligned correctly.

I feel like I'm overlooking something very simple, but I cannot figure out what it is. I'm using R 3.1.3 on OSX 10.10.2, and loading the newest versions of ggplot2 and ggthemes.

Community
  • 1
  • 1
Jasper
  • 555
  • 2
  • 12
  • The thing with [minimal reproducible examples](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) is that they should be minimal and reproducible. Your example is neither. – shadow Mar 13 '15 at 15:30
  • Thanks for your comment! I added a dput and some information about loaded packages. I can reproduce my problem with the code provided. Is there other information I should give? – Jasper Mar 13 '15 at 15:59

1 Answers1

1

In order for position_dodge to work, there needs to be a reason to dodge. That is you need to change the ann_text appropriately with variable = c("LF", "HF"), so that there is a reason to dodge. Then just define the label appropriately. Below I assume you just want the * over the LF bar.

ann_text <- data.frame(gGroup = rep("MC",2), 
                       mean = 6, 
                       variable = c("LF", 'HF'), 
                       label = c("*", ""),
                       parameter = "RR", 
                       Episode = "Day")
p + geom_text(aes(ymax = 6.5, width = .2, label = label), data = ann_text, position=pd)
shadow
  • 21,823
  • 4
  • 63
  • 77