首页 文章

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

提问于
浏览
1

我的目标是为数据集中给定年份(顶部x轴)的12个月(顶部x轴)中的每一个创建单独的4面板图(右y轴) . 我还想创建一个图例,其中两个面板(V2和V3)中的点(V5和V6)的名称重叠 . 因此,应该有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)

以下Month和Year函数派生自 smwrBase 中的waterYear函数 .

Month <- function (x, numeric = FALSE) 
{
x <- as.POSIXlt(x)
yr <- x$year + 1900L
mn <- x$mon + 1L
if (numeric) 
return(mn)
ordered(mn)
}

Year <- function (x, numeric = FALSE) 
{
x <- as.POSIXlt(x)
yr <- x$year + 1900L
mn <- x$mon + 1L
if (numeric) 
return(yr)
ordered(yr)
}

# month
mn <- Month(timeframe$Time, numeric = TRUE)

# year
yr <- Year(timeframe$Time, numeric = TRUE)

以下绘图方法源自Add a geom layer for a single panel in a faceted plot

timeframe <- data.table(timeframe, mn, yr)
setnames(timeframe, 8:9, c("Month", "Year"))
setkey(timeframe, Time)

df1 <- setDF(timeframe[, list(Time, V1, Month, Year)])
df2 <- setDF(timeframe[, list(Time, V2, Month, Year)])
df3 <- setDF(timeframe[, list(Time, V3, Month, Year)])
df4 <- setDF(timeframe[, list(Time, V4, Month, Year)])
df5 <- setDF(timeframe[, list(Time, V5, Month, Year)])
df6 <- setDF(timeframe[, list(Time, V6, Month, Year)])

names(df1) <- c("Time", "value", "Month", "Year")
names(df2) <- c("Time", "value", "Month", "Year")
names(df3) <- c("Time", "value", "Month", "Year")
names(df4) <- c("Time", "value", "Month", "Year")
names(df5) <- c("Time", "value", "Month", "Year")
names(df6) <- c("Time", "value", "Month", "Year")

df1$panel <- "V1"
df2$panel <- "V2"
df3$panel <- "V3"
df4$panel <- "V4"
df5$panel <- "V2"
df6$panel <- "V3"

dff <- rbind(df1, df2, df3, df4)

p <- ggplot(data = dff, mapping = aes(x = Time, y = value))
p <- p + facet_grid(panel ~ Month + Year, scale = "free")
p <- p + layer(data = df1, geom = "line")
p <- p + layer(data = df2, geom = "line")
p <- p + layer(data = df5, geom = "point", colour = "green")
p <- p + layer(data = df3, geom = "line")
p <- p + layer(data = df6, geom = "point", colour = "red")
p <- p + layer(data = df4, geom = "line") +  
scale_fill_manual(values=c("green", "red"), name="Legend",  
labels=c("v5", "v6")) # this last part is my attempt at creating the legend
p

对facet_grid的帮助来自http://docs.ggplot2.org/current/facet_grid.html


enter image description here

2 回答

  • 0

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

    enter image description here

  • 1

    这是我上面问题的完整答案,其中大部分都依赖于Henrik的答案 . 谢谢Henrik .

    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"
    

    以下代码的来源:Selecting and plotting months in ggplot2

    # 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)
    }
    

相关问题