peho15ae
peho15ae

Reputation: 119

group by ggplots using dummy variables

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

Upvotes: 1

Views: 509

Answers (1)

user2332849
user2332849

Reputation: 1450

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.

Upvotes: 1

Related Questions