下面是我的数据集,我想绘制变量states
,o7
,cas
,并df
随着时间的推移
year states o7 cas df
1989 151 117 35 16
1990 150 158 27 12
1991 150 194 43 12
1992 150 173 38 9
1993 151 169 35 14
1994 153 169 23 9
1995 153 158 22 8
1996 153 157 18 6
1997 153 214 18 11
1998 154 186 17 5
1999 154 222 16 7
2000 155 210 20 4
2001 154 210 19 2
2002 155 231 17 2
2003 155 268 18 1
2004 155 236 16 3
2005 155 263 19 1
2006 155 238 17 5
2007 155 284 16 3
2008 155 318 20 4
2009 155 295 18 5
2010 155 330 20 4
2011 155 312 16 3
我用ggplot2
包装来做到这一点
ggplot(dat, aes(year, o7)) +
geom_line()
但是,我无法在同一图中绘制其他变量。
它主要是推荐使用melt
的功能reshape2
,当你想在同一ggplot积数列包。
# df = your example
require(reshape2)
df_melt = melt(df, id = "year")
ggplot(df_melt, aes(x = year, y = value, color = variable)) + geom_point()
正如@Nathan Day所提到的,列的范围有很大不同,使用afacet_wrap
可能是:
ggplot(df_melt, aes(x = year, y = value, color = variable)) + geom_point() +
facet_wrap(~variable, scales = "free")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句