Mischa Mischa - 3 months ago 22
R Question

How can I remove duplicate labels produced by geom_text on each faceted plot?

I need to produce a facetted plot that shows transactions along a time axis. In addition, there are certain time stamps that I want to mark with a vertical line across all the facets.

I have managed to produce a plot like the one below, which shows the labels / markers (v4.6.3, v5.0.3, v5.2.3) in a pretty way. However, it draws them on each facet, which I want to avoid; and even worse, the labels overlap with the facet above.

Facetted ggplot2 plot with duplicate labels.

I have tried to tackle this using

geom_label
, but this only leads to a different set of problems (e.g. no 45 degree angle).

How can I remove all these labels except for the topmost facet?

Please find below a code snippet that should allow one to reproduce the plot.

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)

Answer

If you add the layerId variable to dfMarkers, it will then only plot the label on the corresponding facet.

dfMarkers$layerId <- "application"

enter image description here