ggplot多个数字gridExtra [英] ggplot multiple figures gridExtra
问题描述
我知道这个主题已经有很多答案了.但是,对于新手来说,还有一些我无法解决的步骤.所以我们开始.希望你能帮帮我.
我想2乘2排列四个不同的地块.我使用的是ggplot,所以我不能使用 par(mfrow = c(2,2))
,但是本质上我是一样的想要做.从我读过的书中,我应该使用gridExtra.所以这是我的代码:
Plot_Graph<-函数(DF,na.rm = TRUE){nm =名称(DF)[-1]对于(i以nm为单位){p<-ggplot(DF,aes(x = Date,y = get(i)))+geom_line()+scale_x_date(minor_breaks ="1年")+xlab(年份")+ylab(股票价格US $")+ggtitle(粘贴(i))+theme_bw()grid.arrange(p)}}
数据样本:
structure(list(Date = structure(c(10960,10961,10962,10963,10966),类=日期"),AAPL = c(1,1.01463414634146,0.926829268292683,0.970731707317073、0.953658536585366),GE = c(1,0.998263888888889,1.01159722222222、1.05076388888889、1.05034722222222),SPY = c(1,1.00178890876565、0.985688729874776、1.04293381037567、1.04651162790698),WMT = c(1,0.976675478152698,0.990359197636448,1.06515316436013,1.04571606282071)),row.names = c(NA,5L),class ="data.frame")
我想我的问题确实是,在执行循环时,我不知道我的图存储在哪里,所以我可以再次访问它们.
您可以使用出色的
或者,您可以使用 tidyr :: pivot_longer
和facet:
库(ggplot2)图书馆(tidyr)DF%>%axis_longer(-Date)%&%;%ggplot(aes(日期,值))+geom_line()+scale_x_date(minor_breaks ="1年")+xlab(年份")+ylab(股票价格US $")+theme_bw()+facet_wrap(〜name)
I know there are a lot of answers already on this topic. However, for a newbiw there are still some steps I can't get around. So here we go. Hope you can help me out.
I want to arrange four different plots 2 by 2. I'm using ggplot, so I can't use par(mfrow=c(2,2))
But it's essentially the same I want to do. From what I've read I should use the gridExtra. SO here is my code:
Plot_Graph <- function(DF, na.rm = TRUE){
nm = names(DF)[-1]
for (i in nm) {
p <- ggplot(DF, aes(x = Date, y = get(i))) +
geom_line() +
scale_x_date(minor_breaks = "1 year") +
xlab("Year") +
ylab("Stock price US$") +
ggtitle(paste(i)) +
theme_bw()
grid.arrange(p)
}
}
Data sample:
structure(list(Date = structure(c(10960, 10961, 10962, 10963,
10966), class = "Date"), AAPL = c(1, 1.01463414634146, 0.926829268292683,
0.970731707317073, 0.953658536585366), GE = c(1, 0.998263888888889,
1.01159722222222, 1.05076388888889, 1.05034722222222), SPY = c(1,
1.00178890876565, 0.985688729874776, 1.04293381037567, 1.04651162790698
), WMT = c(1, 0.976675478152698, 0.990359197636448, 1.06515316436013,
1.04571606282071)), row.names = c(NA, 5L), class = "data.frame")
I guess my problem really is, that I don't know where my plots are stored at, when doing the loop, so I can access them again.
You can use the excellent patchwork package:
library(ggplot2)
library(patchwork)
nm <- names(DF)[-1]
plots <- lapply(nm, function(x) {
ggplot(DF, aes(x = Date, y = get(x))) +
geom_line() +
scale_x_date(minor_breaks = "1 year") +
xlab("Year") +
ylab("Stock price US$") +
ggtitle(x) +
theme_bw()
})
Reduce(`+`, plots) + plot_layout(nrow = 2)
Alternately you can use tidyr::pivot_longer
and facet:
library(ggplot2)
library(tidyr)
DF %>%
pivot_longer(-Date) %>%
ggplot(aes(Date, value)) +
geom_line() +
scale_x_date(minor_breaks = "1 year") +
xlab("Year") +
ylab("Stock price US$") +
theme_bw() +
facet_wrap(~name)
这篇关于ggplot多个数字gridExtra的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!