从一个数据帧创建多个图形-R
我试图创建一个工作流程,从网站收集信息(股票行情数据、30种不同的行情、与单个行情相关的三种不同价格),清理数据(添加与信息收集日期相关的日期列),将其推入一个主文件从一个数据帧创建多个图形-R,r,dataframe,ggplot2,dplyr,timeserieschart,R,Dataframe,Ggplot2,Dplyr,Timeserieschart,我试图创建一个工作流程,从网站收集信息(股票行情数据、30种不同的行情、与单个行情相关的三种不同价格),清理数据(添加与信息收集日期相关的日期列),将其推入一个主文件tsibbledataframe,该数据框每天保存新的数据点,然后将编译到一页上的各个绘图上的价格范围绘制成图形 下面一天的示例df被推入主df以保存所有数据: df <- data.frame(ticker = c("XLU", "XLK", "XLF", &quo
tsibble
dataframe,该数据框每天保存新的数据点,然后将编译到一页上的各个绘图上的价格范围绘制成图形
下面一天的示例df被推入主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"))
我使用了facet\u wrap\u paginate
按股票代码名称划分facet,并创建了多个图形。但是,在使用facet时,我不能很好地控制轴和单独的绘图,因此我必须使用一种方法,分别绘制每个ticker并编译到相同的页面上。我使用了以下代码:
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)
for(1:4中的i){
rr_图生成了一些随机数据,在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)
添加用于格式化大数字的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!'
)))))}
你能举一个你想用轴做什么的例子吗?所以一些股票数据的价格或值在1.0-1.9之间,也在43000-56000之间,我想能够设置单独的断点,小值的断点更精细,大值的断点更大,并且还可以在+和-方向上扩展y轴s约为该股票代码最大值的10%,因此,当按此处所示进行刻面时,我不会遇到绘制数据位于y轴顶部的问题…(英镑/美元和NYXBT图)()@VitaminB16您是否建议通过嵌套数据框的方式实现这一点,并绘制所有嵌套变量?我不能说我理解问题所在。我已经创建了一个名为rr_tsible
的数据框,包含10天的数据,其中一个标记器的值>100k,其他标记器的值~50。我已经在thfacet_wrap(~ticker,scales=“free_y”)+scale_y_continuous(breaks=scales::pretty_breaks())
而不是分页,在我看来一切正常。我使用pretty_breaks()
只要有可能,但这可能与您的问题无关。我的一些数据被意外地强制为chr字符串,因此无法在y上绘制分类数据。我找到了!我加载了scales
包,但找不到pretty_breaks()
?尝试scales::pretty_breaks()
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)
rr_tsibble$date = as.Date(gsub("st|th|nd","",rr_tsibble$date), "%b %d, %Y")
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)
}
plotlist[[4]]