使用ggplot2将其他系列添加到面板/构面图

时间:2019-06-09 18:05:04

标签: r ggplot2 facet-grid

我有一个数据框,其中包含要用于在ggplot2中进行构面/面板图绘制的4只股票的股票表现数据。数据为长/高格式,“库存”列指示特定的库存。

dat <- structure(list(date = structure(c(15887, 15888, 15889, 15891, 
15894, 15895, 15896, 15897, 15898, 15901, 15902, 15903, 15904, 
15905, 15908, 15909, 15910, 15911, 15912, 15915, 15916, 15917, 
15887, 15888, 15889, 15891, 15894, 15895, 15896, 15897, 15898, 
15901, 15902, 15903, 15904, 15905, 15908, 15909, 15910, 15911, 
15912, 15915, 15916, 15917, 15887, 15888, 15889, 15891, 15894, 
15895, 15896, 15897, 15898, 15901, 15902, 15903, 15904, 15905, 
15908, 15909, 15910, 15911, 15912, 15915, 15916, 15917, 15887, 
15888, 15889, 15891, 15894, 15895, 15896, 15897, 15898, 15901, 
15902, 15903, 15904, 15905, 15908, 15909, 15910, 15911, 15912, 
15915, 15916, 15917), tzone = "UTC", tclass = "Date", class = "Date"), 
    Stock = c("AMZN", "AMZN", "AMZN", "AMZN", "AMZN", "AMZN", 
    "AMZN", "AMZN", "AMZN", "AMZN", "AMZN", "AMZN", "AMZN", "AMZN", 
    "AMZN", "AMZN", "AMZN", "AMZN", "AMZN", "AMZN", "AMZN", "AMZN", 
    "SP500", "SP500", "SP500", "SP500", "SP500", "SP500", "SP500", 
    "SP500", "SP500", "SP500", "SP500", "SP500", "SP500", "SP500", 
    "SP500", "SP500", "SP500", "SP500", "SP500", "SP500", "SP500", 
    "SP500", "XOM", "XOM", "XOM", "XOM", "XOM", "XOM", "XOM", 
    "XOM", "XOM", "XOM", "XOM", "XOM", "XOM", "XOM", "XOM", "XOM", 
    "XOM", "XOM", "XOM", "XOM", "XOM", "XOM", "IBM", "IBM", "IBM", 
    "IBM", "IBM", "IBM", "IBM", "IBM", "IBM", "IBM", "IBM", "IBM", 
    "IBM", "IBM", "IBM", "IBM", "IBM", "IBM", "IBM", "IBM", "IBM", 
    "IBM"), Index = c(1.56722225555556, 1.57627783888889, 1.57794443888889, 
    1.58822225, 1.61438886666667, 1.61961110555556, 1.62405548333333, 
    1.6647778, 1.70861104444444, 1.70316670555556, 1.70483330555556, 
    1.71494445555556, 1.68949991666667, 1.69572228333333, 1.68600006111111, 
    1.67255554444444, 1.66077778888889, 1.68555552222222, 1.73338894444444, 
    1.70055558888889, 1.68005557777778, 1.67344445, 1.28411941552289, 
    1.28341968826429, 1.28447728661051, 1.29758118025531, 1.30439548792506, 
    1.3138258375152, 1.31406441850532, 1.33187557649396, 1.33598638796492, 
    1.3378232084958, 1.33286154225937, 1.33655896278078, 1.34328581696727, 
    1.34544857496443, 1.34818390698232, 1.34568715595456, 1.34055844350659, 
    1.34398554422587, 1.34509875944111, 1.34007341997622, 1.34057436221127, 
    1.34039149509727, 1.23495622668396, 1.2396060623794, 1.24028991081962, 
    1.25232489391446, 1.26162467471452, 1.2765316215865, 1.26942007920869, 
    1.27557430488617, 1.27735227253752, 1.27530082294991, 1.27598467139013, 
    1.27817280040319, 1.29075482942746, 1.30155900020956, 1.29690916451412, 
    1.30196927098047, 1.29909729352719, 1.29882381159093, 1.29636210490856, 
    1.28596820489737, 1.28295943860943, 1.28213889706761, 1.30335244969176, 
    1.30485150261827, 1.31677573305995, 1.32822294658704, 1.32856365932692, 
    1.30348875386647, 1.30996188709328, 1.31370952281649, 1.3087354425162, 
    1.32188611753496, 1.32086408152303, 1.32563375325817, 1.34907339701122, 
    1.31875170069337, 1.32249933641658, 1.32856365932692, 1.33967026232183, 
    1.3438267083615, 1.3447125424064, 1.33694476481823, 1.33558191385875, 
    1.32897246964338)), class = "data.frame", row.names = c(NA, 
-88L))

以下是数据的顶部:

head(dat)
    date Stock  Index
1 2013-07-01  AMZN 1.5672
2 2013-07-02  AMZN 1.5763
3 2013-07-03  AMZN 1.5779
4 2013-07-05  AMZN 1.5882
5 2013-07-08  AMZN 1.6144
6 2013-07-09  AMZN 1.6196

