我在做一些挖掘後感覺這可能不起作用,我需要發現另一種方法,但是我會反正問。在同一圖表上繪製兩張圖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")
如果這是不可能的,那麼一個更好的路線方向將不勝感激。
謝謝
謝謝你,這是一個相當簡單的解決方案,做了我想要的。 –
如果我想選擇'plot1'和'plot2'進行堆疊,然後說'plot3'和'plot4'堆疊在它旁邊,該怎麼辦?與'par(mfcol)'類似' – Bonono
@Bonono只需將'nrow'和'ncol'設置爲合理的值,然後嘗試確定這些圖是從左到右還是從上到下「填充」佈局。 –