如何将具有不同x轴和y轴间隔的4个图合并为一个图

时间:2015-04-14 12:12:29

标签: r ggplot2

我有4张图,下面显示了两张图,其中包含数据框和结果图

这是我的数据框(h1):

h2 <- structure(list(Tool.Module = structure(c(4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("", "M_AUB01", 
"M_ETR01", "M_FRA01", "M_FRA01_01", "M_FRA01_02", "M_FRA01_03", 
"M_FRA01_04", "M_KPR01_00", "M_KPR02_00", "M_LAM01", "M_LAM01_01", 
"M_LAM02", "M_LAM02_01", "M_LAY01", "M_LOT01_01", "M_LOT01_02_1", 
"M_LOT01_02_2", "M_LOT01_03_1", "M_LOT01_03_2", "M_LOT01_04", 
"M_TAB01_1", "M_TAB01_2"), class = "factor"), end1 = structure(c(1428984210, 
1428984310, 1428985632, 1428985772, 1428985881, 1428985990, 1428986230, 
1428986332, 1428986460, 1428986580, 1428986700, 1428986780, 1428986923, 
1428987020, 1428988400), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("Tool.Module", 
"end1"), row.names = c(7L, 37L, 102L, 111L, 118L, 123L, 140L, 
147L, 156L, 167L, 174L, 180L, 188L, 191L, 280L), class = "data.frame")

看起来像这样:

> h1
    Tool.Module                end1
7       M_FRA01 2015-04-14 06:03:30
37      M_FRA01 2015-04-14 06:05:10
102     M_FRA01 2015-04-14 06:27:12
111     M_FRA01 2015-04-14 06:29:32
118     M_FRA01 2015-04-14 06:31:21
123     M_FRA01 2015-04-14 06:33:10
140     M_FRA01 2015-04-14 06:37:10
147     M_FRA01 2015-04-14 06:38:52
156     M_FRA01 2015-04-14 06:41:00
167     M_FRA01 2015-04-14 06:43:00
174     M_FRA01 2015-04-14 06:45:00
180     M_FRA01 2015-04-14 06:46:20
188     M_FRA01 2015-04-14 06:48:43
191     M_FRA01 2015-04-14 06:50:20
280     M_FRA01 2015-04-14 07:13:20

以下是情节的命令:

   plot(h1$end1, seq_along(h1$end1), type = "b")

enter image description here

我的第二个数据框(h2):

h2 <- structure(list(Tool.Module = structure(c(3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("M_AUB02", 
    "M_ETR02", "M_FRA02", "M_FRA02_01", "M_FRA02_02", "M_FRA02_03", 
    "M_FRA02_04", "M_KPR03_00", "M_KPR04_00", "M_LAM03", "M_LAM03_01", 
    "M_LAM04", "M_LAM04_01", "M_LAY02", "M_LOT02_01", "M_LOT02_02_1", 
    "M_LOT02_02_2", "M_LOT02_03_1", "M_LOT02_03_2", "M_LOT02_04", 
    "M_TAB02_1", "M_TAB02_2"), class = "factor"), end2 = structure(c(1428984300, 
    1428984380, 1428984480, 1428984570, 1428984660, 1428984740, 1428984830, 
    1428984920, 1428985020, 1428985120, 1428985183, 1428985270, 1428985360, 
    1428985450, 1428985540), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("Tool.Module", 
    "end2"), row.names = c(17L, 24L, 34L, 44L, 52L, 60L, 69L, 79L, 
    89L, 99L, 107L, 114L, 124L, 132L, 140L), class = "data.frame")

以下是情节的命令:

plot(h2$end2, seq_along(h2$end2), type = "b")

enter image description here

我想在一个方框中显示上面的两个图,我尝试了线概念。这是情节的命令:

plot(h2$end2, seq_along(h2$end2), type = "b")
    lines(h1$end1,seq_along(h1$end1), type = "b", col = "red")

graph using lines concept

但这些不是我想要的实际图形。实际上我想在一个方框中显示4个图形(与上面两个相同)。

1 个答案:

答案 0 :(得分:1)

你的X轴看起来和我一样。由于我没有你的数据,我在这里给出一个例子

library(wikipediatrend)

views1 <-wp_trend(page = "European debt crisis",from = "2010-01-01",to = "2014-12-31",lang = "en",friendly = TRUE,requestFrom = "wp.trend.tester at wptt.wptt",userAgent = TRUE)



views2 <-wp_trend(page = "National debt of the United States",from = "2010-01-01",to = "2014-12-31",lang = "en",friendly = TRUE,requestFrom = "wp.trend.tester at wptt.wptt",userAgent = TRUE)


views3 <-wp_trend(page = "Arab Spring",from = "2010-01-01",to = "2014-12-31",lang = "en",friendly = TRUE,requestFrom = "wp.trend.tester at wptt.wptt",userAgent = TRUE)


views4 <-wp_trend(page = "Greek government-debt crisis",from = "2010-01-01",to = "2014-12-31",lang = "en",friendly = TRUE,requestFrom = "wp.trend.tester at wptt.wptt",userAgent = TRUE)

combview1<-cbind(views1,views2[,2],views3[,2],views4[,2])

library(ggplot2)
library(reshape2)

meltdf1 <- melt(combview1,id="Time")

ggplot(meltdf1,aes(x=Time,y=value,colour=variable,group=variable)) + geom_line()

enter image description here