figurine
figurine

Reputation: 756

Label NA values in geom_line

I'm trying to create a ggplot line chart with a time series that has missing data. I want to label the NA values as NA on the chart itself, but not plot the line through these points (ie have a break in the line). Below is a reproducible example of what I've tried so far. I would like the chart to have a a label of "NA" just above the point y = 0 for each year where number = NA.

library(ggplot2)

year <- c(1990:2000)
number <- c(10, 20, 30, 40, NA, 50, 60, NA, 70, 80, 90)

data <- data.frame(year, number)

ggplot(data, aes(x = year, y = number)) +
  geom_line() +
  geom_text(data = subset(data, !is.na(number)), 
            aes(label = number),
            hjust = 1,
            vjust = -1) +
  geom_text(data = subset(data, is.na(number)),
            aes(label = "NA"))

Upvotes: 1

Views: 1908

Answers (1)

conrad-mac
conrad-mac

Reputation: 893

Because the geom_text functions inherit the y location from the initial ggplot function (which is NA for these records), you need to explicitly give a y value in the second geom_text call.

library(ggplot2)

year <- c(1990:2000)
number <- c(10, 20, 30, 40, NA, 50, 60, NA, 70, 80, 90)

data <- data.frame(year, number)

ggplot(data, aes(x = year, y = number)) +
    geom_line() +
    geom_text(data = subset(data, !is.na(number)), 
              aes(label = number),
              hjust = 1,
              vjust = -1) +
    geom_text(data = subset(data, is.na(number)),
              aes(y = 1, label = "NA"))

Upvotes: 4

Related Questions