我需要生成一个沿时间轴显示交易的分面图。此外,我想在所有方面用垂直线标记某些时间戳。
我设法制作了一个如下图所示的图,它以一种漂亮的方式显示了标签/标记(v4.6.3、v5.0.3、v5.2.3)。然而,它在每个方面都绘制了它们,我想避免这种情况;更糟糕的是,标签与上面的方面重叠。
我曾尝试使用 geom_label
解决这个问题,但这只会导致一组不同的问题(例如,没有 45 度角)。
除了最上面的方面,我如何删除所有这些标签?
请在下面找到一个代码片段,它应该允许人们重现情节。
if (!require("pacman")) install.packages("pacman")
library("pacman")
pacman::p_load(plyr, reshape2, ggplot2, lubridate, stringr, grid, gridExtra)
df <- structure(list(instantId = structure(c(1184065080, 1184065080,
1184065082, 1184065080, 1184065084, 1184065084), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), layerId = c("application", "application",
"application", "infrastructure", "infrastructure", "infrastructure")), .Names = c("instantId",
"layerId"), row.names = c(NA, 6L), class = "data.frame")
dfMarkers <- structure(list(version = c("v4.6.3", "v5.0.3", "v5.2.3"), releaseDate = structure(c(1184065081,
1184065083, 1184065085), class = c("POSIXct", "POSIXt"
), tzone = "UTC")), .Names = c("version", "releaseDate"), row.names = c(NA,
3L), class = "data.frame")
RenderTransEvo <- function(df, dfMarkers, boCumulative = FALSE) {
df <- plyr::count(df, vars = c("layerId","instantId"))
p <- ggplot(df, aes(instantId)) +
scale_x_datetime(expand = c(0.0, 0)) +
theme(plot.margin = unit(c(1,0.1,0.1,0.1),"cm"),
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
axis.title = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1))
p <- p + geom_area(aes(y = freq)) + facet_grid(layerId ~ ., scales = "free_y", space = "free_y")
p <- p + geom_vline(color = "blue",
xintercept = as.numeric(dfMarkers$releaseDate),
linetype = 2,
size = 0.25) +
geom_text(data = dfMarkers,
mapping = aes(x = releaseDate, y = Inf, label = paste0(" ", version)),
check_overlap = TRUE,
size = 3, angle = 45, vjust = 0, hjust = 0)
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)
}
RenderTransEvo(df,dfMarkers)
最佳答案
如果将 layerId
变量添加到 dfMarkers
,则它只会在相应的方面绘制标签。
dfMarkers$layerId <- "application"
关于r - 如何在每个分面图上删除由 geom_text 生成的重复标签?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39014308/