【问题标题】:ggplot with a lot of groups; subplots (facets) for better arrangement有很多组的ggplot;子图(方面)以更好地安排
【发布时间】:2017-11-08 02:12:06
【问题描述】:

我有一个数据框,其中包含从 1970 年到 2015 年的长格式资产毛利润值、10 个行业类别和 e 时间跨度。我想绘制每个时间序列和(整体)平均值。但问题是,情节变得相当混乱。因此我想把它分成两个或三个子图。我正在使用 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 个行业和平均值)进行精心安排。由于我已经尝试过使用不同的颜色、线型和点,我认为最好的方法是一些子图,但也许有人有更好的主意......?

【问题讨论】:

  • 我认为您的原始情节看起来不错。它是平均值背景下所有行业最直接的可视化。我认为方面会使眼睛更难将平均值与各个组进行比较。也许堆积条加上一条均值线可能会使它更干净一些?
  • 如果要拆分为子组,请在新列中为这些组创建一个虚拟变量,并按虚拟变量分面
  • 如果没有必要使用ggplot2,请尝试使用animint package。它适用于 ggplot2 v 2.1.0,但它为数据子集生成了出色的交互式可视化。如果您愿意,您也可以保存它们。例如。 bl.ocks.org/tdhock/raw/217823c39eb1fc7c5dc9。尝试单击图例以更改显示数据的子集。
  • @neilfws 你说得对,我按照 geryan 的建议和 Adam Quek 的回答做了子组,但是(至少有三个子组)事情变得太小了,线条消失了在“平均线”后面(而且它不再是“整体平均线”)。所以我会尝试一些建议的解决方案的变体,但最终可能会得到一个图,因为“整体平均线”是最重要的
  • @kayB 在下面的答案中添加了一条总体平均线。

标签: r ggplot2 visibility facet subplot


【解决方案1】:

假设我们有如下的数据输入:

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

将几个图强制为一个方面的一种方法是创建一个新组。在这种情况下,我将前三个列出的行业分组为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)

编辑:

可以使用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)

请注意,由于地块之间使用的种子不同,附加表看起来略有不同

【讨论】:

    猜你喜欢
    • 2021-01-21
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多