/ / Barre di errore colore in ggplot2 - r, ggplot2

Barre degli errori di colore in ggplot2 - r, ggplot2

Per un esempio di dataframe:

df <- structure(list(site = structure(c(1L, 1L, 1L, 1L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 2L, 2L, 2L, 2L), .Label = c("1 - JL",
"11 - KT", "2 - SSD", "3 - USSD", "4 - MES"), class = "factor"),
x = c(3L, 4L, 2L, 2L, 4L, 6L, 1L, 3L, 6L, 4L, 4L, 5L, 3L,
3L, 3L, 1L, 2L, 5L, 4L, 1L), y = c(3L, 5L, 3L, 5L, 4L, 3L,
5L, 2L, 6L, 4L, 3L, 5L, 5L, 5L, 6L, 3L, 4L, 3L, 4L, 2L),
type = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 3L, 4L, 3L,
3L, 3L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("a",
"b", "c", "d"), class = "factor"), y_upper.error = c(2L,
4L, 2L, 4L, 3L, 2L, 4L, 1L, 5L, 3L, 2L, 4L, 4L, 4L, 5L, 2L,
3L, 2L, 3L, 1L), y_lower.error = c(4.5, 6.5, 4.5, 6.5, 5.5,
4.5, 6.5, 3.5, 7.5, 5.5, 4.5, 6.5, 6.5, 6.5, 7.5, 4.5, 5.5,
4.5, 5.5, 3.5)), .Names = c("site", "x", "y", "type", "y_upper.error",
"y_lower.error"), class = "data.frame", row.names = c(NA, -20L
))

Sto creando un grafico con barre di errore.

g <- ggplot (df, aes(x=x, y=y, shape = type)) +
geom_point() +
scale_shape_manual(values=c(21,15,25,18)) + #makes open circle/triangle
facet_wrap(~site, ncol=2)+
geom_errorbar(aes(ymin=y_lower.error, ymax=y_upper.error), colour="red")

g

Desidero colorare diverse parti delle barre di errore, ad es. blu sotto e rosso sopra? È possibile colorare ciascuna coda separata della barra degli errori in questo modo?

risposte:

1 per risposta № 1

Questa è una soluzione di hacking che divide le barre di errore in due (coda inferiore in y, y in coda superiore) e le colora separatamente:

ggplot(df2, aes(x=x, y=y, shape = type))+
geom_errorbar(aes(ymin=y, ymax=y_upper.error), colour="blue", width = 0.1) +
geom_errorbar(aes(ymin=y_lower.error, ymax=y), colour="red", width = 0.1) +
geom_point() +
scale_shape_manual(values=c(21,15,25,18)) + #makes open circle/triangle
facet_wrap(~site, ncol=2)

grafico della barra degli errori a due colori

Ho accorciato la larghezza delle code della barra degli errori in modo che siano (approssimativamente) coperte da geom_point.