从一个数据框创建多个图 - R
Create Multiple Graphs from One Dataframe - R
我正在尝试创建一个工作流程,用于从网站收集信息(股票行情数据、30 种不同的行情,以及与单个行情相关的三种不同价格)、清理数据(添加相对于当天的日期列信息已收集),将其推入一个主文件 tsibble
数据框,该文件每天保存新的数据点,然后将各个地块的价格范围绘制在一页上。
下面一天的 df 示例被推送到 master df 以保存所有数据:
df <- data.frame(ticker = c("XLU", "XLK", "XLF", "XLE", "XLP"),
buy_price = c(62.00, 68.00, 37.00, 55.00, 41.00),
sale_price = c(64.00, 71.00, 42.00, 60.00, 45.00),
close_price = c(63.00, 70.00, 38.00, 56.00, 43.00),
date = c("April 29th, 2021", "April 29th, 2021", "April 29th, 2021", "April 29th, 2021", "April 29th, 2021"))
第二天的数据:
df2 <- data.frame(ticker = c("XLU", "XLK", "XLF", "XLE", "XLP"),
buy_price = c(63.00, 69.00, 38.00, 53.00, 44.00),
sale_price = c(66.00, 77.00, 47.00, 63.00, 48.00),
close_price = c(65.00, 74.00, 39.00, 55.00, 45.00),
date = c("April 30th, 2021", "April 30th, 2021", "April 30th, 2021", "April 30th, 2021", "April 30th, 2021"))
DF 主文件:rbind(df, df2)
ticker buy_price sale_price close_price date
1 XLU 62 64 63 April 29th, 2021
2 XLK 68 71 70 April 29th, 2021
3 XLF 37 42 38 April 29th, 2021
4 XLE 55 60 56 April 29th, 2021
5 XLP 41 45 43 April 29th, 2021
6 XLU 63 66 65 April 30th, 2021
7 XLK 69 77 74 April 30th, 2021
8 XLF 38 47 39 April 30th, 2021
9 XLE 53 63 55 April 30th, 2021
10 XLP 44 48 45 April 30th, 2021
我曾使用 facet_wrap_paginate
按股票代码名称分面,并创建多个图表。但是,我无法精确控制使用分面时所需的轴和单独的绘图,因此我必须使用一种方法来单独绘制每个自动收报机并编译到相同的页面上。我使用了下面的代码:
for(i in 1:4){
rr_plot <- ggplot(rr_tsibble, aes(x = DATE, color = TREND)) +
geom_point(aes(y = BUY.TRADE), size = 1.5) +
geom_point(aes(y = SELL.TRADE), size = 1.5) +
geom_point(aes(y = PREV.CLOSE), color = "black", size = 1, shape = 1) +
ggforce::facet_wrap_paginate(~TICKER,
nrow = 2,
ncol = 4,
scales = "free_y",
page = i) +
scale_y_continuous()
print(rr_plot)
实现这个。原始数据帧有大约 30 个独立代码,第二天将相同的 30 个添加到 df,然后再添加 30 个。我试过使用 dplyr
到 group_by
和绘图,尽管我还没有达到预期的结果。我不认为用 ggplot2
手动创建 30 个图是非常有效的,必须有一个 for 循环可以只允许选择某些代码然后绘制所有数据并使用 cowplot
和extraGrid
编译所有 30 个生成的图。关于如何实现这一点的任何帮助或想法都会很棒!谢谢!
在 4 天内使用大约 30 个随机代码生成了一些随机数据:
r <- function() {abs(c(rnorm(29,50,2),100000)*rnorm(1,10,1))}
tickers = sapply(1:30, function(x) toupper(paste0(sample(letters, 3), collapse = "")))
df <- data.frame(ticker = tickers,
buy_price = r(),
sale_price = r(),
close_price = r(),
date = rep("April 29th, 2021",30))
df2 <- data.frame(ticker = tickers,
buy_price = r(),
sale_price = r(),
close_price = r(),
date = rep("April 30th, 2021",30))
df3 <- data.frame(ticker = tickers,
buy_price = r(),
sale_price = r(),
close_price = r(),
date = rep("May 1st, 2021",30))
df4 <- data.frame(ticker = tickers,
buy_price = r(),
sale_price = r(),
close_price = r(),
date = rep("May 2nd, 2021",30))
rr_tsibble <- rbind(df, df2, df3, df4)
已将 date
转换为日期格式:
rr_tsibble$date = as.Date(gsub("st|th|nd","",rr_tsibble$date), "%b %d, %Y")
添加addUnits()
格式化大数的功能:
addUnits <- function(n) {
labels <- ifelse(n < 1000, n, # less than thousands
ifelse(n < 1e6, paste0(round(n/1e3,3), 'k'), # in thousands
ifelse(n < 1e9, paste0(round(n/1e6,3), 'M'), # in millions
ifelse(n < 1e12, paste0(round(n/1e9), 'B'), # in billions
ifelse(n < 1e15, paste0(round(n/1e12), 'T'), # in trillions
'too big!'
)))))}
制作地块列表:
plotlist <- list()
for (i in 1:ceiling(30/8))
{
plotlist[[i]] <- ggplot(rr_tsibble, aes(x = date)) +
geom_point(aes(y = buy_price), size = 1.5) +
geom_point(aes(y = sale_price), size = 1.5) +
geom_point(aes(y = close_price), color = "black", size = 1, shape = 1) +
scale_y_continuous(breaks = pretty_breaks(), labels = addUnits) +
ggforce::facet_wrap_paginate(~ticker,
nrow = 2,
ncol = 4,
scales = "free_y",
page = i)
}
一共有4页,每页都存储为plotlist
列表的一个元素。例如,最后一页是第 4 个元素,看起来像这样:
plotlist[[4]]
我正在尝试创建一个工作流程,用于从网站收集信息(股票行情数据、30 种不同的行情,以及与单个行情相关的三种不同价格)、清理数据(添加相对于当天的日期列信息已收集),将其推入一个主文件 tsibble
数据框,该文件每天保存新的数据点,然后将各个地块的价格范围绘制在一页上。
下面一天的 df 示例被推送到 master df 以保存所有数据:
df <- data.frame(ticker = c("XLU", "XLK", "XLF", "XLE", "XLP"),
buy_price = c(62.00, 68.00, 37.00, 55.00, 41.00),
sale_price = c(64.00, 71.00, 42.00, 60.00, 45.00),
close_price = c(63.00, 70.00, 38.00, 56.00, 43.00),
date = c("April 29th, 2021", "April 29th, 2021", "April 29th, 2021", "April 29th, 2021", "April 29th, 2021"))
第二天的数据:
df2 <- data.frame(ticker = c("XLU", "XLK", "XLF", "XLE", "XLP"),
buy_price = c(63.00, 69.00, 38.00, 53.00, 44.00),
sale_price = c(66.00, 77.00, 47.00, 63.00, 48.00),
close_price = c(65.00, 74.00, 39.00, 55.00, 45.00),
date = c("April 30th, 2021", "April 30th, 2021", "April 30th, 2021", "April 30th, 2021", "April 30th, 2021"))
DF 主文件:rbind(df, df2)
ticker buy_price sale_price close_price date
1 XLU 62 64 63 April 29th, 2021
2 XLK 68 71 70 April 29th, 2021
3 XLF 37 42 38 April 29th, 2021
4 XLE 55 60 56 April 29th, 2021
5 XLP 41 45 43 April 29th, 2021
6 XLU 63 66 65 April 30th, 2021
7 XLK 69 77 74 April 30th, 2021
8 XLF 38 47 39 April 30th, 2021
9 XLE 53 63 55 April 30th, 2021
10 XLP 44 48 45 April 30th, 2021
我曾使用 facet_wrap_paginate
按股票代码名称分面,并创建多个图表。但是,我无法精确控制使用分面时所需的轴和单独的绘图,因此我必须使用一种方法来单独绘制每个自动收报机并编译到相同的页面上。我使用了下面的代码:
for(i in 1:4){
rr_plot <- ggplot(rr_tsibble, aes(x = DATE, color = TREND)) +
geom_point(aes(y = BUY.TRADE), size = 1.5) +
geom_point(aes(y = SELL.TRADE), size = 1.5) +
geom_point(aes(y = PREV.CLOSE), color = "black", size = 1, shape = 1) +
ggforce::facet_wrap_paginate(~TICKER,
nrow = 2,
ncol = 4,
scales = "free_y",
page = i) +
scale_y_continuous()
print(rr_plot)
实现这个。原始数据帧有大约 30 个独立代码,第二天将相同的 30 个添加到 df,然后再添加 30 个。我试过使用 dplyr
到 group_by
和绘图,尽管我还没有达到预期的结果。我不认为用 ggplot2
手动创建 30 个图是非常有效的,必须有一个 for 循环可以只允许选择某些代码然后绘制所有数据并使用 cowplot
和extraGrid
编译所有 30 个生成的图。关于如何实现这一点的任何帮助或想法都会很棒!谢谢!
在 4 天内使用大约 30 个随机代码生成了一些随机数据:
r <- function() {abs(c(rnorm(29,50,2),100000)*rnorm(1,10,1))}
tickers = sapply(1:30, function(x) toupper(paste0(sample(letters, 3), collapse = "")))
df <- data.frame(ticker = tickers,
buy_price = r(),
sale_price = r(),
close_price = r(),
date = rep("April 29th, 2021",30))
df2 <- data.frame(ticker = tickers,
buy_price = r(),
sale_price = r(),
close_price = r(),
date = rep("April 30th, 2021",30))
df3 <- data.frame(ticker = tickers,
buy_price = r(),
sale_price = r(),
close_price = r(),
date = rep("May 1st, 2021",30))
df4 <- data.frame(ticker = tickers,
buy_price = r(),
sale_price = r(),
close_price = r(),
date = rep("May 2nd, 2021",30))
rr_tsibble <- rbind(df, df2, df3, df4)
已将 date
转换为日期格式:
rr_tsibble$date = as.Date(gsub("st|th|nd","",rr_tsibble$date), "%b %d, %Y")
添加addUnits()
格式化大数的功能:
addUnits <- function(n) {
labels <- ifelse(n < 1000, n, # less than thousands
ifelse(n < 1e6, paste0(round(n/1e3,3), 'k'), # in thousands
ifelse(n < 1e9, paste0(round(n/1e6,3), 'M'), # in millions
ifelse(n < 1e12, paste0(round(n/1e9), 'B'), # in billions
ifelse(n < 1e15, paste0(round(n/1e12), 'T'), # in trillions
'too big!'
)))))}
制作地块列表:
plotlist <- list()
for (i in 1:ceiling(30/8))
{
plotlist[[i]] <- ggplot(rr_tsibble, aes(x = date)) +
geom_point(aes(y = buy_price), size = 1.5) +
geom_point(aes(y = sale_price), size = 1.5) +
geom_point(aes(y = close_price), color = "black", size = 1, shape = 1) +
scale_y_continuous(breaks = pretty_breaks(), labels = addUnits) +
ggforce::facet_wrap_paginate(~ticker,
nrow = 2,
ncol = 4,
scales = "free_y",
page = i)
}
一共有4页,每页都存储为plotlist
列表的一个元素。例如,最后一页是第 4 个元素,看起来像这样:
plotlist[[4]]