Я рассчитываю создать график, отображающий общее количество наблюдений и количество наблюдений с переменной Rating = Rated в каждом сегменте small, medium и large. Мой ожидаемый результат - три графика с одной линией, изображающей общее количество наблюдений, и одной линией, показывающей количество наблюдений с переменной Rated

Мой текущий код выглядит так:

df <- CombData %>%
  group_by(Date, MarketSeg) %>%
  summarise(Total = n())

ggplot(data = CombData1, aes(x=Date, y=Ratings, group=MarketSeg, color=MarketSeg)) + 
  geom_line() + facet_wrap(~MarketSeg)

Заранее спасибо.

Данных:

structure(list(Date = structure(c(17044, 17044, 17044, 17044, 
17044, 17044, 17044, 17044, 17044, 17044, 17044, 17044, 17044, 
17044, 17044, 17044, 17044, 17044, 17044, 17044, 17044, 17044, 
17044, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 17074, 
17074, 17074, 17074, 17074, 17074, 17074, 17074, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 17105, 
17105, 17105, 17105, 17105, 17135, 17135, 17135, 17135, 17135, 
17135, 17135, 17135, 17135, 17135, 17135, 17135, 17135, 17135, 
17135, 17135, 17135, 17135, 17135, 17135, 17135, 17135, 17135, 
17135, 17135, 17135, 17135, 17135, 17135, 17135, 17135, 17135, 
17135, 17135), class = "Date"), MarketCap = c(17793490000, 37606234252.4, 
2.1108e+10, 1309638615, 1.113e+10, 3397350000, 2652530240, 37091806405, 
1359499200, 6809300000, 76266899.28, 13774786240, 1545680000, 
63594722.73, 51341862.01, 50107136.47, 1.29e+09, 46223250, 128974374.6, 
3577976769.416, 3706781337, 11026283863.73, 5842461152.7, 4362690525, 
821145923.5, 238287420, 227040000, 131262000, 73756593.4, 77758200, 
73271825.2, 89201950, 24055716, 7815943725, 75117974128, 35904920250, 
1040640000, 17203972.5, 243725033, 275137500, 797503125, 2959027243, 
12806008832, 87684100, 1081800000, 251619858, 12771500000, 745577600, 
901299772.75, 97787904480, 1037369208, 269790000, 96921756.9, 
82668000, 363480200, 11028750000, 313985864.5, 2174677900, 765596013, 
72429837.39, 1065910694, 1324654600.8, 120852648578.5, 288792000, 
264450000, 165827845.298, 68080914610, 22072758675.4, 190250062289, 
2650279142.5, 93334060, 42901711525.4, 10427653484.6, 385600000, 
1381724307.15, 290812500, 29367359691, 14549176078.6, 248830003.2, 
793066396.5, 58800000, 14086361.16, 8761750000, 7733515000, 262440000, 
739680000, 8636728800, 1.28e+09, 8033914752, 42317532.63, 9663376.8, 
89517150, 2008211179.58, 1422750374.5, 233263043.52, 616047756.6, 
216763086, 483999180, 77497804.8, 18828151.2, 328728735, 6.27e+10, 
117600000, 3996340000, 371634938.745, 1.332e+09, 321133312.5, 
67716000, 115604797000, 71070174, 334599975, 1077300000, 170400000, 
67878383.2, 572400000, 352716000, 51949883424, 93762513621, 2421822652, 
313731880, 74698740600, 1.01772e+11, 17622500000, 360985600, 
15977500000, 4872145008, 507927000, 224770774, 554260125600, 
51077328972.6, 38632433, 5137500000, 17690700000, 37577980132, 
2.01e+10, 1322669845, 1.13e+10, 3386700000, 2521047060, 36000870922.5, 
1282459200, 6535100000, 72800222.04, 14776392320, 1432862000, 
58929472.8, 47415719.621, 43476908, 1.27e+09, 41416032, 104091858.6, 
3534532125.208, 3769080183, 11393974326.04, 5644411622.1, 4462246625, 
814971894, 239169966, 254100000, 133620000, 72896458.2, 74767500, 
81688183.5, 94192500, 15034822.5, 7716165720, 72621965494, 42379578000, 
1010830000, 13233825, 249669546, 276375000, 799912500, 3239810850, 
12182897664, 94959200, 1083600000, 292003292, 12617750000, 755452800, 
927965446.5, 104629033600, 977750288, 263160000, 65804771.79, 
75696000, 361630000, 10977750000, 311226762, 2350055150, 878472348.25, 
64650336.337, 1108358465, 1278175492, 120409559124.5, 270742500, 
269062500, 167940301.926, 67357931446, 21267861444.6, 205792725082, 
2615176107.5, 93536520, 43178369269, 12247597602.8, 424160000, 
1404375525.3, 290812500, 29224799692.5, 15307698168.1, 220318232, 
733721292, 51300000, 9328716, 8848500000, 7114833800, 270540000, 
753080000, 8434161600, 1.11e+09, 8536034424, 47942014.815, 10974835.08, 
84062700, 2076286134.82, 1465733769.5, 216692444.8, 629969739.8, 
218176758.3, 482694600, 82056499.2, 17833167.6, 298047386.4, 
6.2396e+10, 128800000, 4070190000, 369604146.73, 1.314e+09, 328084250, 
65407500, 112956396196, 63568322.3, 326381730, 1065960000, 1.92e+08, 
72918857.2, 587250000, 392616000, 53505742876.8, 103244116122, 
2449851782, 403369560, 64499735880, 93614400000, 17337500000, 
355699200, 15541750000, 5166831198, 515508000, 237105755.5, 487845513600, 
49462015406.4, 36267182, 5037500000, 17149700000, 37408455409.6, 
1.965e+10, 1316154230, 1.173e+10, 3.408e+09, 2449588810, 33579526315, 
1335360000, 6553380000, 69600212.28, 14568858720, 1423750000, 
59175012.27, 46207675.809, 45650753.4, 1.3e+09, 49181538, 111635430.34, 
3413507759.2, 3613333068, 10906673713.34, 5050263030.3, 4419579725, 
703839363, 241229240, 251790000, 137812000, 73111492, 75764400, 
83173423.2, 92506950, 16237608.3, 7815943725, 70957959738, 39358071050, 
1151750000, 9175452, 259554030.12, 282975000, 798707812.5, 3175014633, 
12161411072, 87301200, 1083600000, 333939935, 14749750000, 720889600, 
767971404, 93663106040, 977750288, 265200000, 153274185.36, 74700000, 
370040000, 1.1934e+10, 231764610), MarketSeg = c("Large", "Large", 
"Large", "Medium", "Large", "Medium", "Medium", "Large", "Medium", 
"Medium", "Small", "Large", "Medium", "Small", "Small", "Small", 
"Medium", "Small", "Small", "Medium", "Medium", "Large", "Medium", 
"Medium", "Small", "Small", "Small", "Small", "Small", "Small", 
"Small", "Small", "Small", "Large", "Large", "Large", "Small", 
"Small", "Small", "Small", "Small", "Medium", "Large", "Small", 
"Small", "Small", "Large", "Small", "Small", "Large", "Small", 
"Small", "Small", "Small", "Small", "Large", "Small", "Medium", 
"Small", "Small", "Small", "Medium", "Large", "Small", "Small", 
"Small", "Large", "Large", "Large", "Medium", "Small", "Large", 
"Large", "Small", "Medium", "Small", "Large", "Large", "Small", 
"Small", "Small", "Small", "Large", "Large", "Small", "Small", 
"Large", "Medium", "Large", "Small", "Small", "Small", "Medium", 
"Medium", "Small", "Small", "Small", "Small", "Small", "Small", 
"Small", "Large", "Small", "Medium", "Small", "Medium", "Small", 
"Small", "Large", "Small", "Small", "Small", "Small", "Small", 
"Small", "Small", "Large", "Large", "Medium", "Small", "Large", 
"Large", "Large", "Small", "Large", "Medium", "Small", "Small", 
"Large", "Large", "Small", "Medium", "Large", "Large", "Large", 
"Medium", "Large", "Medium", "Medium", "Large", "Medium", "Medium", 
"Small", "Large", "Medium", "Small", "Small", "Small", "Medium", 
"Small", "Small", "Medium", "Medium", "Large", "Medium", "Medium", 
"Small", "Small", "Small", "Small", "Small", "Small", "Small", 
"Small", "Small", "Large", "Large", "Large", "Small", "Small", 
"Small", "Small", "Small", "Medium", "Large", "Small", "Small", 
"Small", "Large", "Small", "Small", "Large", "Small", "Small", 
"Small", "Small", "Small", "Large", "Small", "Medium", "Small", 
"Small", "Small", "Medium", "Large", "Small", "Small", "Small", 
"Large", "Large", "Large", "Medium", "Small", "Large", "Large", 
"Small", "Medium", "Small", "Large", "Large", "Small", "Small", 
"Small", "Small", "Large", "Medium", "Small", "Small", "Large", 
"Small", "Large", "Small", "Small", "Small", "Medium", "Medium", 
"Small", "Small", "Small", "Small", "Small", "Small", "Small", 
"Large", "Small", "Medium", "Small", "Medium", "Small", "Small", 
"Large", "Small", "Small", "Small", "Small", "Small", "Small", 
"Small", "Large", "Large", "Medium", "Small", "Large", "Large", 
"Large", "Small", "Large", "Medium", "Small", "Small", "Large", 
"Large", "Small", "Medium", "Large", "Large", "Large", "Medium", 
"Large", "Medium", "Medium", "Large", "Medium", "Medium", "Small", 
"Large", "Medium", "Small", "Small", "Small", "Medium", "Small", 
"Small", "Medium", "Medium", "Large", "Medium", "Medium", "Small", 
"Small", "Small", "Small", "Small", "Small", "Small", "Small", 
"Small", "Large", "Large", "Large", "Medium", "Small", "Small", 
"Small", "Small", "Medium", "Large", "Small", "Small", "Small", 
"Large", "Small", "Small", "Large", "Small", "Small", "Small", 
"Small", "Small", "Large", "Small"), ESG = c("Not Rated", "Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Rated", "Not Rated", "Not Rated", "Not Rated", "Rated", "Not Rated", 
"Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Rated", "Rated", "Not Rated", 
"Not Rated", "Rated", "Rated", "Not Rated", "Not Rated", "Rated", 
"Not Rated", "Not Rated", "Not Rated", "Rated", "Rated", "Not Rated", 
"Not Rated", "Not Rated", "Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Rated", "Not Rated", 
"Not Rated", "Not Rated", "Rated", "Not Rated", "Rated", "Not Rated", 
"Not Rated", "Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Rated", "Rated", "Not Rated", "Not Rated", "Rated", 
"Rated", "Not Rated", "Not Rated", "Rated", "Not Rated", "Not Rated", 
"Not Rated", "Rated", "Rated", "Not Rated", "Not Rated", "Not Rated", 
"Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Rated", "Not Rated", 
"Not Rated", "Not Rated", "Not Rated", "Not Rated", "Not Rated", 
"Not Rated")), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -321L), groups = structure(list(Date = structure(c(17044, 
17044, 17074, 17074, 17105, 17105, 17135, 17135), class = "Date"), 
    ESG = c("Not Rated", "Rated", "Not Rated", "Rated", "Not Rated", 
    "Rated", "Not Rated", "Rated"), .rows = list(c(1L, 3L, 4L, 
    5L, 6L, 7L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
    19L, 21L, 22L, 23L), c(2L, 8L, 20L), c(24L, 25L, 26L, 27L, 
    28L, 29L, 30L, 31L, 32L, 33L, 34L, 36L, 37L, 38L, 39L, 40L, 
    41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 51L, 52L, 53L, 
    54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 64L, 65L, 66L, 
    68L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 78L, 79L, 80L, 81L, 
    82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 
    94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 103L, 104L, 105L, 
    106L, 107L, 108L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 
    119L, 120L, 123L, 124L, 126L, 127L, 128L, 131L, 132L, 133L, 
    135L, 136L, 137L, 138L, 139L, 141L, 142L, 143L, 144L, 145L, 
    146L, 147L, 148L, 149L, 150L, 151L, 153L, 154L, 155L), c(35L, 
    50L, 63L, 67L, 69L, 77L, 102L, 109L, 117L, 118L, 121L, 122L, 
    125L, 129L, 130L, 134L, 140L, 152L), c(156L, 157L, 158L, 
    159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 168L, 169L, 
    170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 
    180L, 181L, 183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L, 
    191L, 192L, 193L, 194L, 196L, 197L, 198L, 200L, 202L, 203L, 
    205L, 206L, 207L, 208L, 210L, 211L, 212L, 213L, 214L, 215L, 
    216L, 217L, 218L, 219L, 220L, 221L, 222L, 223L, 224L, 225L, 
    226L, 227L, 228L, 229L, 230L, 231L, 232L, 233L, 235L, 236L, 
    237L, 238L, 239L, 240L, 242L, 243L, 244L, 245L, 246L, 247L, 
    248L, 251L, 252L, 255L, 256L, 258L, 259L, 260L, 263L, 264L, 
    265L, 267L, 268L, 269L, 270L, 271L, 273L, 274L, 275L, 276L, 
    277L, 278L, 279L, 280L, 281L, 282L, 283L, 285L, 286L, 287L
    ), c(167L, 182L, 195L, 199L, 201L, 204L, 209L, 234L, 241L, 
    249L, 250L, 253L, 254L, 257L, 261L, 262L, 266L, 272L, 284L
    ), c(288L, 289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L, 
    297L, 298L, 300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 
    308L, 309L, 310L, 311L, 312L, 313L, 315L, 316L, 317L, 318L, 
    319L, 320L, 321L), c(299L, 314L))), row.names = c(NA, -8L
), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
1
peho15ae 14 Апр 2020 в 12:14

2 ответа

Лучший ответ

Или же:

library(ggplot2) 
library(dplyr)

df <- df %>%
  group_by(Date, MarketSeg, ESG) %>%
  summarise(Total = n()) 

df %>%
  ggplot(aes(x=Date, y=Total, group=MarketSeg, color=MarketSeg, size = 'Rated')) + 
  geom_line(data = ~.x %>% summarise(Total=sum(Total)), aes(size='Total')) + 
  geom_line(data = ~.x %>% filter(ESG=='Rated')) +
  facet_wrap(~MarketSeg) +
  scale_size_manual(values = c(.5, 2))

Создано в 2020-04-14 с помощью пакетаprex (v0.3.0)

2
GGamba 14 Апр 2020 в 09:51

Попробуй это.

library(ggplot2)
library(dplyr)

# Total number of obs
df <- CombData %>%
  group_by(Date, MarketSeg) %>%
  summarise(Total = n())

# Number of rated obs
df1 <- CombData %>%
  group_by(Date, MarketSeg, ESG) %>%
  summarise(Total = n()) %>% 
  filter(ESG == "Rated")

ggplot() +
  geom_line(data = df, mapping = aes(x = Date, y = Total, group = MarketSeg, color = MarketSeg)) +
  geom_line(data = df1, mapping = aes(x = Date, y = Total, group = MarketSeg, lty = ESG), color = "red", size = 1) +
  facet_wrap(~MarketSeg)

Создано в 2020-04-14 с помощью пакетаprex (v0.3.0)

1
stefan 14 Апр 2020 в 09:40