Ggplot 折线图操作

Ggplot Line Chart Manipulation

我有以下数据并想绘制多个 Line ggplots(每个代表不同的 stock1。每个图都有多条线代表因子 1、2、3 等(即第 3 至 6 列)。

数据:

sample_data<-structure(list(Date = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L, 1L, 2L, 3L, 4L), .Label = c("2017-01-31", "2017-02-28", "2017-03-31", 
"2017-04-30"), class = "factor"), stock1 = structure(c(1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("a", "b", 
"c"), class = "factor"), Factor1 = c(5, 6, 8, 9, 5, 6, 7, 2, 
3, 4, 5, 6), Factor2 = c(4, 4.5, 5, 4, 4.5, 6, 2, 3.4, 4, 5, 
4, 3), Factor3 = c(6, 7.8, 8, 8.5, 9, 8.5, 4, 5, 6, 5, 5.5, 6
), Factor4 = c(5, 5.5, 6.2, 7, 5.5, 6, 3.4, 4, 5.6, 6, 7, 4)), .Names = c("Date", 
"stock1", "Factor1", "Factor2", "Factor3", "Factor4"), row.names = c(NA, 
-12L), class = "data.frame")

到目前为止我已经尝试过:

ggplot(sample_data, aes(x=Date, y = [,3:6])) + geom_line() + facet_wrap(~Stock1)

我假设你想如何呈现你的数据,并建议这将帮助你开始:

library(tidyverse)
sample_data %>% 
mutate(Date = as.Date(Date)) %>% 
gather("Factor", "Value", 3:6) %>% 
ggplot(aes(x = Date, y = Value, colour = Factor)) + 
geom_line() + 
facet_wrap(~stock1)