【问题标题】:R ggplot scale: Show dates on breaks and hours on minor breaksR ggplot scale:显示休息时间和小休息时间
【发布时间】:2017-04-07 06:08:29
【问题描述】:

我当前的图表如下所示:

我想在规模上实现的是这样的:

所以基本上在间隔中间显示日期和日期,并在那些小休息时显示所有单个小时。

这是一个可重现的情节示例:

# Input load. Please do not change #
`dataset` = structure(list(JobName = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Job 1", class = "factor"),
    RunDateTime = structure(c(1479195000, 1479196617, 1479281400,
    1479281851, 1479367800, 1479368235), class = c("POSIXct",
    "POSIXt"), tzone = ""), EndRunDateTime = structure(c(1479195855,
    1479197916, 1479283032, 1479283032, 1479369407, 1479369407
    ), class = c("POSIXct", "POSIXt"), tzone = ""), Status = structure(c(1L,
    2L, 2L, 2L, 2L, 2L), .Label = c("failed", "successfull"), class = "factor"),
    GraphicColor = structure(c(2L, 1L, 1L, 1L, 1L, 1L), .Label = c("green",
    "red"), class = "factor")), .Names = c("JobName", "RunDateTime",
"EndRunDateTime", "Status", "GraphicColor"), row.names = c(NA,
6L), class = "data.frame")

# Original Script. Please update your script content here and once completed copy below section back to the original editing window #
vars <- c("RunDateTime", "EndRunDateTime")
dataset[vars] <- lapply(dataset[vars], as.POSIXct, format = "%Y-%m-%dT%H:%M:%S")
df <- with(dataset, data.frame(Name = JobName, Start = RunDateTime, End = EndRunDateTime, Status = Status))
library(ggplot2)
lims <- with(df, c(min(Start), max(End)))
ggplot(df, aes(color = Status)) +
geom_segment(aes(x = Start, xend = End, y = Name, yend = Name), size = 3) +
scale_x_datetime(date_breaks = "1 day", date_minor_breaks = "1 hour", limits = lims, labels = function(x) paste(format(x, "%a \n %d.%m.%y"))) +
xlab(NULL) +
ylab(NULL) +
scale_colour_manual(values = c("successfull" = "#a1d99b", "failed" = "red", "repeated" = "yellow", "canceled" = "grey")) +
theme_bw()

很想听听有关如何获得这样的规模的建议!

解决方案:

我选择了@Marijn Stevering 解决方案并稍微重新安排了代码:

vars <- c("RunDateTime", "EndRunDateTime")
dataset[vars] <- lapply(dataset[vars], as.POSIXct, format = "%Y-%m-%dT%H:%M:%S")
df <- with(dataset, data.frame(Name = JobName, Start = RunDateTime, End = EndRunDateTime, Status = Status))
library(ggplot2)
lims <- with(df, c(min(Start), max(End)))
library(dplyr)
library(lubridate)
dates <- data.frame(Date = df$Start)
minute(dates$Date) <- 0
second(dates$Date) <- 0
hour(dates$Date) <- 12
dates <- distinct(dates) %>%
  mutate(Label = paste(format(Date, "%a - %d.%m.%y")),
         DateRound = Date)
hour(dates$DateRound) <- 0

ggplot(df) +
  geom_segment(aes(x = Start, xend = End, y = Name, yend = Name, color = Status), size = 3) +
  # Add the major labels as a geom, this does limit to the plot area so have to put them above axis
geom_text(data = dates, aes(x = dates$Date, label = dates$Label, y = 0, vjust = -0.5),check_overlap = TRUE, size = 3.5) +
  # Add vertical lines to separate the days visually
geom_vline(data = dates, aes(xintercept = as.numeric(dates$DateRound)),linetype = "longdash") +
  scale_x_datetime(date_breaks = "2 hour", date_minor_breaks = "1 hour", limits = lims, labels = function(x) paste(format(x, "%H"))) +
  xlab(NULL) +
  ylab(NULL) +
  scale_colour_manual(values = c("successfull" = "#a1d99b", "failed" = "red", "repeated" = "yellow", "canceled" = "grey")) +
