Reputation: 529
I have a dataframe with stock performance data on 4 stocks that I want to use to make a facet/panel plot in ggplot2. The data is in long/tall format with the particular Stock indicated by the Stock column.
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))
Here is the top of the data:
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
I would like to make a panel plot of this data with one panel for each stock. This part I have down with this code:
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)
Here is the output so far:
My question - Is there a way possible to add the other Stock series into each panel, and make them a dull color like grey?
For example the top left panel for AMZN would look similar to this plot:
Upvotes: 3
Views: 150
Reputation: 3438
There is probably a more elegant solution, but here is one way to plot one of them:
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: I understand you want to plot them all, though. markus's solution is excellent :)
Upvotes: 0
Reputation: 26343
When you want to draw a plot like this you need a different variable that you use for facetting but not for coloring.
Below in line 4 we create a variable called Stock2
only used for this purpose.
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)
Upvotes: 3
Reputation: 1111
Using grid.arrange
with heds1 solution:
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)
Upvotes: 0