[英]Plot two Graphs on Same Chart R, ggplot2 par(mfrow())
在做了一些挖掘之后我感覺這可能不起作用,我需要發現一種替代方法,但我還是會問。
我必須通過使用par(mfrow=c(1,2))
繪制我想在同一圖表上繪制的圖表
我的圖表代碼如下:
mTotal <- mean(data$Total)
mTotal
data$valence1[data$Total >= mTotal] <- "Above Mean"
data$valence1[data$Total < mTotal] <- "Below Mean"
data$valence2[data$Delta >= 0] <- "Positive"
data$valence2[data$Delta < 0] <- "Negative"
data
par(mfrow=c(1,2))
ggplot(data,
aes(x = Index,
y = Total,
fill = valence1)) +
geom_bar(stat = "identity",
colour = "black",
alpha = 0.618) +
geom_hline(yintercept = mTotal,
linetype = "dashed",
colour = "red") +
annotate("text", x = 19, y = mTotal + 50,
label = "Problem Period") +
xlab("Date") +
ylab("Ambulance Arrivals") +
ggtitle("Ambulance Arrivals by Month
Jan 2013 - Feb 2014")
maxDelta <- max(data$Delta)
maxDelta
minDelta <- min(data$Delta)
minDelta
ggplot(data,
aes(x = Index,
y = Delta,
fill = valence2)) +
geom_bar(stat = "identity",
position = "identity",
colour = "black",
alpha = 0.618) +
annotate("rect", xmin = 13.5, xmax = 24.5,
ymin = minDelta, ymax = maxDelta,
alpha = 0.3, fill = "blue") +
annotate("text", x = 19, y = maxDelta + 25,
label = "Problem Period") +
xlab("Date") +
ylab("Change in Arrivals") +
ggtitle("Change in Ambulance Arrivals Month over Month")
如果這是不可能的,那么可以理解到更好路線的方向。
謝謝,
查看gridExtra
包並使用grid.arrange
。 使用ggplot
非常ggplot
。
只要你的分配首先ggplot
調用一個變量(例如plot1
)和下一個到另一個(例如plot2
),並做一些事情,如:
grid.arrange(plot1, plot2, nrow=1, ncol=2)
mfrow
用於基本圖形。 對於ggplot2
你需要一個不同的方法,比如@hrbmstr提到的方法,或者這個方法:
library("ggplot2")
library("grid")
a <- qplot(x = rnorm(10))
b <- qplot(x = rnorm(10))
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
print(a, vp = vplayout(1,1))
print(b, vp = vplayout(1,2))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.