R:在同一轴上绘制多条线,平均值以粗体显示?

时间:2010-10-25 15:31:07

标签: r plot

我有一些看起来像这样的数据:

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.80000
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

我想在x轴上用time绘制相同画布上的线条。我希望行abc略微透明(或浅色),mean为粗体且非常清晰。

3 个答案:

答案 0 :(得分:4)

好的,这次是在ggplot!首先,你必须将你的df融化到每行一个点,然后是ggplot魔法:

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"))

答案 1 :(得分:2)

在基本图形中,matplot:

matplot(df$time,df[,c("a","b","c","mean")],
  type="l",
  col=c("#FF000040","#00FF0040","#0000FF40","black"),
  lty=1,lwd=3)

根据您的口味调整颜色。请注意,参数重复,因此所有行都有lty = 1和lwd = 3,但颜色是单独的。

答案 2 :(得分:1)

Spacedman不做格子,但看起来相当简单:

   xyplot(a+b+c+mean ~ time, data=df, type="l", 
           col=c("#FF000080","#00FF0080", "#0000FF80","black"))