How can I add error bars to a ggplot object - r

I have two data sets like below
df1<- structure(list(time = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L), .Label = c("24", "48", "72"), class = "factor"), place = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("B,C", "D,E", "F,G"
), class = "factor"), key = c("boy1", "boy2", "boy3", "boy1",
"boy2", "boy3", "boy1", "boy2", "boy3"), value = c(177.72258835,
0, 74.438539625, 134.3410045, 48915.1, 38.302204425, 97.32286187,
25865.25, 28.67291878), x = c("1", "2", "3", "1", "2", "3", "1",
"2", "3"), y = c(177.72258835, 0, 74.438539625, 134.3410045,
48915.1, 38.302204425, 97.32286187, 25865.25, 28.67291878)), .Names = c("time",
"place", "key", "value", "x", "y"), row.names = c(NA, -9L), class = "data.frame")
df2<- structure(list(time = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L), .Label = c("24", "48", "72"), class = "factor"), place = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("B,C", "D,E", "F,G"
), class = "factor"), key = c("boy1", "boy2", "boy3", "boy1",
"boy2", "boy3", "boy1", "boy2", "boy3"), value = c(58.852340736,
0, 21.291893740908, 42.92051958201, 72521.52726, 16.309811239722,
32.403556124268, 38347.81965, 10.342042262244), x = c("1", "2",
"3", "1", "2", "3", "1", "2", "3"), y = c(58.852340736, 0, 21.291893740908,
42.92051958201, 72521.52726, 16.309811239722, 32.403556124268,
38347.81965, 10.342042262244)), .Names = c("time", "place", "key",
"value", "x", "y"), row.names = c(NA, -9L), class = "data.frame")
I want to plot them together with df2 as the standard deviation for df1
when I plot df1, I do the following
library(ggplot2)
ggplot(df1, aes(x, y, col = key)) +
geom_point() +
scale_x_discrete(labels=c("first", "second", "third"), limits = c(1, 2,3)) +
facet_grid(time ~ .)
but now I want to have the second df as the standard deviation (i.e., the first y-value in df1 is 177.72259, so it's standard deviation is the corresponding y-value in df2, which is 58.85234).

If I understand your question correctly, it sounds like you want to include error bars in your plot. This can be accomplished using only a single data frame, if you just add the standard error as an additional variable like so:
df <- structure(list(time = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L),
.Label = c("24", "48", "72"), class = "factor"), place = structure(c(1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L), .Label = c("B,C", "D,E", "F,G"), class = "factor"),
key = c("boy1", "boy2", "boy3", "boy1", "boy2", "boy3", "boy1", "boy2", "boy3"),
value = c(58.852340736, 0, 21.291893740908, 42.92051958201, 72521.52726,
16.309811239722, 32.403556124268, 38347.81965, 10.342042262244),
x = c("1", "2", "3", "1", "2", "3", "1", "2", "3"), y = c(177.72258835, 0,
74.438539625, 134.3410045, 48915.1, 38.302204425, 97.32286187, 25865.25, 28.67291878),
sd = c(58.852340736, 0, 21.291893740908, 42.92051958201, 72521.52726, 16.309811239722,
32.403556124268,38347.81965, 10.342042262244)), .Names = c("time", "place", "key",
"value", "x", "y", "sd"), row.names = c(NA, -9L), class = "data.frame")
Then you can add error bars to the plot using geom_errorbar(), as follows (I am borrowing the "free-y" scale trick from #jazzurro's answer above):
ggplot(df, aes(x, y, col = key)) +
geom_point() +
scale_x_discrete(labels=c("first", "second", "third"), limits = c(1, 2,3)) +
facet_grid(time ~ .) +
geom_errorbar(aes(ymin = y-sd, ymax = y+sd)) +
facet_grid(time ~ ., scale = "free_y")
Unfortunately your data is a little skewed, in that some measurements are way larger in magnitude than others (especially at time=48 and time=72); you may want to consider a log transformation so that the error bars for the smaller observations do not appear so negligible.

Here is one way for you. I changed the shape of the sd in the second geom_point(). Since the y-scale has a wide range for two of the plots, you see points overlapping.
ggplot() +
geom_point(data = df1, aes(x, y, col = key)) +
geom_point(data = df2, aes(x, y, col = key), shape = 22, alpha = 0.3) +
scale_x_discrete(labels=c("first", "second", "third"), limits = c(1, 2, 3)) +
facet_grid(time ~ ., scale = "free_y")

Related

How to organize all shapes for each color in rows of ggplot legend?

For a plot like this:
df <- structure(list(x = c(-0.951618567265016, -0.0450277248089203,
-0.784904469457076, -1.66794193658814, -0.380226520287762, 0.918996609060766,
-0.575346962608392, 0.607964322225033, -1.61788270828916, -0.0555619655245394
), y = c(0.519407203943462, 0.301153362166714, 0.105676194148943,
-0.640706008305376, -0.849704346033582, -1.02412879060491, 0.117646597100126,
-0.947474614184802, -0.490557443700668, -0.256092192198247),
color = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L
), .Label = c("1", "2", "3", "4"), class = "factor"), shape = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L), .Label = c("1", "2",
"3"), class = "factor")), class = "data.frame", row.names = c(NA,
-10L))
g <- ggplot() +
geom_point(data = df, aes(x = x, y = y, colour = color, shape = shape)) +
theme(legend.position = "right")
Is it possible to somehow obtain the legend in the following format?
Maybe this is what you are looking for.
The starting point is to have only one legend. To this end I add a new variable shape_color as the interaction of your factos color and shape.
Map shape_color on both color and shape.
To get the colors and shapes right we make use of scale_xxx_manual. To this end I set up two vectors with colors and shapes.
Organize the legend in rows using guide_legend with arguments nrow = 4 and byrow = TRUE
The tricky part is the labelling.
a. To this end I use a helper function which replaces the unwanted labels to empty strings, i.e. only every third label is shown, and makes sure that only the color category shows up in the label
b. Finally, to have the label for the fourth row also on the right we have to make sure that the empty categories are "included" in the legend. To this end I use arguemnt drop=FALSE in both scales so that unused levels are included in the legend. However, I set the color and the shape for these categories to NA so that they are invisible.
library(ggplot2)
df <- structure(list(x = c(-0.951618567265016, -0.0450277248089203,
-0.784904469457076, -1.66794193658814, -0.380226520287762, 0.918996609060766,
-0.575346962608392, 0.607964322225033, -1.61788270828916, -0.0555619655245394
), y = c(0.519407203943462, 0.301153362166714, 0.105676194148943,
-0.640706008305376, -0.849704346033582, -1.02412879060491, 0.117646597100126,
-0.947474614184802, -0.490557443700668, -0.256092192198247),
color = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L
), .Label = c("1", "2", "3", "4"), class = "factor"), shape = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L), .Label = c("1", "2",
"3"), class = "factor")), class = "data.frame", row.names = c(NA,
-10L))
df$shape_color = interaction(df$shape, df$color)
colors <- rep(scales::hue_pal()(4), each = 3)
shapes <- rep(scales::shape_pal()(3), 4)
colors <- setNames(colors, levels(df$shape_color))
shapes <- setNames(shapes, levels(df$shape_color))
colors[!levels(df$shape_color) %in% df$shape_color] <- NA
shapes[!levels(df$shape_color) %in% df$shape_color] <- NA
mylabels <- function(breaks) {
breaks[!grepl("^3", breaks)] <- ""
gsub("^\\d+\\.", "", breaks)
}
ggplot() +
geom_point(data = df, aes(x = x, y = y, colour = shape_color, shape = shape_color)) +
scale_color_manual(values = colors, labels = mylabels, drop = FALSE) +
scale_shape_manual(values = shapes, labels = mylabels, drop = FALSE) +
guides(color = guide_legend(nrow = 4, byrow = TRUE, label.position = "right")) +
theme(legend.position = "right", legend.key = element_rect(fill = NA))
Thanks for this answer, it works really well although Im having trouble generalizing to my real data which is like 12 shapes and 34 colors lol. probably need to play around with this idea a bit more

