我正在尝试绘制一段时间内不同股票的市场细分图。为此,我想创建一个折线图,以显示随着时间的流逝,大,中,小盘股的数量。
我的数据看起来像这样
ISIN Date Ticker MarketSeg
1 BSP951331318 31-01-2010 UIE Medium
2 BSP951331318 28-02-2010 UIE Medium
3 BSP951331318 31-03-2010 UIE Medium
4 BSP951331318 30-04-2010 UIE Medium
5 BSP951331318 31-05-2010 UIE Medium
6 BSP951331318 30-06-2010 UIE Medium
7 BSP951331318 31-07-2010 UIE Medium
8 BSP951331318 31-08-2010 UIE Medium
到目前为止,我的代码看起来像这样。
CombData <- CombData %>% group_by(Date) %>%
count(CombData$MarketSeg)
ggplot(data = CombData, aes(x=Date, y=, group=CombData$MarketSeg, color=CombData$MarketSeg))
因此,我需要一种方法来对由date变量分组的每个细分中的金额进行计数,以便我可以输入y变量,因为当前代码无法进行计数
答案 0 :(得分:1)
如果我做对了,这应该会为您提供所需的内容(我认为添加带有计数数据的附加列会更容易):
CombData <- CombData %>%
group_by(Date, MarketSeg) %>%
mutate(count_seg = n())
ggplot(data = CombData, aes(x=Date, y= count_seg, group=MarketSeg, color=MarketSeg)) +
geom_line()
数据:
structure(list(ISIN = c("BSP951331318", "BSP951331318", "BSP951331318",
"BSP951331318", "BSP951331318", "BSP951331318", "BSP951331318",
"BSP951331318"), Date = c("31.01.10", "28.02.10", "31.03.10",
"30.04.10", "31.05.10", "30.06.10", "31.07.10", "31.08.10"),
Ticker = c("UIE", "UIE", "UIE", "UIE", "UIE", "UIE", "UIE",
"UIE"), MarketSeg = c("Medium", "Medium", "Medium", "Medium",
"Medium", "Medium", "Medium", "Medium"), count_seg = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L)), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -8L), groups = structure(list(
Date = c("28.02.10", "30.04.10", "30.06.10", "31.01.10",
"31.03.10", "31.05.10", "31.07.10", "31.08.10"), MarketSeg = c("Medium",
"Medium", "Medium", "Medium", "Medium", "Medium", "Medium",
"Medium"), .rows = list(2L, 4L, 6L, 1L, 3L, 5L, 7L, 8L)), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
希望这会有所帮助!