Reputation: 396
I need to make a plot of three mean values +- SE, and the add some additional points to this plot. The y-axis is numerical and the x-axis should be categorical. Some of the additional points have the same y-value within the same category, so to get the point offset, so they did not cover each other, I changed my categories to values 5,10,15, and then gave the two points overlapping for 5 the x-values of 4.9 and 5.1. This works perfect - but now I need to have the categories show on the x-axis.
This is my original data:
Island;Armean;SE;TLR3;TLR4
ST;4,166666667;0,477;1;1
FG;3,666666667;0,715;3;3
SN;1,666666667;0,333;3;2
TLR3 and TLR4 hold the values for the additional points I want to plot.
qplot(df$Island, df$Armean) + geom_errorbar(aes(x=df$Island, ymin=df$Armean-df$SE, ymax = df$Armean+df$SE), width = 0.25) + geom_point(aes(y=df$TLR3), color ="red") + geom_point(aes(y=df$TLR4), color ="blue") + theme_bw()
The above gives me the plot I need, but except I need to offset the points on 3 for FG and 1 for ST.
I changed my dataset to this:
Island;Armean;SE;TLR3;TLR4
5;4,166666667;0,477;NA;NA
10;3,666666667;0,715;NA;NA
15;1,666666667;0,333;3;2
4,92;NA;NA;1;NA
5,08;NA;NA;NA;1
9,92;NA;NA;3;NA
10,08;NA;NA;NA;3
Which offsets the points perfectly, but I am not sure how to get the old categories back on the x-axis?
I could use jitter instead of the above, but I wish the points to have the same distance to the center line, and only the point that overlap to be offset.
Upvotes: 1
Views: 2115
Reputation: 14370
EDIT: Per @Gregor's comment below, changed qplot
to ggplot
Should be simple enough to get your old categories on the X-Axis. Try this:
ggplot(data=df,aes(Island,Armean)) +
geom_errorbar(aes(x=Island, ymin=Armean-SE, ymax = Armean+SE), width = 0.25) +
geom_point(aes(y=TLR3), color ="red") +
geom_point(aes(y=TLR4), color ="blue") + theme_bw() +
scale_x_continuous(breaks=seq(from=5,to=15, by = 5), labels=c("FG","SN","ST"))
Which gives the output of:
If you don't want to add in the labels manually you can do this by adding in another column to your dataframe that contains the levels you want. See:
df <- read.csv(textConnection("Island;Armean;SE;TLR3;TLR4;Island.group
5;4,166666667;0,477;NA;NA;ST
10;3,666666667;0,715;NA;NA;FG
15;1,666666667;0,333;3;2;SN
4,92;NA;NA;1;NA;ST
5,08;NA;NA;NA;1;ST
9,92;NA;NA;3;NA;FG
10,08;NA;NA;NA;3;FG"),header=TRUE,dec=",",sep=";")
ggplot(data=df,aes(Island,Armean)) +
geom_errorbar(aes(x=Island, ymin=Armean-SE, ymax = Armean+SE), width = 0.25) +
geom_point(aes(y=TLR3), color ="red") +
geom_point(aes(y=TLR4), color ="blue") + theme_bw() +
scale_x_continuous(breaks=seq(from=5,to=15, by = 5), labels=levels(df$Island.group))
Which gives the same exact output:
Upvotes: 2