How can I merge two plots of ggplot.predict in R?

I have produced two different plots based on two different models: model and model1. Please find enclosed My Data below. I have attached the two plots:
Model
Model1
I wish to merge the two plots and keep the confidence bands at the same time. I have tried several solution, e.g. rbind, but that does not seem to work - please see below.
I have used the following scripts to produce the two plots
model <- cph(Surv(os.neck,mors)~rcs(test),data=n)
model1 <- cph(Surv(os.neck,mors)~rcs(test),data=n1)
j <- ggplot(Predict(model, fun=exp), colfill = "blue")
k <- ggplot(Predict(model1, fun=exp), colfill = "yellow")
I have tried rbind:
e <- Predict(model, fun=exp, conf.int = TRUE)
f <- Predict(model1, fun=exp, conf.int = TRUE)
j <- ggplot(rbind(e,f))
Which gave this:
rbind()
My data:
n <- subset(w, w$stadie %in% 1:2)
n1 <- subset(w, w$stadie %in% 3:5)
The requested dput(out) from the comments
w <- structure(list(model = c("1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2"), test = c(0.0438735177865613, 0.0465676207122569,
0.0492617236379526, 0.0519558265636483, 0.0546499294893439, 0.0573440324150396,
0.0600381353407353, 0.062732238266431, 0.0654263411921266, 0.0681204441178223,
0.070814547043518, 0.0735086499692136, 0.0762027528949093, 0.078896855820605,
0.0815909587463007, 0.0842850616719963, 0.086979164597692, 0.0896732675233877,
0.0923673704490833, 0.095061473374779, 0.05, 0.0530569514237856,
0.0561139028475712, 0.0591708542713568, 0.0622278056951424, 0.065284757118928,
0.0683417085427136, 0.0713986599664992, 0.0744556113902848, 0.0775125628140703,
0.0805695142378559, 0.0836264656616415, 0.0866834170854271, 0.0897403685092127,
0.0927973199329983, 0.0958542713567839, 0.0989112227805695, 0.101968174204355,
0.105025125628141, 0.108082077051926), yhat = c(0.715524721809984,
0.72420520893997, 0.732895287854242, 0.741495950465592, 0.749903690905934,
0.758010700841758, 0.765705214141122, 0.772872009692537, 0.779393079520142,
0.785148467039571, 0.79001727733411, 0.793878857700365, 0.796614142441177,
0.798107151024956, 0.798246668871875, 0.796979824770716, 0.794412433838086,
0.790683064226291, 0.785933397797749, 0.780306386213083, 1.24887346414771,
1.12142387236568, 1.00744333341272, 0.906978784944319, 0.819807522848923,
0.745379660125369, 0.682977886151413, 0.631846830283734, 0.591296955987878,
0.560790614744859, 0.53975355731851, 0.52685030147002, 0.520878199524915,
0.520957917193064, 0.526437601275528, 0.53682068603444, 0.551708849922178,
0.570754454105439, 0.593618741429514, 0.619933518450193), lower = c(0.445870969928758,
0.472487603995491, 0.498645159577579, 0.523317755828918, 0.545270747924011,
0.563214260495099, 0.576107648755599, 0.583517928079882, 0.585795811114823,
0.583918701876133, 0.579131268180072, 0.572630973080174, 0.565412209767786,
0.558237952034289, 0.551671245622871, 0.546072898734981, 0.541548416151744,
0.538098574671309, 0.535672640626991, 0.534183860233478, 0.613882362074539,
0.611611984419279, 0.601234738035742, 0.579326232945668, 0.543582975437934,
0.496000647093785, 0.443637816386947, 0.39437687025085, 0.353159479619957,
0.321944706132161, 0.30083406381699, 0.288326373517578, 0.282948308375769,
0.283624310505754, 0.289563062775844, 0.300128054614955, 0.314709399887597,
0.332603569457389, 0.352917102130059, 0.374528152852913), upper = c(1.14825961332055,
1.11002527943736, 1.07718984661152, 1.05063556210888, 1.03133268706487,
1.02018052967182, 1.01769951541058, 1.02367230657634, 1.03697151956046,
1.05572593121937, 1.07769573631852, 1.10061046351294, 1.12235654089946,
1.14104571750444, 1.1550316414364, 1.16317224781343, 1.16534569433533,
1.16183119131315, 1.15311341092747, 1.13982862772903, 2.54069024589915,
2.05619172538896, 1.68809618910841, 1.4199434956646, 1.23639702655924,
1.1201413566373, 1.05144055745915, 1.01230687460364, 0.990011907755607,
0.976832690818709, 0.968420593537629, 0.962698059052612, 0.958882208194717,
0.956889594556209, 0.957085290437296, 0.96017831230139, 0.967186411308867,
0.979426190201882, 0.998487202942342, 1.02613799355416), .predictor. = c("test",
"test", "test", "test", "test", "test", "test", "test", "test",
"test", "test", "test", "test", "test", "test", "test", "test",
"test", "test", "test", "test", "test", "test", "test", "test",
"test", "test", "test", "test", "test", "test", "test", "test",
"test", "test", "test", "test", "test", "test", "test"), .set. = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", "2"), class = "factor")), .Names = c("model",
"test", "yhat", "lower", "upper", ".predictor.", ".set."), row.names = c("1.1",
"1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "1.10",
"1.11", "1.12", "1.13", "1.14", "1.15", "1.16", "1.17", "1.18",
"1.19", "1.20", "2.201", "2.202", "2.203", "2.204", "2.205",
"2.206", "2.207", "2.208", "2.209", "2.210", "2.211", "2.212",
"2.213", "2.214", "2.215", "2.216", "2.217", "2.218", "2.219",
"2.220"), class = c("Predict", "data.frame"), info = structure(list(
Design = structure(list(label = structure("Set", .Names = ".set."),
units = structure("", .Names = ".set.")), .Names = c("label",
"units")), varying = ".set.", adjust = structure(list(`1` = NULL,
`2` = NULL), .Names = c("1", "2"))), .Names = c("Design",
"varying", "adjust")))
Thank you in advance,
C.
Here is a basic plot
ggplot(as.data.frame(out), aes(x = test)) +
geom_ribbon(aes(fill = model, ymin = lower, ymax = upper), alpha = .3) +
geom_line(aes(y = yhat, col = model))
We need as.data.frame(out) because out is of class Predict.
You could add another theme change fill and color or you might also want to add a meaningful title, subtitle etc. SO is full of examples.
We can use the JCO palette from the ggsci package
library(ggsci)
ggplot(as.data.frame(out), aes(x = test)) +
geom_ribbon(aes(fill = model, ymin = lower, ymax = upper), alpha = .3) +
geom_line(aes(y = yhat, col = model)) +
scale_color_jco() +
scale_fill_jco()
To change legend labels do
... +
scale_color_jco(labels = c("A", "B")) +
scale_fill_jco(labels = c("A", "B"))

