[英]Plotting grouped variables in line plot r
I am trying to construct a graph of the market segment of different stocks over time.我正在尝试构建不同股票的细分市场图。 For this, I would like to create a line plot that shows how many stocks are in small, medium and large cap over time.为此,我想创建一个线图,显示随着时间的推移有多少股票处于小盘、中盘和大盘中。
My data looks like this我的数据看起来像这样
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
My code so far looks like this.到目前为止,我的代码看起来像这样。
CombData <- CombData %>% group_by(Date) %>%
count(CombData$MarketSeg)
ggplot(data = CombData, aes(x=Date, y=, group=CombData$MarketSeg, color=CombData$MarketSeg))
I, therefore, need a way to count the amount in each segment grouped by the date variable so that I can input in the y variable since my current code does not work with counting因此,我需要一种方法来计算按日期变量分组的每个段中的数量,以便我可以输入 y 变量,因为我当前的代码不能用于计数
If I get it right this should give you what you want (I thought it's easier to add an additional column with the count data):如果我做对了,这应该给你你想要的(我认为添加一个带有计数数据的附加列更容易):
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()
Data:数据:
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))
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.