Reputation: 119
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
Upvotes: 0
Views: 53
Reputation: 607
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!
Upvotes: 1