R-在ggplot中使用面板图层创建图例和单独的月份图

R-在ggplot中使用面板图层创建图例和单独的月份图,r,ggplot2,time-series,R,Ggplot2,Time Series,我的目标是为数据集中给定年份(顶部x轴)的12个月(顶部x轴)中的每个月创建一个单独的4面板图(右侧y轴)。我还想创建一个图例,其中点的名称(V5和V6)覆盖在两个面板(V2和V3)中。因此,应该有12个图,而不是1个大图 可复制示例之后是我的当前绘图 你能帮助我实现我的目标吗 多谢各位 library(ggplot2) library(data.table) startdate <- as.POSIXct("2008-09-12 10:00:00") enddate <- as.

我的目标是为数据集中给定年份(顶部x轴)的12个月(顶部x轴)中的每个月创建一个单独的4面板图(右侧y轴)。我还想创建一个图例,其中点的名称(V5和V6)覆盖在两个面板(V2和V3)中。因此,应该有12个图,而不是1个大图

可复制示例之后是我的当前绘图

你能帮助我实现我的目标吗

多谢各位

library(ggplot2)
library(data.table)

startdate <- as.POSIXct("2008-09-12 10:00:00")
enddate <- as.POSIXct("2011-04-26 23:45:00")
interval <- 1296000
Time <- seq(from = startdate, by = interval/2, to = enddate)

set.seed(1)
timeframe <- data.frame(Time, V1 = abs(rnorm(length(Time))), V2 = 
abs(rnorm(length(Time))), V3 = abs(rnorm(length(Time))), V4 = 
abs(rnorm(length(Time))), V5 = abs(rnorm(length(Time))), V6 = 
abs(rnorm(length(Time))))

timeframe <- setDT(timeframe)
以下绘图方法源自


timeframe
ggplot
通常更喜欢使用长格式的数据。因此,从重塑数据开始。然后,使用一个数据集表示直线,一个数据集表示点,并将一个变量映射到点的颜色
aes
thetics,这非常简单

# melt data from wide to long format
library(reshape2)
df <- melt(timeframe, id.vars = "Time")

# create year and month variables 
df$year <- format(df$Time, "%Y")
df$month <- format(df$Time, "%m")

# select data for lines
d1 <- df[!df$variable %in% c("V5", "V6"), ]

# select data for points
d2 <- df[df$variable %in% c("V5", "V6"), ]

# rename V5 and V6 to place them in correct panels
d2$variable[d2$variable == "V5"] <- "V2"
d2$variable[d2$variable == "V6"] <- "V3"

# plot
ggplot() +
  geom_line(data = d1, aes(x = Time, y = value)) +
  geom_point(data = d2, aes(x = Time, y = value, color = variable)) +
  facet_grid(variable ~ month + year, scale = "free") +
  scale_color_manual(values = c("green", "red"), name = "Legend",  
                     labels = c("V5", "V6"))
#将数据从宽格式转换为长格式
图书馆(E2)

df这是我上述问题的完整答案,大部分答案都依赖于亨里克的答案。谢谢你,亨里克

library(ggplot2)
library(reshape2)

startdate <- as.POSIXct("2008-09-12 10:00:00")
enddate <- as.POSIXct("2011-04-26 23:45:00")
interval <- 1296000
Time <- seq(from = startdate, by = interval/2, to = enddate)

set.seed(1)
timeframe <- data.frame(Time, V1 = abs(rnorm(length(Time))), V2 = 
abs(rnorm(length(Time))), V3 = abs(rnorm(length(Time))), V4 = 
abs(rnorm(length(Time))), V5 = abs(rnorm(length(Time))), V6 = 
abs(rnorm(length(Time))))

df <- melt(timeframe, id.vars = "Time")

# create year and month variables 
df$year <- format(df$Time, "%Y")
df$month <- format(df$Time, "%b")

# select data for lines
d1 <- df[!df$variable %in% c("V5", "V6"), ]

# select data for points
d2 <- df[df$variable %in% c("V5", "V6"), ]

# rename V5 and V6 to place them in correct panels
d2$variable[d2$variable == "V5"] <- "V2"
d2$variable[d2$variable == "V6"] <- "V3"
库(ggplot2)
图书馆(E2)
起始日期
# melt data from wide to long format
library(reshape2)
df <- melt(timeframe, id.vars = "Time")

# create year and month variables 
df$year <- format(df$Time, "%Y")
df$month <- format(df$Time, "%m")

# select data for lines
d1 <- df[!df$variable %in% c("V5", "V6"), ]

# select data for points
d2 <- df[df$variable %in% c("V5", "V6"), ]

# rename V5 and V6 to place them in correct panels
d2$variable[d2$variable == "V5"] <- "V2"
d2$variable[d2$variable == "V6"] <- "V3"

# plot
ggplot() +
  geom_line(data = d1, aes(x = Time, y = value)) +
  geom_point(data = d2, aes(x = Time, y = value, color = variable)) +
  facet_grid(variable ~ month + year, scale = "free") +
  scale_color_manual(values = c("green", "red"), name = "Legend",  
                     labels = c("V5", "V6"))
library(ggplot2)
library(reshape2)

startdate <- as.POSIXct("2008-09-12 10:00:00")
enddate <- as.POSIXct("2011-04-26 23:45:00")
interval <- 1296000
Time <- seq(from = startdate, by = interval/2, to = enddate)

set.seed(1)
timeframe <- data.frame(Time, V1 = abs(rnorm(length(Time))), V2 = 
abs(rnorm(length(Time))), V3 = abs(rnorm(length(Time))), V4 = 
abs(rnorm(length(Time))), V5 = abs(rnorm(length(Time))), V6 = 
abs(rnorm(length(Time))))

df <- melt(timeframe, id.vars = "Time")

# create year and month variables 
df$year <- format(df$Time, "%Y")
df$month <- format(df$Time, "%b")

# select data for lines
d1 <- df[!df$variable %in% c("V5", "V6"), ]

# select data for points
d2 <- df[df$variable %in% c("V5", "V6"), ]

# rename V5 and V6 to place them in correct panels
d2$variable[d2$variable == "V5"] <- "V2"
d2$variable[d2$variable == "V6"] <- "V3"
# separate plot for each month
for (u in unique(df$month)) {
p <- ggplot() + geom_line(data = d1[format(d1$Time,"%b")==u, ], aes(x = 
Time, y = value)) + geom_point(data = d2[format(d2$Time,"%b")==u, ], aes(x= Time, 
y = value, color = variable)) + facet_grid(variable ~ month + year, scale = "free") 
+ scale_color_manual(values = c("green", "red"), name = "Legend", 
labels = c("V5", "V6"))
print(p)
}