1

无论我尝试了什么,我似乎都无法在我的情节中切换两个图例元素的位置。

我需要Chats started先出现,然后Chats completed再出现。

这是数据框:

grouped <- structure(list(date = c("2018-07-16", "2018-07-17", "2018-07-18", 
"2018-07-19", "2018-07-20", "2018-07-21", "2018-07-22", "2018-07-23", 
"2018-07-24", "2018-07-25", "2018-07-26", "2018-07-27", "2018-07-28", 
"2018-07-29", "2018-07-30", "2018-07-31"), homepage_opens = c(5L, 
0L, 0L, 3L, 1L, 2L, 0L, 1L, 0L, 2L, 5L, 0L, 0L, 0L, 0L, 0L), 
    sitewide_opens = c(39L, 34L, 19L, 62L, 46L, 44L, 16L, 51L, 
    25L, 66L, 75L, 0L, 0L, 0L, 0L, 0L), chats_started = c(10L, 
    16L, 9L, 8L, 13L, 13L, 5L, 13L, 4L, 8L, 11L, 0L, 0L, 0L, 
    0L, 0L), chats_completed = c(7L, 13L, 8L, 4L, 5L, 9L, 6L, 
    13L, 2L, 7L, 5L, 0L, 0L, 0L, 0L, 0L)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -16L))

这是情节代码:

library(ggplot2)

ggplot(grouped) +
  geom_col(aes(x = date, y = chats_started, fill = "Chats started"),
           colour = "black",
           alpha = 0.5) +
  geom_col(aes(x = date, y = chats_completed, fill = "Chats completed"),
           colour = "black",
           alpha = 0.5) +
  geom_text(aes(x = date, y = chats_started, label = chats_started),
            colour = "black",
            hjust = -0.15,
            vjust = -1.5,
            size = 3) +
  geom_text(aes(x = date, y = chats_completed, label = chats_completed),
            hjust = -0.15,
            vjust = -1.5,
            size = 3,
            colour = "black") +
  labs(title = paste0("Chat Analysis\n(started vs. completed)"),
       x = "Date",
       y = "Count") +
  theme(legend.position = "top",
        legend.title = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank(),
        axis.text = element_text(colour = "black")) +
  scale_fill_manual(values = c("Chats completed" = "lightgreen", "Chats started" = "sandybrown"),
                    labels = c("Chats completed", "Chats started"))

这就是我最终的结果:

在此处输入图像描述

4

1 回答 1

1

您只需要R 的食谱中guide = guide_legend(reverse=TRUE)所示:

ggplot(grouped) +
    geom_col(aes(x = date, y = chats_started, fill = "Chats started"),
             colour = "black",
             alpha = 0.5) +
    geom_col(aes(x = date, y = chats_completed, fill = "Chats completed"),
             colour = "black",
             alpha = 0.5) +
    geom_text(aes(x = date, y = chats_started, label = chats_started),
              colour = "black",
              hjust = -0.15,
              vjust = -1.5,
              size = 3) +
    geom_text(aes(x = date, y = chats_completed, label = chats_completed),
              hjust = -0.15,
              vjust = -1.5,
              size = 3,
              colour = "black") +
    labs(title = paste0("Chat Analysis\n(started vs. completed)"),
         x = "Date",
         y = "Count") +
    theme(legend.position = "top",
          legend.title = element_blank(),
          panel.grid.minor.x = element_blank(),
          panel.grid.major.x = element_blank(),
          axis.text = element_text(colour = "black")) +
    scale_fill_manual(values = c("Chats completed" = "lightgreen", "Chats started" = "sandybrown"),
                      labels = c("Chats completed", "Chats started"),
                      guide = guide_legend(reverse=TRUE))

在此处输入图像描述

对于它的价值,搜索显示了与图例排序相关的几个不同问题以及类似问题的各种解决方案:

但是,这些问题的答案都没有显示这个简单的解决方案来解决反转图例顺序的具体问题,所以我提供了这个答案,而不是试图判断许多问题中的一个是否真的足够接近以标记为重复.

于 2018-07-27T16:37:41.953 回答