【发布时间】:2018-03-06 02:17:19
【问题描述】:
最终目标:
为从第 1:167 行开始的日期范围内的 StressCumulative、BaseCumulative、StressQoQ 和 BaseQoQ 的每个区域创建一个图。
问题:
我在对我的 data.frame 进行子集化时遇到了困难。我的问题是我进行子集化的条件是合乎逻辑的,因此只会返回条件之后的第一个元素。
subset_region_1 <- subset.data.frame(HPF, HPF$region == 1, select = BaseCumulative, HPF$StressCumulative, StressQoQ, BaseQoQ)
Warning messages:
1: In if (drop) warningc("drop ignored") :
the condition has length > 1 and only the first element will be used
2: drop ignored
这仅返回第一列 BaseCumulative。
数据:
在这里,您可以了解我正在处理的内容。这是我要从中提取子集的表。我的 data.frame 格式很高
我想创建一个子集,以便在第 1:167 行的日期范围内绘制 BaseCumulative、StressCumulative、BaseQoQ 和 StressQoQ 变量的图表。日期列对所有 100 个区域使用相同的日期。我的问题是,当我在 ggplot 中绘图时,我收到一个错误,即我的 aes 映射大小不同。完整表的 date = 18370 行长,但值每 167 行重复一次(对于每个唯一区域)。此外,BaseCumulative 变量也是 18370 行长,但对所有区域都是唯一的,即每 167 行。我想知道如何按区域进行子集化,同时为我有兴趣测量的变量获取正确的行大小。
数据点:
#Rows 1-3 (Region 1 Sample):
dput(head(HPF[1:3, ]))
structure(list(region = c(1, 1, 1), path = c(1, 1, 1), date = c(20140215,
20140515, 20140815), index_value = c(1, 1.033852765, 1.041697122
), index = 0:2, counter = 1:3, BaseQoQ = c(NA, 0.033852765, 0.00758749917354029
), BaseCumulative = c(100, 103.3852765, 104.1697122), StressCumulative = c(110,
113.3852765, 114.1697122), StressQoQ = c(NA, 0.0307752409090909,
0.00691832065162346)), .Names = c("region", "path", "date", "index_value",
"index", "counter", "BaseQoQ", "BaseCumulative", "StressCumulative",
"StressQoQ"), row.names = c(NA, -3L), class = c("tbl_df", "tbl",
"data.frame"))
#Rows 168:200 (Region 2 Sample):
dput(head(HPF[168:200, ]))
structure(list(region = c(2, 2, 2, 2, 2, 2), path = c(1, 1, 1,
1, 1, 1), date = c(20140215, 20140515, 20140815, 20141115, 20150215,
20150515), index_value = c(1, 1.014162265, 1.01964828, 1.009372314,
1.007210703, 1.018695493), index = 0:5, counter = 1:6, BaseQoQ = c(NA,
0.014162265, 0.00540940556489744, -0.0100779515854232, -0.0021415398163972,
0.0114025694582001), BaseCumulative = c(100, 101.4162265, 101.964828,
100.9372314, 100.7210703, 101.8695493), StressCumulative = c(110,
111.4162265, 111.964828, 110.9372314, 110.7210703, 101.8695493
), StressQoQ = c(NA, 0.0128747863636363, 0.00492389230216839,
-0.00917785181610786, -0.00194849914020834, -0.0799443229370588
)), .Names = c("region", "path", "date", "index_value", "index",
"counter", "BaseQoQ", "BaseCumulative", "StressCumulative", "StressQoQ"
), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))
问题:
除了指定 region == # 之外,我如何对其他列进行子集化?我尝试了以下方法,但问题是日期的值循环并且我的图表不正确:
ggplot(HPF, aes(x = date, y= BaseCumulative, linetype = factor(region == 1))) +
geom_line() +
theme_light()
此外,如果我尝试在 ggplot 中进行子集化,例如:
ggplot(HPF[HPF$region == 1, ], aes(x = HPF$date[1:167, ], y= HPF$BaseCumulative[1:167, ], linetype = factor(region == 1))) +
geom_line() +
theme_light()
感谢任何帮助。
【问题讨论】:
-
屏幕截图没有帮助。请提供一个工作示例。使用
dput。另请澄清这是一个绘图问题还是子集问题?这听起来像是一个子集问题...... -
我的问题是子集 - 我已经编辑了帖子以包含数据
标签: r ggplot2 subset visualization