首页 > 解决方案 > 如何按日期着色/识别绘图点

问题描述

我正在尝试构建一个geom_lineggplot2来显示一段时间内的一些呼叫中心数据。我按照自己的意愿构建了图表,但我想用诸如特殊标记/颜色之类的东西..目前日期是字符格式(它们是如何提供给我的),但lubridate我相信如果需要,它们可以在很少/没有问题的情况下进行转换。这可能吗?

我是使用 R 绘图的超级新手,我已经从其他 SO 帖子和一些“备忘单”中了解到这一点。

我能找到的唯一“相关”帖子直接提到使用任何类型的函数与 ggplot 在这里:http: //zevross.com/blog/2014/08/04/beautiful-plotting-in-ra-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")

这是我的图表目前的样子: 没有标记

这是我正在寻找的基本概念: 确定的星期一

对我来说如何确定星期一并不重要..是否是彩色日期,绘制的点是不同的..只要它们很容易指出即可。

这是我的数据:

    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 示例

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

 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. 

更正杰森的答案 我不得不修改杰森的部分答案以使其正常工作,如下所示:

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)

这产生了我正在寻找的最准确的最终结果,如下所示:

Jason 的工作示例

标签: rggplot2

解决方案


在不知道您的数据的情况下,解决方案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")

推荐阅读