我有一个数据框,其中包含资产毛利,10个工业类和e时间1970年至2015年的长格式值。我想绘制每个时间序列和(总体)平均值。但问题是,情节变得非常混乱。所以我想把它分成两个或三个小区。我使用ggplot并设法绘制时间序列,但我无法弄清楚如何以正确的方式进行子图。ggplot有很多群体;子图(方面)更好的安排
df <- melt(sic_j[1:11], id.vars='time', variable.name='Industry')
> head(df, 20)
time Industry value
1 1970 Agriculture, Forestry, Fishing 0.4450458
2 1971 Agriculture, Forestry, Fishing 0.3834808
3 1972 Agriculture, Forestry, Fishing 0.3970010
4 1973 Agriculture, Forestry, Fishing 0.3993006
5 1974 Agriculture, Forestry, Fishing 0.3960956
6 1975 Agriculture, Forestry, Fishing 0.4052760
7 1976 Agriculture, Forestry, Fishing 0.3856735
8 1977 Agriculture, Forestry, Fishing 0.4062286
9 1978 Agriculture, Forestry, Fishing 0.3631151
10 1979 Agriculture, Forestry, Fishing 0.3987136
11 1980 Agriculture, Forestry, Fishing 0.3926147
12 1981 Agriculture, Forestry, Fishing 0.3207508
13 1982 Agriculture, Forestry, Fishing 0.3638654
14 1983 Agriculture, Forestry, Fishing 0.2901777
15 1984 Agriculture, Forestry, Fishing 0.3329089
16 1985 Agriculture, Forestry, Fishing 0.3384187
17 1986 Agriculture, Forestry, Fishing 0.3142270
18 1987 Agriculture, Forestry, Fishing 0.3610059
19 1988 Agriculture, Forestry, Fishing 0.2502937
20 1989 Agriculture, Forestry, Fishing 0.3156292
ggplot(df, aes(x=time, y=value))+
geom_line(aes(group=Industry, color=Industry))+
stat_summary(fun.y=mean, na.rm=T, group=11, alpha=1, color='red', size=1.5, geom='line')+
theme_bw()+
labs(x='year', y='gross profits on assets',
color=NULL)+theme(legend.position = 'bottom')
我试着用facet_grid如下因素:
ggplot(df, aes(x=time, y=value))+
geom_line(aes(group=Industry, color=Industry))+
stat_summary(fun.y=mean, na.rm=T, group=11, alpha=1, color='red', size=1.5, geom='line')+
theme_bw()+
labs(x='year', y='gross profits on assets',
color=NULL)+theme(legend.position = 'bottom')+facet_grid(Industry~.)
我试图拆为了每个子区有3-4个行业,但是我得到了这个错误:
Error in combine_vars(data, params$plot_env, vars, drop = params$drop) :
At least one layer must contain all variables used for facetting
最后,我想要有一个安排好的这11个时间序列(10个行业和均值)的情节。由于我已经尝试过不同的颜色,线型和点数,我认为最好的方法是一些子图,但也许有人有更好的想法......?
我认为你的原始情节看起来不错。这是所有行业在平均价值背景下最直接的可视化。我认为方面会让眼睛比较平均值与个人群体更加困难。也许堆积的酒吧加上一条线的意思可能会使它更清洁一点? – neilfws
如果您想要分成多个子组,请在新列中为这些组创建一个虚拟变量,然后使用虚拟变量构建面 –
如果不需要使用'ggplot2',请尝试使用['animint'包] (https://github.com/tdhock/animint)。它适用于ggplot2 v 2.1.0,但它为数据子集产生了很好的交互式可视化效果。如果你愿意,你也可以保存它们。例如。 http://bl.ocks.org/tdhock/raw/217823c39eb1fc7c5dc9/。尝试点击图例来更改显示数据的子集。 – fzk