1

I am looking to create a code in R that does the exact same as the following code:

CombData <- CombData %>%
  group_by(Date, ESG) %>%
  mutate(Ratings = n())

ggplot(data = CombData, aes(x=Date, y=Ratings, group=ESG, color=ESG)) + geom_line()

But instead of including all data I only want data of the group "Large", "Medium", or "Small" to be included respectively. I therefore either want one plot with large, medium and small firms being divided into two categories: Rated and Unrated, or 3 plots consisting of small unrated and rated firms, medium unrated and rated firms and, large unrated and rated firms.

My current data looks like this:

    ISIN            Date        Ticker  Price Close MarketCap   MarketSeg   Number of Firms Ratings ESG 
1   BSP951331318    31-01-2010  UIE     434         2232199578  Medium      26              120 Not Rated
2   BSP951331318    28-02-2010  UIE     440         2263059480  Medium      26              120 Not Rated
3   BSP951331318    31-03-2010  UIE     513         2638521621  Medium      27              120 Not Rated
4   BSP951331318    30-04-2010  UIE     512         2633378304  Medium      25              120 Not Rated
5   BSP951331318    31-05-2010  UIE     465         2391642405  Medium      24              120 Not Rated
6   BSP951331318    30-06-2010  UIE     481         2473935477  Medium      23              121 Not Rated
7   BSP951331318    31-07-2010  UIE     497         2556228549  Medium      23              121 Not Rated
8   BSP951331318    31-08-2010  UIE     438         2252772846  Medium      23              121 Not Rated
9   BSP951331318    30-09-2010  UIE     445         2288776065  Medium      23              121 Not Rated
10  BSP951331318    31-10-2010  UIE     486         2499652062  Medium      23              122 Not Rated
Dave2e
  • 22,192
  • 18
  • 42
  • 50
peho15ae
  • 119
  • 9
  • Please specify how you imagine the info on this plot. Will the three market segments be placed in a single plot? If yes, how to divide into two categories? Would you use six colors for different combinations? Or do you imagine something different? – user2332849 Mar 22 '20 at 13:43
  • Have you already thought about how would you like your first plot to look like. – user2332849 Mar 22 '20 at 14:41
  • I only need one of the two and therefore think that the second option will suffice. Thanks – peho15ae Mar 22 '20 at 14:53

1 Answers1

1

One solution for your second suggestion would be:

CombData <- CombData %>%
  subset(MarketCap %in% c("Large", "Medium", "Small")) %>%
  group_by(Date, MarketCap, ESG) %>%
  summarise(Ratings = n())

ggplot(data = CombData, aes(x=Date, y=Ratings, group=ESG, color=ESG)) + 
  geom_line() +
  facet_wrap(~MarketCap)

As for the first option, it needs more detail of what it would be like.

user2332849
  • 1,421
  • 1
  • 9
  • 12
  • The resulting graph does not look right. The large and medium segments are completely identical. The small-cap is similar to the two other caps but does not contain any rated observations. – peho15ae Mar 22 '20 at 14:31
  • We cannot reproduce your plot without any sample of the data. So this code was intended as a starting point showing the use of subset() and facet_wrap() and summarise(). You must improve it on the right direction. Feel free to ask if more questions come up. – user2332849 Mar 22 '20 at 14:39
  • When using summarise I get the following error: Error: At least one layer must contain all faceting variables: `MarketSeg`. * Plot is missing `MarketSeg` * Layer 1 is missing `MarketSeg`. What does this mean? – peho15ae Mar 22 '20 at 14:51
  • Please check https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. – user2332849 Mar 22 '20 at 14:54
  • It seems that the variable MarketSeg had to be MarketCap instead. Please check the updated code. – user2332849 Mar 22 '20 at 16:01