我想用每只股票一个面板制作此数据的面板图。这部分我对下面的代码有所了解:

ggplot(dat, aes(x = date, y = Index, group = Stock, color = Stock)) +
  geom_hline(aes(yintercept = 1), color = 'black') + 
  geom_line() +
  theme_bw() +
  facet_wrap(~ Stock) +
  scale_y_continuous(labels = scales::dollar) +
  labs(x = NULL, y = 'Value of Investment ($)') +
  scale_color_brewer(palette = "Set1") +
  guides(color = FALSE)

这是到目前为止的输出:

enter image description here

我的问题-是否可以将其他Stock系列添加到每个面板中,并使它们像灰色一样变暗?

例如,AMZN的左上方面板看起来类似于此图:

enter image description here

3 个答案:

答案 0 :(得分:3)

当您要绘制这样的图时,您需要一个用于刻面而非着色的变量。

在第4行下面,我们创建了一个变量Stock2,该变量仅用于此目的。

ggplot(dat, aes(date, Index)) +
  geom_hline(aes(yintercept = 1), color = 'black') + 
  geom_line(aes(group = Stock), col = "grey80") +
  geom_line(data = transform(dat, Stock2 = Stock), aes(col = Stock)) +
  facet_wrap(~Stock2) + 
  theme_bw() +
  scale_y_continuous(labels = scales::dollar) +
  labs(x = NULL, y = 'Value of Investment ($)') +
  scale_color_brewer(palette = "Set1") +
  guides(color = FALSE)

enter image description here

答案 1 :(得分:0)

可能有一个更优雅的解决方案,但这是绘制其中一个的一种方法:

ggplot(dat) +
  geom_hline(aes(yintercept = 1), color = 'black') + 
  geom_line(data=subset(dat, Stock == "AMZN"), aes(x=date,y=Index, colour=Stock)) +
  geom_line(data=subset(dat, Stock == "SP500"), aes(x=date,y=Index, colour=Stock)) +
  geom_line(data=subset(dat, Stock == "IBM"), aes(x=date,y=Index, colour=Stock)) +
  geom_line(data=subset(dat, Stock == "XOM"), aes(x=date,y=Index, colour=Stock)) +
  scale_colour_manual(values=c("red", "#D3D3D3", "#D3D3D3", "#D3D3D3")) +
  theme_bw() +
  scale_y_continuous(labels = scales::dollar) +
  labs(x = NULL, y = 'Value of Investment ($)', color = "Stock:")

result

编辑:不过,我知道您想全部绘制它们。 markus的解决方案非常好:)

答案 2 :(得分:0)

# Calculate standard errors for each year range se1970 <- sd(data$Tot[data$Year >= 1970 & data$Year < 1994]) / sqrt(length(data$Tot[data$Year >= 1970 & data$Year < 1994])) se1994 <- sd(data$Tot[data$Year >= 1994 & data$Year < 2005]) / sqrt(length(data$Tot[data$Year >= 1994 & data$Year < 2005])) se2005 <- sd(data$Tot[data$Year >= 2005 & data$Year < 2010]) / sqrt(length(data$Tot[data$Year >= 2005 & data$Year < 2010])) se2010 <- sd(data$Tot[data$Year >= 2010]) / sqrt(length(data$Tot[data$Year >= 2010])) # Calculate means for each year range mean1970 <- mean(data$Tot[data$Year >= 1970 & data$Year < 1994]) mean1994 <- mean(data$Tot[data$Year >= 1994 & data$Year < 2005]) mean2005 <- mean(data$Tot[data$Year >= 2005 & data$Year < 2010]) mean2010 <- mean(data$Tot[data$Year >= 2010]) # 1970-1993 polygon(x = c(1970, 1970, 1993, 1993), y = c(mean1970 + se1970, mean1970 - se1970, mean1970 - se1970, mean1970 + se1970), col = alpha("#4286f4", .25)) lines(x = 1970:1993, y = rep(mean1970, length(1970:1993)), col = "red", lwd = 2) # 1993-2004 polygon(x = c(1994, 1994, 2004, 2004), y = c(mean1994 + se1994, mean1994 - se1994, mean1994 - se1994, mean1994 + se1994), col = alpha("#4286f4", .25)) lines(x = 1994:2004, y = rep(mean1994, length(1994:2004)), col = "red", lwd = 2) # 2005-2009 polygon(x = c(2005, 2005, 2009, 2009), y = c(mean2005 + se2005, mean2005 - se2005, mean2005 - se2005, mean2005 + se2005), col = alpha("#4286f4", .25)) lines(x = 2005:2009, y = rep(mean2005, length(2005:2009)), col = "red", lwd = 2) # 2010-2016 polygon(x = c(2010, 2010, 2016, 2016), y = c(mean2010 + se2010, mean2010 - se2010, mean2010 - se2010, mean2010 + se2010), col = alpha("#4286f4", .25)) lines(x = 2010:2016, y = rep(mean2010, length(2010:2016)), col = "red", lwd = 2) heds1 解决方案配合使用:

grid.arrange

enter image description here