[英]Plot multiple plots with a for loop in R
I am trying to plot some line graphs from a dataset but with varied y-axis values that are set in the list var.我正在尝试 plot 来自数据集的一些线图,但在列表 var 中设置了不同的 y 轴值。 For some reason, all the plots seem to display an empty plot but when I have the same code without the for loop it seems to work.
出于某种原因,所有图似乎都显示一个空的 plot 但是当我有相同的代码而没有 for 循环时,它似乎可以工作。 So my question is can I possibly use a for loop to plot multiple plots at once instead of individually plotting them.
所以我的问题是我是否可以一次对 plot 多个图使用 for 循环,而不是单独绘制它们。 Am I missing something while plotting this.
我在绘制这个时是否遗漏了一些东西。
var = list("CAH", 'CTLT',"CI","DVA","HSIC","HOLX","HUM","IDXX","INCY","MRK","REGN","VTRS")
for(v in var){
p<-closing_price %>%
mutate(date = as.Date(Date,"%m/%d/%y")) %>%
ggplot(aes(x = date, y = v, group=1)) +
geom_line() +
labs(x = "Date", y = "Stock Price ($)", title = glue("{v} price over time")) +
scale_x_date(date_minor_breaks = "2 day")
print(p)
}
This is the outcome I am looking for:这是我正在寻找的结果:
closing_price %>%
mutate(date = as.Date(Date,"%m/%d/%y")) %>%
select(date,CAH) %>%
ggplot(aes(x = date, y = CAH, group=1)) +
geom_line() +
labs(x = "Date", y = "Stock Price ($)", title = "CAH price over time") +
scale_x_date(date_minor_breaks = "2 day")
As noted by stefan, ggplot thinks you're trying to plot the character which is why you're getting "CAH" on the y-axis.正如 stefan 所指出的,ggplot 认为您正在尝试 plot 字符,这就是您在 y 轴上获得“CAH”的原因。 You can refer to the variable with
.data[[v]]
.您可以使用
.data[[v]]
引用变量。
var = list("CAH", 'CTLT',"CI","DVA","HSIC","HOLX","HUM","IDXX","INCY","MRK","REGN","VTRS")
for(v in var){
p<-closing_price %>%
mutate(date = as.Date(Date,"%m/%d/%y")) %>%
ggplot(aes(x = date, y = .data[[v]], group=1)) +
geom_line() +
labs(x = "Date", y = "Stock Price ($)", title = glue("{v} price over time")) +
scale_x_date(date_minor_breaks = "2 day")
print(p)
}
Since I don't have access to your data, here's a reproducible example:由于我无权访问您的数据,因此这是一个可重现的示例:
library(tidyverse)
library(zoo)
closing_price <-
tibble(
dt = as.Date(seq(as.yearmon("2020-01-31"), as.yearmon("2020-12-31"), by = 1 / 12), frac = 1),
CAH = rnorm(12, mean = 10, sd = 2),
CTLT = rnorm(12, mean = 50, sd = 5),
CI = rnorm(12, mean = 25, sd = 2)
)
vars <- c("CAH", "CTLT","CI")
for(v in vars){
p <-
ggplot(data = closing_price, aes(x = dt, y = .data[[v]])) +
geom_line() +
labs(x = "Date", y = "Stock Price ($)", title = paste(v, "price over time")) +
scale_x_date(date_minor_breaks = "2 day")
print(p)
}
You could also use lapply
for this by wrapping your plot in a function instead.您也可以通过将
lapply
包装在 function 中来使用 lapply。 The function may be useful depending on how often you make these plots or to make it easier to fix and share. function 可能有用,具体取决于您绘制这些图的频率或使其更易于修复和共享。
closing_price_plot <- function(data, var){
p <-
data %>%
ggplot(aes(x = dt, y = .data[[var]], group=1)) +
geom_line() +
labs(x = "Date", y = "Stock Price ($)", title = paste(var, "price over time")) +
scale_x_date(date_minor_breaks = "2 day")
return(p)
}
## prints out the plots as in the loop
lapply(vars, FUN = closing_price_plot, data = closing_price)
## save them to a list instead
p <- list()
p <- lapply(vars, FUN = closing_price_plot, data = closing_price)
## view the first plot
p[[1]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.