how to change the aesthetics and appearance of just 1 level of a factor in a ggplot graph
I'm plotting a set of discrete levels of a factor on the x axis and their relevant mean outcome value on the y-axis, something like this:
ggplot(data, aes(item, outcome)) +
stat_summary(fun.y=开发者_运维知识库mean, geom="point", colour="red",size=3)
the last 'item' I have is the mean, and I would like to make this pop out visually.
- Is it possible to have a different shape or color for just one level of the factor item?
- Is it possible to physically shift or create a barrier for one level of a factor (as if it were a facet)?
You can easily make the last level a different color (or shape) by adding another factor to your data frame that has two levels: the one you want, and everything else. For instance:
dat <- data.frame(item=rep(letters[1:3],times=3),outcome=runif(9))
dat$grp <- rep(c("grp1","grp1","grp2"),times=3)
ggplot(dat, aes(item, outcome))+
stat_summary(fun.y=mean,aes(colour=grp), geom="point",size=3)
Then you set the colour aesthetic in aes
rather than globally. Once you have this additional variable, you can also facet on it (edited to reflect @Ben Bolker's comment):
ggplot(dat, aes(item, outcome)) +
stat_summary(fun.y=mean, geom="point",size=3) +
facet_grid(.~grp,scale="free_x",space="free")
精彩评论