Credibility interval with respect two factors using ggplot2 in r

I have problem ploting credibility interval like this:
My data structure is following,L1,L2,M,U1,U2 stand for 0.025quant,0.25quant,0.5quant,0.75quant,0.975quant,respectively.
`
structure(list(approach = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("INLA",
"rjags")), param = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("alpha",
"beta", "sig2")), L1 = c(0.0844546867936143, 1.79242348175439,
0.163143886545317, 0.0754165380733685, 1.79067991488052, 3.66675821267498
), L2 = c(0.60090835904286, 1.95337968870806, 0.898159977552433,
0.606017177641373, 1.95260448314298, 4.07080184844179), M = c(0.870204161297956,
2.03768437879748, 2.20651061559405, 0.87408237273113, 2.03725552264872,
4.32531027636171), U2 = c(1.13905085248391, 2.12210930874551,
4.26836270504725, 1.66260576926063, 2.28900567640091, 5.10063756831338
), U1 = c(1.65214011950274, 2.28396345192398, 4.9109804477583,
1.1450384685802, 2.12117799328209, 4.55657971279654), AP = structure(c(1L,
4L, 5L, 2L, 3L, 6L), .Label = c("INLA.alpha", "rjags.alpha",
"INLA.beta", "rjags.beta", "INLA.sig2", "rjags.sig2"), class = "factor")), .Names = c("approach",
"param", "L1", "L2", "M", "U2", "U1", "AP"), row.names = c(NA,
-6L), class = "data.frame")`
I referenced this answerenter link description here,but 'fill' seems only work for boxplot case.the code I tried so far is:
CI$AP=interaction(CI$approach,CI$param)
p=ggplot(CI,aes(y=AP))+geom_point(aes(x=M))
p=p+geom_segment(aes(x=L1,xend=U1,y=AP,yend=AP))
p=p+geom_segment(aes(x=L2,xend=U2,y=AP,yend=AP),size=1.5)
It is far away from what I want.
Many thanks!
How about the following:
ggplot(df, aes(x = param, y = M, colour = approach)) +
geom_point(position = position_dodge2(width = 0.3), size = 3) +
geom_linerange(
aes(ymin = L2, ymax = U2, x = param),
position = position_dodge2(width = 0.3),
size = 2) +
geom_linerange(
aes(ymin = L1, ymax = U1, x = param),
position = position_dodge2(width = 0.3),
size = 1) +
coord_flip() +
labs(x = "Parameter", y = "Estimate")
Sample data
df <- structure(list(approach = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("INLA",
"rjags")), param = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("alpha",
"beta", "sig2")), L1 = c(0.0844546867936143, 1.79242348175439,
0.163143886545317, 0.0754165380733685, 1.79067991488052, 3.66675821267498
), L2 = c(0.60090835904286, 1.95337968870806, 0.898159977552433,
0.606017177641373, 1.95260448314298, 4.07080184844179), M = c(0.870204161297956,
2.03768437879748, 2.20651061559405, 0.87408237273113, 2.03725552264872,
4.32531027636171), U2 = c(1.13905085248391, 2.12210930874551,
4.26836270504725, 1.66260576926063, 2.28900567640091, 5.10063756831338
), U1 = c(1.65214011950274, 2.28396345192398, 4.9109804477583,
1.1450384685802, 2.12117799328209, 4.55657971279654), AP = structure(c(1L,
4L, 5L, 2L, 3L, 6L), .Label = c("INLA.alpha", "rjags.alpha",
"INLA.beta", "rjags.beta", "INLA.sig2", "rjags.sig2"), class = "factor")), .Names = c("approach",
"param", "L1", "L2", "M", "U2", "U1", "AP"), row.names = c(NA,
-6L), class = "data.frame")

Adding different labels to each point on a Taylor diagram

I was trying to plot Taylor diagram to compare original and bias-corrected rainfall for five rain gauge stations using 'openair' package. The plotting is okay, but I want to label each point by their name for same for each panel.
How can I do that? Solutions will be highly appreciated. I am using the below code:
TaylorDiagram(kj, obs = "Gauge", mod = "value", group = c("prod","variable"), type = "station", normalise = T, pch=1)
And the output is:
kj is the dataframe I used. I can share it if needed
Sample of kj: by dput(head(kj))
structure(list(Gauge = c(0, 0, 0, 0, 20, 0), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("A", "B", "C", "D"), class = c("ordered",
"factor")), value = c(0, 0, 0, 0, 0, 0), station = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("Sunamganj", "Sheola", "Nakuagaon",
"Brahmanbaria", "Bhairab.Bazar"), class = c("ordered", "factor"
)), prod = c("original GSRPs", "original GSRPs", "original GSRPs",
"original GSRPs", "original GSRPs", "original GSRPs")), .Names = c("Gauge",
"variable", "value", "station", "prod"), row.names = c("1", "2",
"3", "4", "5", "6"), class = "data.frame")

R: ggplot annotate geom_text with position_dodge and facets

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:
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.
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)

Resources