theme_bw() +
theme(axis.text.x = element_text(size = 7))

所以现在我的体重秤看起来像这样,我可以很好地生活。

【问题讨论】:

标签: r plot ggplot2


【解决方案1】:

我尝试通过将轴标记为小时,然后手动添加日期标签来解决此问题。我使用 geom_text 执行此操作,但这仅限于绘图区域,因此它出现在轴上方。此外,我为 geom_text 创建数据的方式可能不是最佳的。然后情节如下所示:

使用此代码生成:

# Fiddling with dates, there is probably a better way to do this
library(dplyr)
library(lubridate)
dates <- data.frame(Date = df$Start)
minute(dates$Date) <- 0
second(dates$Date) <- 0
hour(dates$Date) <- 12
dates <- distinct(dates) %>% 
  mutate(Label = paste(format(Date, "%a \n %d.%m.%y")),
         DateRound = Date)
hour(dates$DateRound) <- 0


ggplot(df) +
  geom_segment(aes(x = Start, xend = End, y = Name, yend = Name, color = Status), size = 3) +
  # Add the major labels as a geom, this does limit to the plot area so have to put them above axis
  geom_text(data = dates, aes(x = Date, label = Label, y = 0, vjust = -0.2)) +
  # Add vertical lines to separate the days visually
  geom_vline(data = dates, aes(xintercept = as.numeric(DateRound))) +
  scale_x_datetime(date_breaks = "1 hour", date_minor_breaks = "1 hour", limits = lims, labels = function(x) paste(format(x, "%H"))) +
  xlab(NULL) +
  ylab(NULL) +
  scale_colour_manual(values = c("successfull" = "#a1d99b", "failed" = "red", "repeated" = "yellow", "canceled" = "grey")) +
  theme_bw()

【讨论】:

  • 在切换剪辑时是否应该能够将日期移出情节:df$layout$clip[df$layout$name=="panel"]
  • 我从来没有做过太多的裁剪,但如果你可以关闭它,你可以通过将vjust 更改为正值来将标签放在轴下方。
  • 我试过了。它通常可以工作,但会将文本推出可见窗口,因此不是解决方案。
【解决方案2】:

我认为方面可能会帮助你实现你想要的。

    # Original Script. Please update your script content here and once completed             copy below section back to the original editing window #
vars <- c("RunDateTime", "EndRunDateTime")
dataset[vars] <- lapply(dataset[vars], as.POSIXct, format = "%Y-%m-%dT%H:%M:%S")
df <- with(dataset, data.frame(Name = JobName, Start = RunDateTime, End = EndRunDateTime, Status = Status))
library(ggplot2)
library(lubridate)
df <- transform(df,
                Date = as.Date(Start),
                DateStart = as.Date(Start),
                DateEnd = as.Date(End),
                HourStart = hour(Start),
                HourEnd = hour(End)) 

if(sum(df$DateStart != df$DateEnd) != 0){
  stop("At least one job runs in two different days.")
}

ggplot(df, aes(color = Status)) +
  geom_segment(aes(x = HourStart, xend = HourEnd, y = Name, yend = Name), size = 3) +
  xlab(NULL) +
  ylab(NULL) +
  scale_x_continuous(breaks = 0:24, limits = c(0,24)) +
  scale_colour_manual(values = c("successfull" = "#a1d99b",
  "failed" = "red", "repeated" = "yellow", "canceled" = "grey")) +
  theme_bw()+
  facet_grid(~Date)

这使您可以将每一天分组在一列中,然后将小时作为轴上的标签。我知道这不完全是您绘制的方式,而是我将如何显示它。

注意对一天开始,另一天结束的工作的处理方式。

我希望这会有所帮助。

【讨论】:

  • 不错的解决方案,但是你说得对,一天到一天运行的工作绝对是这个解决方案中的一个问题。 ://
猜你喜欢
  • 1970-01-01
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 2011-04-29
  • 2016-11-23
  • 2014-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多