【发布时间】:2023-03-21 23:45:01
【问题描述】:
我正在尝试创建一个工作流程,从网站收集信息(股票代码数据、30 种不同的代码、与单个代码相关的三种不同价格),清理数据(添加与信息日期相关的日期列已收集),将其推送到主文件 tsibble 数据框中,该数据框每天保存新的数据点,然后将各个图上的价格范围绘制在一个页面上。
下面一天的示例 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"))
第二天的数据:
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 个生成的图。任何关于如何实现这一点的帮助或想法都会很棒!谢谢!
【问题讨论】:
-
你能举个例子说明你想用轴做什么吗?
-
因此,一些股票数据的价格或价值范围为 1.0-1.9,还有 43,000-56,000,我希望能够设置单独的休息时间,为小价值提供更精细的细节更大的值和更大的中断,并且还在 + 和 - 方向上扩展 y 轴大约该股票最大值的 10%,所以我不会遇到绘制数据位于 y 轴顶部的问题刻面如图所示...(GBP/USD 和 NYXBT 数字)(imgur.com/zRjtmyJ)
-
@VitaminB16 你会建议通过嵌套数据框来实现这一点并绘制所有嵌套变量
-
我不能说我理解问题所在。我创建了一个名为
rr_tsibble的数据框,其中包含 10 天的数据,其中一个代码的值 >100k,其他代码的值约为 50。我用facet_wrap(~ticker,scales = "free_y") + scale_y_continuous(breaks = scales::pretty_breaks())而不是分页来运行你的ggplot代码,对我来说一切都很好。我尽可能使用pretty_breaks(),但这可能与您的问题无关。 -
我的一些数据被意外强制转换为 chr 字符串,因此无法在 y 上绘制分类数据。想通了!
标签: r dataframe ggplot2 dplyr timeserieschart