2017-06-06 36 views
1

我有一個數據框,其中包含資產毛利,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') 

enter image description here

我試着用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~.) 

所有我設法得到如下,這顯然是沒有用的: enter image description here

我試圖拆爲了每個子區有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個行業和均值)的情節。由於我已經嘗試過不同的顏色,線型和點數,我認爲最好的方法是一些子圖,但也許有人有更好的想法......?

+1

我認爲你的原始情節看起來不錯。這是所有行業在平均價值背景下最直接的可視化。我認爲方面會讓眼睛比較平均值與個人羣體更加困難。也許堆積的酒吧加上一條線的意思可能會使它更清潔一點? – neilfws

+0

如果您想要分成多個子組,請在新列中爲這些組創建一個虛擬變量,然後使用虛擬變量構建面 –

+0

如果不需要使用'ggplot2',請嘗試使用['animint'包] (https://github.com/tdhock/animint)。它適用於ggplot2 v 2.1.0,但它爲數據子集產生了很好的交互式可視化效果。如果你願意,你也可以保存它們。例如。 http://bl.ocks.org/tdhock/raw/217823c39eb1fc7c5dc9/。嘗試點擊圖例來更改顯示數據的子集。 – fzk

回答

0

認爲我們有一個數據輸入如下:

time <- 1970:2011 
industry <- letters[1:10] 

dat <- expand.grid(time=time, industry=industry) 
dat$value <- rnorm(nrow(dat)) 

該數據的ggplot會在這個問題同樣令人困惑:

ggplot(dat, aes(time, value, colour=industry)) + 
    geom_line() 

enter image description here

之一通過創建一個新組來強制將一些情節強制爲單個方面。在這種情況下,我在分組的前三個行業上市爲group_one,未來三年爲group_two,其餘爲group_three

library(tidyverse) 
dat2 <- dat %>% 
    mutate(group_one = ifelse(industry %in% letters[1:3], value, NA), 
      group_two = ifelse(industry %in% letters[4:6], value, NA), 
      group_three = ifelse(industry %in% letters[7:10], value, NA)) %>% 
    gather(variable, new_val, group_one:group_three) 

與小新的情節現在看起來稍微整潔:

ggplot(dat2, aes(time, new_val, colour=industry)) + geom_line() + 
    facet_wrap(~variable, ncol=1) 

enter image description here

編輯:

疊加在所有方面CA附加線n用annotate函數完成。

首先,生成與平均值彙總表每個時間點:

dat3 <- dat %>% 
    group_by(time) %>% 
    summarise(mean.value=mean(value)) 

添加註釋到ggplot以上:

ggplot(dat2, aes(time, new_val, colour=industry)) + 
    geom_line() + 
    facet_wrap(~variable, ncol=1) + 
    annotate(geom="line", x=dat3$time, y=dat3$mean.value, 
      color='red', size=1.5) 

enter image description here

注意,附加由於在地塊之間使用的不同種子,表格看起來有點不同