R: Plot multiple lines on the same axis with the average in bold?
I have some data that looks like this:
df <- data.frame(time=1:6, a=c(100, 90, 91, 92, 91, 91.5), b=c(99.9, 90.3, 90.9, 91.8, 92, 91.5), c=c(100.3, 88.5, 90.5, 91.5, 91, 91.3))
df <- data.frame(df, mean=apply(df[,2:4], 1, mean))
> df
time a b c mean
1 1 100.0 99.9 100.3 100.06667
2 2 90.0 90.3 88.5 89.60000
3 3 91.0 90.9 90.5 90.8000开发者_运维问答0
4 4 92.0 91.8 91.5 91.76667
5 5 91.0 92.0 91.0 91.33333
6 6 91.5 91.5 91.3 91.43333
I want to plot the lines on the same canvas with time
on the x-axis. I want the lines a
, b
, and c
to be slightly transparent (or lightly colored) and the mean
to be bold and very clear.
Okay, this time in ggplot! First you have to melt your df down to one point per line, then the ggplot magic:
dfm = melt(df,"time",c("a","b","c","mean"))
ggplot(dfm)+geom_line(
aes(x=time,y=value,colour=variable)) +
scale_colour_manual(values=c("#FF000080","#00FF0080","#0000FF80","black"))
In base graphics, matplot:
matplot(df$time,df[,c("a","b","c","mean")],
type="l",
col=c("#FF000040","#00FF0040","#0000FF40","black"),
lty=1,lwd=3)
tweak the colours to your taste. Note that arguments are repeated so all lines have lty=1 and lwd=3, but the colours are individual.
Spacedman doesn't do lattice, but it seems fairly straightforward:
xyplot(a+b+c+mean ~ time, data=df, type="l",
col=c("#FF000080","#00FF0080", "#0000FF80","black"))
精彩评论