【问题标题】:highlight weekends using ggplot?使用 ggplot 突出显示周末?
【发布时间】:2014-08-13 15:44:59
【问题描述】:

我还没有找到任何关于此的历史问题...我想突出显示 ggplot 图表的周末表现,以便用户可以直接告诉图表哪个部分(可能是灰色阴影?)是周末表现马上。

这是一个简单版本的测试数据:

test <- data.frame(DATE=seq(from = as.POSIXct("2014-07-16 01:00"), to = as.POSIXct("2014-07-30 00:00"), by = "hour"),count=floor(runif(336,1,100)))

我的图表的简单版本是:

ggplot() + geom_line(aes(x=DATE,y=count),data=test) + labs(title="test")

所以结果可能如下所示......

【问题讨论】:

  • geom_rect?。应该做你所追求的。 this question 也可能有所帮助。
  • @AdamKimberley 非常感谢!我实际上可以指定日期而不是单元格位置。认为我需要在更长的时间范围内想出一个更聪明的方法,但到目前为止,这就像一个魅力! :)

标签: r ggplot2


【解决方案1】:

使用 geom_area() 更简洁,适用于任何日期范围。

    library(ggplot2)

    test <- data.frame(DATE=seq(from = as.POSIXct("2014-07-16 01:00"), 
                                to = as.POSIXct("2014-07-30 00:00"), 
                                by = "hour"),
                       count=floor(runif(336,1,100)))

    test$weekend <- weekdays(test$DATE) %in% c("Saturday", "Sunday")

    ggplot(data=test, aes(x=DATE, y=count)) +
      geom_area(aes(y=weekend*max(count)), fill="yellow") +
      geom_line() +
      labs(title="test")

【讨论】:

【解决方案2】:

这是在您的数据中执行两个周末的代码。您可以通过添加更多 geom_rect() 调用(或调用一个循环)来推广到任意数量的周末。

# your data
test <- data.frame(DATE=seq(from = as.POSIXct("2014-07-16 01:00"), to = as.POSIXct("2014-07-30 00:00"), by = "hour"),count=floor(runif(336,1,100)))
your_plot <- ggplot(test) + geom_line(aes(x=DATE,y=count)) + labs(title="test") 

# get all the start and end points
library(lubridate) # for hour function
sats <- which(hour(test$DATE)==0 & weekdays(test$DATE)=='Saturday')
suns <- which(hour(test$DATE)==23 & weekdays(test$DATE)=='Sunday')

# do your plot plus weekend highlights
your_plot +
  geom_rect(aes(xmin=DATE[sats[1]], xmax=DATE[suns[1]],
                  ymin=min(count), ymax=max(count)),
            fill='yellow', alpha=.005) +

  geom_rect(aes(xmin=DATE[sats[2]], xmax=DATE[suns[2]],
                ymin=min(count), ymax=max(count)),
            fill='yellow', alpha=.005)

【讨论】:

  • 不错!我只是按照亚当的建议指定日期,但这正是我在寻找更长的时间范围。谢谢! :)
  • 在 lubridate 包中提到小时和工作日功能怎么样?
  • 好点。添加。 (虽然只是一个小时。工作日是基本的)
  • 你也可以使用data.table::hour
  • 如果您使用循环添加更多 geom_rect(),请不要忘记使用 aes_string() 而不是普通的 aes()。否则只会显示最后一个矩形。
【解决方案3】:

这是一种使用 Tydiverse 工具的方法,更准确地说是geom_tile。优点是您不必精确定义x 的边界。

library(dplyr)
library(lubridate)
library(ggplot2)

test %>%
  # My preference :-)
  rename_all(tolower) %>%
  # Computing the weekends by converting in weekday starting on monday to ease the cut
  mutate(weekend = wday(date, week_start = getOption("lubridate.week.start", 1)) > 5) %>%
  ggplot() +
  geom_line(aes(x = date, y = count)) +
  # And here is the trick!
  scale_fill_manual(values = c("alpha", "yellow")) +
  geom_tile(aes(
    x = date,
    y = min(count),
    height = Inf,
    fill = weekend
  ), alpha = .4)

注意:我已经写了一个更详细的post 关于这个主题。

【讨论】:

    【解决方案4】:

    根据我的评论,希望这是您正在寻找的:

    test <- data.frame(DATE=seq(from = as.POSIXct("2014-07-16 01:00"), to = as.POSIXct("2014-07-30 00:00"), by = "hour"),count=floor(runif(336,1,100)))
    
    ggplot() + geom_rect(aes(xmin = as.POSIXct("2014-07-26 00:00"), xmax = as.POSIXct("2014-07-28 00:00"), ymin = 0, ymax = 100), fill = "yellow")+
     geom_line(aes(x=DATE,y=count),data=test) + labs(title="test")
    

    几乎完全准确地给出了问题中的图像。如果需要,您可以更改填充以更改颜色并在不同点轻松添加更多矩形。

    【讨论】:

      【解决方案5】:

      我用geom_bar() 找到了一个很好的解决方案,您可以在其中创建一个在周末无限的向量,其余值为 NA 并且不会被绘制。我使用lubridate,因为我更熟悉它。我还必须在第一次调用 ggplot() 时命名美学,因此轴继承了它们的专有名称,y 没有标记为“周末”:

      library(lubridate)
      library(ggplot2)
      
      test <- data.frame(DATE=seq(from = as.POSIXct("2014-07-16 01:00"), to = as.POSIXct("2014-07-30 00:00"), by = "hour"),count=floor(runif(336,1,100)))
      your_plot <- ggplot(test) + geom_line(aes(x=DATE,y=count)) + labs(title="test") 
      your_plot
      
      test$wd <- wday(test$DATE, label = TRUE)
      test$weekend[test$wd=="Sat" | test$wd=="Sun"] <- 1/0
      
      highlight_plot <- ggplot(test, aes(x= DATE, y = weekend)) + 
        geom_bar(stat="Identity", aes(y=weekend), col="yellow", alpha=.4, width = 1) + 
      geom_line(aes(x=DATE,y=count)) + labs(title="test") 
      
      highlight_plot
      

      此解决方案非常灵活,并且很容易调整该方法以突出显示不同的日期向量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-06
        • 2020-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-13
        • 2019-12-07
        • 2019-08-25
        相关资源
        最近更新 更多