Using these data
Data <- structure(list(value = c(180, 528, 180, 147, 468, 151, 194, 568,
210), SE = c(21.7869586486209, 21.0831764730322, 21.2726560659361,
21.7869586486209, 21.0831764730322, 21.2726560659361, 21.7869586486209,
21.0831764730322, 21.2726560659361), PredictionType = c("InSitu",
"ExSitu", "ExSitu", "ExSitu", "InSitu", "ExSitu", "ExSitu", "ExSitu",
"InSitu"), Area = c("AAA", "BBB", "CCC", "AAA", "BBB", "CCC",
"AAA", "BBB", "CCC")), .Names = c("value", "SE", "PredictionType",
"Area"), class = "data.frame", row.names = c(NA, -9L))
and the following code I can produce the plot below.
ggplot(Data)+
geom_point(aes(x=Area, y=value, color=Area),size=3, shape=1)+
geom_errorbar(aes(x=Area, ymin=value-SE, ymax=value+SE, color=Area, linetype = PredictionType),cex=0.75)
The linetype is correctly distinguishing between ExSitu and InSitu predictions, but I want the line type to be reversed. Equivocally, i want the dotted line to be ExSitu and the solid line to be InSitu.
Also, is it possible to offset the error bars so that they are not directly on top of each other? Ideally I would want the solid InSitu estimate centered over the area (AAA, BBB, CCC) and the two dotted ExSitu estimates slightly right and left of center.
Thanks in advance.
You can use
position
argument to offset (dodge) points and error bars (see e.g. here). However, you have more than one point/error bar with the same 'PredictionType' within each 'Area', and need to create a new variable with a unique value for each point within Area to make thedodge
-ing work. Here I useave
to create the dummy variable. I 'borrow' an unusedaes
thetics,fill
, for the dodging, and then remove the fill legend. The order in which thelinetype
s are used is changed inscale_linetype_manual
.Update following comment:
In order to place the 'InSitu' points in the middle, one possibility is to set the id variable to 2 for 'InSitu' data, and to 1 and 3 respectively for 'ExSitu' data:
ggplot coerces PredictionType to a factor, and then assigns each level of the factor to a linetype, in the order that the levels occur. The default puts ExSitu first, so you can fix that in a couple ways.
First, you can convert PredictionType to a factor yourself, and assign the levels you want directly:
Or, you can just call convert it right when you give it to ggplot (although you might need to change the legend title):