Reputation: 5141
I'm trying to plot three different lines with ggplot2 that show the temporal series of three different variable (max, min and mean temperature). The problem comes when I want to set the color for each line/variable.
ggplot command:
plot.temp=ggplot(data,aes_string(x="date", y="Tmed",colour="Tmed")) + geom_line() +
geom_line(data=data,aes_string(x="date",y="Tmin",colour="Tmin")) +
geom_line(data=data,aes_string(x="date",y="Tmax",colour="Tmax"))
I don't use group as each row stands for one single time, not available categories here.
I have tried different optinos found in another posts like scale_color_manual
but then a Continuous value supplied to discrete scale
error message appears
You can find data file at http://ubuntuone.com/45LqzkMHWYp7I0d47oOJ02 that can be easily read with data = read.csv(filename,header=T, sep=",",na.strings="-99.9")
I'd just like to set the color line manually but can't find the way.
Thanks in advance.
Upvotes: 1
Views: 4077
Reputation: 98429
First, you need to convert date
to Date object because now it is treated as factor. If date is treated as factor then each date
value is assumed as separate group.
data$date<-as.Date(data$date)
Second, as you are using aes_string()
also colour="Tmed"
is interpreted as colors that depend from actual Tmed
values. Use aes()
and quotes only for colour=
variable. Also there is no need to repeat data=
argument in each geom_line()
because you use the same data frame.
ggplot(data,aes(x=date, y=Tmed,colour="Tmed")) + geom_line() +
geom_line(aes(y=Tmin,colour="Tmin")) +
geom_line(aes(y=Tmax,colour="Tmax"))
Of course you can also melt your data and then you will need only one geom_line()
call (but still you need to change date column).
library(reshape2)
data2<-melt(data[,1:4],id.vars="date")
ggplot(data2,aes(date,value,color=variable))+geom_line()
Upvotes: 4