Chris
Chris

Reputation: 1237

Colour points by x-axis values in ggplot

I have the following code to make a graph of my data:

library(ggplot2)
library(reshape)

sdata <- read.csv("http://dl.dropbox.com/u/58164604/sdata.csv", stringsAsFactors = FALSE)
pdata<-melt(sdata, id.vars="Var")
p<-ggplot(pdata, aes(Var,value,col=variable))
p+geom_point(aes(shape = variable),alpha=0.7)

This creates a graph with 'Var' being the x-axis and 'value' being the y-axis. What I would like to do is change how the points are coloured. Instead of being by the variable name, I would like them to be by their 'Var' value. So I would like all points that have a Var value between 1-10 to be one colour, 11-20 to be another, and so on for 21-30, 31-35 and 36-41. What I would also like is there to be a ribbon/area shaded behind these points that extends from the highest to lowest value for each Var value, but this ribbon would also have to have the same colour as the points, just with a lower transparency level.

For a bonus question I am also having trouble getting the 'mean' variable from my example to appear as a geom_line rather than a geom_point. I have been playing around with this:

 p+geom_point()+geom_line(data=pdata[which(pdata$variable=="Mean")])

but I can't get it to work. If anyone can help with any of this that would be great. Thanks.

Upvotes: 1

Views: 769

Answers (2)

agstudy
agstudy

Reputation: 121568

Using cut with options labels=F, I add a new variable for coloring.

pdata <- transform(pdata,varc =cut(pdata$Var,10,labels=F))
p<-ggplot(subset(pdata,variable!='Mean'), aes(Var,value,col=varc))
p+geom_point(aes(shape = variable),alpha=0.7)+
 geom_line(data=subset(pdata,variable =='Mean'),size=2)

Edit:ribbon part

I don't understand the part of the ribbon(maybe if you can more explain upper and lower values), but I think here we can simply use geom-polygon

last_plot()+ geom_polygon(aes(fill=varc, group=variable),alpha=0.3,linetype=3) 

enter image description here

Upvotes: 1

Paul Hiemstra
Paul Hiemstra

Reputation: 60924

In regard to your first question, you can use the cut function to classify your continuous data into categories. For example:

with(mtcars, cut(mpg, seq(min(mpg), max(mpg), length = 5))

This cuts the continuous values in the mpg column into 5 classes.

Upvotes: 0

Related Questions