如何按日期着色/识别绘图点

时间:2019-03-25 14:10:53

标签: r ggplot2

我正在尝试在geom_line中构建一个ggplot2图,以显示一段时间内的一些呼叫中心数据。我已经建立了自己想要的图表,但是我想确定特定的日期(具体来说是星期一。IE,1 / 7、1 / 14、1 / 21、1 / 28、2 / 4等)像是特殊的标记/颜色之类的..当前的日期是字符格式(如何给我),但是使用lubridate,我相信如果需要的话,几乎可以转换它们。这可能吗?

我是R绘图的超级新手,我已经从其他SO职位和一些“备忘单”中获得了这一点。

我能找到的唯一一篇与“相关”的文章直接提到了使用ggplot使用任何类型的函数的地方:http://zevross.com/blog/2014/08/04/beautiful-plotting-in-r-a-ggplot2-cheatsheet-3/#use-a-function-to-alter-labels。这也不完全符合我的要求。

这里是我的代码:

a <-  ggplot(cleantargetcounts)+
   geom_line(aes(x=DTE, y=TOTAL, color = DISPOSITION, group = DISPOSITION), size = 1.2)+
   scale_color_manual(values = c(Approval = "dark green",Denial = "red",Skip = "orange"))+
   geom_point(aes(x=DTE, y=TOTAL, color = DISPOSITION),size = 3.2)+
   geom_text_repel(aes(label=cleantargetcounts$TOTAL, x=DTE,y=TOTAL))+
   theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))+
   labs( x = "Date", y = "Disposition Count", title ="Disposition Count by Date")

这是我的图形当前的样子: no markings

这是我正在寻找的基本概念: Mondays identified

对我而言,没关系,如何确定星期一的日期。.是否为带颜色的日期,绘制的点是不同的..只要它们易于指出即可。

这是我的数据

    DTE        DISPOSITION TOTAL  
   <chr>      <chr>       <int>  
 1 2019-01-08 Approval      454 
 2 2019-01-08 Denial        120   
 3 2019-01-08 Skip          135
 4 2019-01-09 Approval      425 
 5 2019-01-09 Denial        141
 6 2019-01-09 Skip          203 
 7 2019-01-10 Approval      448 
 8 2019-01-10 Denial        112
 9 2019-01-10 Skip          169 
10 2019-01-11 Approval      666 

Heck1提供的密码尝试

cleantargetcounts$weekday <- wday(ymd(cleantargetcounts$DTE), label = TRUE, abbr = FALSE)

a <-  ggplot(cleantargetcounts)+
  geom_line(aes(x=DTE, y=TOTAL, color = DISPOSITION, group = DISPOSITION), size = 1.2)+
  scale_color_manual(values = c(Approval = "dark green",Denial = "red",Skip = "orange"))+
  geom_point(aes(x=DTE, y=TOTAL, color = DISPOSITION),size = 3.2)+
  geom_text_repel(aes(label=cleantargetcounts$TOTAL, x=DTE,y=TOTAL))+
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1, color = ifelse(cleantargetcounts$weekday == "Monday", "red", "black")))+
  labs( x = "Date", y = "Disposition Count", title ="Disposition Count by Date")

提供以下图表 Heck1 Example

这有点奇怪,因为它可以正确识别我的数据框中的星期几。

 DTE        DISPOSITION TOTAL weekday
  <chr>      <chr>       <int> <ord>  
1 2019-01-07 Approval      455 Monday 
2 2019-01-07 Denial         95 Monday 
3 2019-01-07 Skip          154 Monday 
4 2019-01-08 Approval      454 Tuesday
5 2019-01-08 Denial        120 Tuesday
6 2019-01-08 Skip          135 Tuesday

尝试杰森的答案 我收到一个我不熟悉的错误

 df_tidy <- cleantargetcounts %>% 
gather(DISPOSITION, TOTAL, -DTE) %>% 
mutate(dow = wday(ymd(cleantargetcounts$DTE, abbr = TRUE, label = TRUE)))


Error: Column `dow` must be length 306 (the number of rows) or one, not 155
In addition: Warning message:
 2 failed to parse. 

