Как расположить df по столбцу с помощью ggplot2 - PullRequest
0 голосов
/ 14 июля 2020

Итак, у меня есть df следующим образом; Это фрейм данных, созданный из сводки, которая группирует все мои данные по месяцам и годам, и я хотел бы построить эти данные в порядке календарного года.

df <- structure(list(Reportable = c("Non", "Non", 
"Non", "Non", "Non", 
"Non", "Non", "Non", 
"Non", "Non", "Non", 
"Non", "Non", "Non", 
"Non", "Non", "Non", 
"Non", "Report", "Report", 
"Report", "Report", "Report", 
"Report", "Report", "Report", 
"Report", "Report", "Report", 
"Report", "Report", "Report", 
"Report", "Report", "Report", 
"Report"), m_y = c("1-2018", "1-2019", "10-2018", 
"11-2018", "12-2018", "2-2018", "2-2019", "3-2018", "3-2019", 
"4-2018", "4-2019", "5-2018", "5-2019", "6-2018", "6-2019", "7-2018", 
"8-2018", "9-2018", "1-2018", "1-2019", "10-2018", "11-2018", 
"12-2018", "2-2018", "2-2019", "3-2018", "3-2019", "4-2018", 
"4-2019", "5-2018", "5-2019", "6-2018", "6-2019", "7-2018", "8-2018", 
"9-2018"), count = c(29L, 32L, 32L, 22L, 26L, 34L, 29L, 46L, 
31L, 40L, 26L, 35L, 28L, 47L, 37L, 44L, 36L, 21L, 80L, 84L, 59L, 
51L, 48L, 60L, 63L, 67L, 63L, 52L, 58L, 65L, 50L, 67L, 61L, 67L, 
70L, 65L), pct = c("27%", "28%", "35%", "30%", "35%", "36%", 
"32%", "41%", "33%", "43%", "31%", "35%", "36%", "41%", "38%", 
"40%", "34%", "24%", "73%", "72%", "64%", "70%", "65%", "64%", 
"68%", "59%", "67%", "57%", "69%", "65%", "64%", "59%", "62%", 
"60%", "66%", "76%"), total = c("29 (27%)", "32 (28%)", "32 (35%)", 
"22 (30%)", "26 (35%)", "34 (36%)", "29 (32%)", "46 (41%)", "31 (33%)", 
"40 (43%)", "26 (31%)", "35 (35%)", "28 (36%)", "47 (41%)", "37 (38%)", 
"44 (40%)", "36 (34%)", "21 (24%)", "80 (73%)", "84 (72%)", "59 (64%)", 
"51 (70%)", "48 (65%)", "60 (64%)", "63 (68%)", "67 (59%)", "63 (67%)", 
"52 (57%)", "58 (69%)", "65 (65%)", "50 (64%)", "67 (59%)", "61 (62%)", 
"67 (60%)", "70 (66%)", "65 (76%)")), groups = structure(list(
    m_y = c("1-2018", "1-2019", "10-2018", "11-2018", "12-2018", 
    "2-2018", "2-2019", "3-2018", "3-2019", "4-2018", "4-2019", 
    "5-2018", "5-2019", "6-2018", "6-2019", "7-2018", "8-2018", 
    "9-2018"), .rows = structure(list(c(1L, 19L), c(2L, 20L), 
        c(3L, 21L), c(4L, 22L), c(5L, 23L), c(6L, 24L), c(7L, 
        25L), c(8L, 26L), c(9L, 27L), c(10L, 28L), c(11L, 29L
        ), c(12L, 30L), c(13L, 31L), c(14L, 32L), c(15L, 33L), 
        c(16L, 34L), c(17L, 35L), c(18L, 36L)), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 18L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), row.names = c(NA, -36L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

И я хочу построить это. Однако он упорядочивает ось x по месяцу, а не по месяцу-году. Как я мог изменить следующее, чтобы сделать его go в порядке календаря ...

ggplot(mon_year, aes(fill=Reportable, y=count, x=as.factor(m_y), label = total)) + 
  geom_bar(position="dodge", stat="identity")+
  geom_text(position = position_dodge(width = .9),    # move to center of bars
            vjust = -0.5,    # nudge above top of bar
            size = 3) +
  scale_fill_manual(values = c("darkorange", "cornflowerblue") )

1 Ответ

2 голосов
/ 14 июля 2020

Вы должны сделать mon_year$m_y вектор дат.

library(ggplot2)

mon_year$m_y <- as.Date(paste0('1-',mon_year$m_y), '%d-%m-%Y')

p <- ggplot(mon_year, aes(fill=Reportable, y=count, x=m_y, group=Reportable,label = total)) + 
  geom_bar(position="dodge", stat="identity")+
  geom_text(position = position_dodge(width = .9),    # move to center of bars
            vjust = -0.5,    # nudge above top of bar
            size = 3) +
  scale_fill_manual(values = c("darkorange", "cornflowerblue"))

library(scales)
p + scale_x_date(date_breaks = "1 month", date_labels =  "%b %Y") +
  theme(axis.text.x=element_text(angle=60, hjust=1))

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...