[英]Plot two Graphs on Same Chart R, ggplot2 par(mfrow())
I am getting the feeling after doing some digging that this probably will not work and I will need to discover an alternate method but I am going to ask anyways. 在做了一些挖掘之后我感觉这可能不起作用,我需要发现一种替代方法,但我还是会问。
I have to graphs that I want to plot on the same chart, by making use of par(mfrow=c(1,2))
我必须通过使用
par(mfrow=c(1,2))
绘制我想在同一图表上绘制的图表
My code for the graphs is as follows: 我的图表代码如下:
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")
If this is not possible, then a direction to a better route would be appreciated. 如果这是不可能的,那么可以理解到更好路线的方向。
Thank you, 谢谢,
Look at the gridExtra
package and use grid.arrange
instead. 查看
gridExtra
包并使用grid.arrange
。 Works wonderfully with ggplot
. 使用
ggplot
非常ggplot
。
Just assign your first ggplot
call to a variable (eg plot1
) and the next to another (eg plot2
) and do something like: 只要你的分配首先
ggplot
调用一个变量(例如plot1
)和下一个到另一个(例如plot2
),并做一些事情,如:
grid.arrange(plot1, plot2, nrow=1, ncol=2)
mfrow
is for use with base graphics. mfrow
用于基本图形。 For ggplot2
you need a different approach, like the one mentioned by @hrbmstr, or this one: 对于
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))
Late to the party, but I just had to deal with this and found a simple solution for multiplots simply by looking in the in the gridExtra
package (builind on @hrbrmstr): 迟到了,但我只需处理这个问题,只需查看
gridExtra
包(@hrbrmstr上的builind)就可以找到一个简单的多点解决方案:
library("ggplot2")
library("gridExtra")
pl <- lapply(1:4, function(.x) qplot(1:10, rnorm(10), main=paste("plot", .x)))
marrangeGrob(pl, nrow=2, ncol=2)
Just put you plot generating function in the lapply
and you're all set. 只需将你的情节生成函数放在
lapply
就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.