outa - 9 months ago 53

R Question

I am trying to add summary statistics to my plot but want them to have some transparency. This does not seem to work with geom_crossbar. Taking the example code from the documentation:

`df <- data.frame(`

trt = factor(c(1, 1, 2, 2)),

resp = c(1, 5, 3, 4),

group = factor(c(1, 2, 1, 2)),

upper = c(1.1, 5.3, 3.3, 4.2),

lower = c(0.8, 4.6, 2.4, 3.6)

)

p <- ggplot(df, aes(trt, resp, colour = group))

p + geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2, alpha=0.3)

works fine, i.e. the errorbars are transparent. However, if I use

`p + geom_crossbar(aes(ymin = lower, ymax = upper), width = 0.2, alpha=0.3)`

instead, the transparency does not work. Am I missing something?

Here is the data and this is my command:

`ggplot(plotdata3, aes(x=Ncases, y=Intercept.ols, group=col, colour=col)) + geom_point() + stat_summary(fun.y="mean", geom="line", alpha=0.3, aes(group=1)) +`

stat_summary(fun.data="mean_cl_boot", geom="crossbar", width=0.08, aes(group=1)) + scale_color_manual(values=rgb(matrix(rep(0,27), ncol=3),alpha = 0.5)) +

scale_y_continuous(breaks=seq(0,1,by=0.1)) + coord_cartesian(ylim=c(0,1))

When I leave out the scale_color_manual() part the plot looks like this and now I want to make the crossbars transparent, like the straight line is.

Answer

I'm not sure what you're going for here but there seem to be a couple of problems. For one thing you are are using color to distinguish among 9 different categories ("col"). Even with ggplot's default palette (hues equally spaced around the color wheel), the colors are barely distinguishable from each other. This problem is made worse by the choice of point size, and by the fact that you put the points underneath the summary graphics (line and crossbar). You might be better off to

- put the points on top,
- use point shape 21 (filled circle) and bind fill to the "col", and
- use a Brewer palette

Below are two examples, the plot generated by @lukeA's solution (which does in fact answer the question), and a plot implementing the suggestions above.

```
ggplot(plotdata3, aes(x=Ncases, y=Intercept.ols, group=col, colour=col)) +
geom_point() +
stat_summary(fun.y="mean", geom="line", alpha=0.3, aes(group=1)) +
stat_summary(fun.data="mean_cl_boot", geom="crossbar",
width=0.08, aes(group=1), color = "#30303030") +
scale_y_continuous(breaks=seq(0,1,by=0.1)) +
coord_cartesian(ylim=c(0,1))
```

```
ggplot(plotdata3, aes(x=Ncases, y=Intercept.ols)) +
stat_summary(aes(group=1), fun.y="mean", geom="line", alpha=0.3) +
stat_summary(fun.data="mean_cl_boot", geom="crossbar",
width=0.08, fill="grey80", color="grey50") +
geom_point(aes(group=col, fill=col), size=3, shape=21) +
scale_fill_brewer(palette="Set1") +
scale_y_continuous(breaks=seq(0,1,by=0.1)) + coord_cartesian(ylim=c(0,1))
```

The second plot makes it clear the "col" 6 and 9 tend to be significantly below the mean, and "col" 1 and 7 tend to be significantly above the mean.

Source (Stackoverflow)