更正杰森的答案 我必须修改Jason的答案的一部分,以使其对我来说可以正常工作,如下所示:

df_tidy <- cleantargetcounts2 %>% mutate(dow = wday(DTE, abbr = TRUE, label = TRUE))
df_regions <- df_tidy %>% filter(dow == "Mon") %>% mutate(min = DTE - 0.5,
                                                          max = min + 1,
                                                          ymin = -Inf,
                                                          ymax = +Inf)

a <-  ggplot(df_tidy)+
  geom_line(aes(x=DTE, y=TOTAL, color = DISPOSITION, group = DISPOSITION), size = 1.2)+
  scale_color_manual(values = c(Approval = "dark green",Denial = "red",Skip = "orange"))+
  geom_point(aes(x=DTE, y=TOTAL, color = DISPOSITION),size = 3.2)+
  geom_text_repel(aes(label=df_tidy$TOTAL, x=DTE,y=TOTAL))+
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))+
  labs( x = "Date", y = "Disposition Count", title ="Disposition Count by Date")+
  geom_rect(data = df_regions,aes(xmin = min, xmax = max, ymin = ymin, ymax = ymax), fill = "blue", alpha = 0.2, color = NA)

我一直在寻找哪种最准确的最终结果:

Working Example via Jason

2 个答案:

答案 0 :(得分:0)

在不知道您的数据的情况下,使用lubridate的解决方案如下所示:

 library(lubridate)
 cleantargetcounts$weekday <- wday(ymd(cleantargetcounts$DTE), label = TRUE, abbr = FALSE)

cleantargetcounts$weekday的输出应该是带有"Monday" "Tuesday"的向量,依此类推。然后在情节上标记星期一,您可以使用:

a <-  ggplot(cleantargetcounts)+
   geom_line(aes(x=DTE, y=TOTAL, color = DISPOSITION, group = DISPOSITION), size = 1.2)+
   scale_color_manual(values = c(Approval = "dark green",Denial = "red",Skip = "orange"))+
   geom_point(aes(x=DTE, y=TOTAL, color = DISPOSITION),size = 3.2)+
   geom_text_repel(aes(label=cleantargetcounts$TOTAL, x=DTE,y=TOTAL))+
   theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1,
colour = ifelse(cleantargetcounts$weekday == "Monday",
                                         "red","black)
    ))+
   labs( x = "Date", y = "Disposition Count", title ="Disposition Count by Date")

答案 1 :(得分:0)

请尝试创建一个可复制的示例,但是,根据您的图片,以下内容将为您提供所需的信息。简而言之,只需将经过“过滤”的数据传递到geom调用中即可:

library(tidyverse)
library(lubridate)

set.seed(101)

df <- tibble(
  date = seq(ymd("2019-01-07"), ymd("2019-03-18"), by = "day"),
  approval = runif(n = 71, 225, 800),
  denial = runif(n = 71, 85, 120),
  skip = runif(n = 71, 120, 300)
)

df %>%
  gather(metric, value, -date) %>%
  mutate(
    dow = wday(date, abbr = TRUE, label = TRUE)
  ) %>%
  ggplot(aes(x = date, y = value, color = metric)) +
  geom_point() +
  geom_line() +
  geom_point(data = . %>% filter(dow == "Mon"), color = "deeppink", size = 3)

或者,您可以像这样创建highlights data.frame并将其传递给geom_rect

df_tidy <-
  df %>%
  gather(metric, value, -date) %>%
  mutate(
    dow = wday(date, abbr = TRUE, label = TRUE)
  )

df_regions <- 
  df_tidy %>%
  filter(dow == "Mon") %>%
  mutate(
    min = date - 0.5, # we want the highlighted region 'around' the point, not on the point.
    max = min + 1,
    ymin = -Inf,
    ymax = +Inf
  )

df_tidy %>%
  ggplot(aes(x = date, y = value, color = metric)) +
  geom_point() +
  geom_line() +
  geom_rect(
    data = df_regions, 
    aes(xmin = min, xmax = max, ymin = ymin, ymax = ymax), 
    fill = "orange", alpha = 0.2, color = NA
  )

Highlighted with Rects

reprex package(v0.2.1)于2019-03-25创建