首页 文章

ggplot geom_line():用因子变量着色线的问题

提问于
浏览
0

我正在尝试使用ggplot在y轴上绘制粒子数 dN ,在x轴上绘制对象's diameter ' Dp' . 我有多年的平均每月 Value .

我的数据是长格式的红外线 . month',, and 'month_year 是因素,而 Dp' and dN`是数字 . 我有几年和几个直径(Dp) .

month year Dp dN month_year 1 2006 3.16 4045.1261 1 / 2006 2 2006 3.16 4447.6436 2 / 2006 3 2006 3.16 6436.9364 3 / 2006 4 2006 3.16 5632.1306 4 / 2006 5 2006 3.16 4840.1949 5 / 2006 6 2006 3.16 1480.3604 6 / 2006 7 2006 3.16 928.6990 7 / 2006 8 2006 3.16 1258.5296 8 / 2006 9 2006 3.16 725.8546 9 / 2006 10 2006 3.16 767.1681 10 / 2006 11 2006 3.16 647.2605 11 / 2006 12 2006 3.16 449.9188 12 / 2006 1 2007 3.16 647.4428 1 / 2007 2 2007 3.16 682.0582 2 / 2007 3 2007 3.16 614.0303 3 / 2007

我首先用 month_year 变量绘制每一行着色的图:

ggplot(dataplot_m_y) + geom_line(aes(Dp, dN, colour=month_year), size=0.5)+ theme(legend.position="none")+ scale_x_log10()

我得到了这个:

enter image description here
每行对应一个月,而月份大致用相同颜色着色(我的意思是,绿色大致对应于所有年份的"Januaries"等) . 传说太长了,所以我没有绘制它 .

现在,我希望获得完全相同的图表,但每月使用相同的颜色(并包括图例) .

这是我试过的:

ggplot(dataplot_m_y) + geom_line(aes(Dp, dN, colour=month), size=0.5)+ scale_x_log10() 但我得到了这个:

enter image description here

我也试过了

ggplot(dataplot_m_y) + geom_line(aes(Dp, dN), colour=month, size=0.5)+ scale_x_log10() 但是我收到以下错误:"Error in rep(value[[k]], length.out = n) : attempt to replicate an object of type 'closure'"

有人可以帮我解决这个问题吗?我需要以不同方式排列数据吗?

谢谢

1 回答

  • 0

    因此解决方案应该是为 month 定义实际上是 month_year 但颜色的行的组 .

    ggplot(dataplot_m_y) +
      geom_line(aes(Dp, dN, colour=month, group=month_year), size=0.5)+
      theme(legend.position="none")+
      scale_x_log10()
    

相关问题