使用 ggplot2 将其他系列添加到 panel/facet 图中

Add other series to panel/facet plot using ggplot2

我有一个数据框,其中包含 4 只股票的股票表现数据,我想用它在 ggplot2 中制作 facet/panel 图。数据采用 long/tall 格式,股票列指示特定股票。

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)

这是目前的输出:

我的问题 - 有没有办法将其他 Stock 系列添加到每个面板中,并使它们成为灰色等暗淡的颜色?

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

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

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:")

edit:我知道你想把它们都画出来。 markus 的解决方案非常好:)

当你想画这样的图时,你需要一个不同的变量,你用它来分面而不是着色。

在下面的第 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)

grid.arrangeheds1结合使用解决方案:

a <- 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:")

b <- 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("#D3D3D3", "blue", "#D3D3D3", "#D3D3D3")) +
  theme_bw() +
  scale_y_continuous(labels = scales::dollar) +
  labs(x = NULL, y = 'Value of Investment ($)', color = "Stock:")

c <- 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("#D3D3D3", "#D3D3D3", "green", "#D3D3D3")) +
  theme_bw() +
  scale_y_continuous(labels = scales::dollar) +
  labs(x = NULL, y = 'Value of Investment ($)', color = "Stock:")

d <- 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("#D3D3D3", "#D3D3D3", "#D3D3D3", "orange")) +
  theme_bw() +
  scale_y_continuous(labels = scales::dollar) +
  labs(x = NULL, y = 'Value of Investment ($)', color = "Stock:")



install.packages("gridExtra")
library(gridExtra)
grid.arrange(a, b, c, d, ncol=2, nrow = 2)