r0berts
r0berts

Reputation: 946

ggplot2: how to mark days when event occurred

I would like to get information from the second plot onto the first one.

Plot 1

The second plot represents days when an event occurred. It seems wider because it does not have a legend, but it is the same timescale. I chose to assign colours manually in the first plot.

I would like to overlay the second plot dots/symbols on the first plot (i.e. combine them) so that the events can be seen in one plot. Ideally this would be a line of dots that is at 0.33 height on Y and at the right end it would have letter F marking the dots - at the same height of 0.33. Could you help with that? Data and plot code are below:
Plot 2

Plot 1:

ggplot(dfSub, aes(x=Date)) + 
  geom_smooth(aes(y=P,col='P'),span=0.5,se=F) + 
  geom_smooth(aes(y=A,col='A'),span=0.5, se=F) + 
  geom_smooth(aes(y=DEP,col='DEP'),span=0.5, se=F) +
  geom_bar(aes(y=R,col='R'),stat="identity",alpha=0.5) +
  scale_color_manual("",
                     values=c('P'='red','A'='purple','DEP'='blue',R='grey')) +
  ylab("Scale 0 to 4")

Plot 2:

ggplot(dfSub, aes(x=Date)) + 
  geom_point(data=subset(dfSub, !is.na(F)), aes(y=F))

Data:

dfSub <- structure(list(Date = structure(c(17539, 17540, 17541, 17542, 
17543, 17544, 17545, 17546, 17547, 17548, 17549, 17550, 17551, 
17552, 17553, 17554, 17555, 17556, 17557, 17558, 17559, 17560, 
17561, 17562, 17563, 17564, 17565, 17566, 17567, 17568, 17569, 
17570, 17571, 17572, 17573, 17574, 17575, 17576, 17577, 17578, 
17579, 17580, 17581, 17582, 17583, 17584, 17585, 17586, 17587, 
17588, 17589, 17590, 17591, 17592, 17593, 17594, 17595, 17596, 
17597, 17598, 17599, 17600, 17601, 17602, 17603, 17604, 17605, 
17606, 17607, 17608, 17609, 17610, 17611, 17612, 17613, 17614, 
17615, 17616, 17617, 17618, 17619, 17620, 17621, 17622, 17623, 
17624, 17625, 17626, 17627, 17628, 17629), class = "Date"), R = c(1, 
2, NA, 1, 1, 2, NA, 1, 1, NA, 1, NA, 1, 1, 1, NA, NA, NA, 1, 
NA, NA, NA, 1, 1, NA, 1, NA, NA, NA, NA, NA, NA, 2, NA, NA, 0, 
1, 1, 1, 0, 1, 0, NA, NA, NA, 1, 1, 1, NA, NA, NA, NA, 1, NA, 
1, 2, 1, 1, 3, 1, NA, 2, NA, NA, 1, 2, 1, 2, NA, NA, 1, NA, NA, 
2, 1, NA, 1, 2, 1, NA, 1, 2, 1, NA, 1, 1, NA, 1, NA, NA, NA), 
    P = c(2, 4, 1, 1, 1, 2, 4, 0, 4, 1, 1, 0, 0, 2, 4, 4, 2, 
    2, 2, 4, 2, 1, 3, 2, 1, 3, 1, 0, 1, NA, 2, NA, 4, 4, 1, 3, 
    4, 4, 3, 2, 4, 4, 3, 0, 0, 2, 1, 3, 2, 0, 0, 0, 0, 1, 2, 
    4, 4, 3, 3, 3, 0, 2, 2, NA, 1, 2, 1, 1, 1, 1, 1, 1, 1, 3, 
    1, 1, 2, 4, 2, 0, 1, 4, 3, 1, 3, 1, 0, 0, 0, 0, 0), F = structure(c(NA, 
    NA, 1L, 1L, 1L, 1L, 1L, 1L, NA, 1L, 1L, 1L, 1L, NA, 1L, 1L, 
    NA, NA, NA, NA, 1L, NA, 1L, NA, 1L, 1L, 1L, 1L, 1L, NA, 1L, 
    1L, 1L, NA, NA, NA, NA, 1L, NA, 1L, NA, 1L, 1L, 1L, NA, NA, 
    NA, NA, 1L, 1L, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, 1L, NA, NA, NA, NA, 1L, 1L, NA, NA, NA, 1L, 
    1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, NA, NA, NA, NA
    ), .Label = "X", class = "factor"), DEP = c(NA, NA, 1.5, 
    NA, NA, NA, NA, 1.5, NA, 1.5, 2, 2, 2, 2, 1.5, 1, 1.5, 1.5, 
    1, 1, 1.5, 1, 2, 2, 1.5, 1, 1, 0, 1, 0.5, 1, 0.5, 1, 1, 1, 
    1, 0, 1.5, 1, 1, 1.5, 0.5, 1, 2, 1.5, 1.5, 1.5, 1.5, 1.5, 
    1.5, 1.5, 2, 2, 2, 2, 1.5, 1.5, 2, 2.5, 2, 1.5, 1, 1.5, 1, 
    NA, 2, 2.5, 2, 1.5, 1.5, 1, 1.5, 1.5, 1, 1.5, 2, 2, 1, 1.5, 
    2, 1, 1.5, 2, 2, 1.5, 2, 2, 1.5, 1, 2, 2), A = c(NA, NA, 
    NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 
    1, 1, 1, NA, 2, 1, 1, 1, 1, 1, 1, NA, 1, 1, 2, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, NA, 1, 1, 1, 1, 1, 1, 1, 1.5, 1, 1, 1, 1, 
    2, 2, 1, 2, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1.5, 1, 1, 2, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 3, 2, 2, 2, 1, 1, 1, 1, 1), FAS = structure(c(NA, 
    NA, 1L, 1L, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    ), .Label = "X", class = "factor")), .Names = c("Date", "R", 
"P", "F", "DEP", "A", "FAS"), class = "data.frame", row.names = c(NA, 
-91L))

Upvotes: 2

Views: 805

Answers (2)

camille
camille

Reputation: 16862

First off, I think you'll have an easier time if you reshape your data into a long format instead of a wide one. ggplot expects long shaped data. That way you can map aesthetics--in your case, color--to a variable, and then call geom_smooth once instead of multiple times.

Also just as a simplification, geom_col() is equivalent to geom_bar(stat = "identity").

@Gregor's suggestion above is good: if you only need one or two pieces of text, an annotate call is pretty simple. In this case, I did it with x = max(df_long$Date), y = 0.33, but you could place it at any date you want.

Let me know if any of this isn't clear.

library(tidyverse)
# deleted long line of reading in data frame
df_long <- dfSub %>%
    select(-FAS) %>%
    mutate(`F` = as.numeric(`F`)) %>%
    gather(key = group, value = value, -Date) %>%
    filter(!is.na(value))

ggplot(df_long, aes(x = Date, group = group)) +
    geom_smooth(aes(y = value, color = group), data = . %>% filter(group %in% c("P", "A", "DEP")), span = 0.5, se = F) +
    geom_col(aes(y = value), data = . %>% filter(group == "R"), alpha = 0.5, fill = "grey") +
    geom_point(aes(color = "F"), y = 0.33, data = . %>% filter(group == "F")) +
    annotate("text", x = max(df_long$Date), y = 0.33, label = "F") +
    scale_color_manual("", values = c("P" = "red", "A" = "purple", "DEP" = "blue", "F" = "gray30")) +
    ylab("Scale 0 to 4")
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Created on 2018-04-23 by the reprex package (v0.2.0).

Added 4/29: For the dots, here's an example of using data where the y value isn't just hardcoded, as the original post had.

set.seed(1234)
dots <- data.frame(
    value = rep(c(0.5, 1, 1.5), each = 20),
    Date = sample(unique(df_long$Date), size = 60, replace = T)
)


ggplot() +
    geom_col(
        aes(x = Date, y = value),
        data = df_long %>% filter(group == "R"),
        fill = "gray60"
    ) +
    geom_point(aes(x = Date, y = value), data = dots)

Upvotes: 4

Tung
Tung

Reputation: 28391

Is this what you want?

library(tidyverse)

pp1 <- ggplot(dfSub, aes(x=Date)) + 
  geom_smooth(aes(y=P,col='P'),span=0.5,se=F) + 
  geom_smooth(aes(y=A,col='A'),span=0.5, se=F) + 
  geom_smooth(aes(y=DEP,col='DEP'),span=0.5, se=F) +
  geom_bar(aes(y=R,col='R'),stat="identity",alpha=0.5) +
  scale_color_manual("",
                 values=c('P'='red','A'='purple','DEP'='blue',R='grey')) +
  ylab("Scale 0 to 4")

dfSub <- dfSub %>% 
  mutate(F1 = ifelse(F == "X", 0.33, .))

pp1 + 
  geom_point(data=subset(dfSub, !is.na(F1)), aes(y=F1)) +
  geom_text(data = dfSub %>% 
          select(Date, F1) %>% 
          arrange(desc(Date)) %>% 
          slice(1), 
        aes(x = Date + 0.03, y = 0.33, label = "F"))

enter image description here

Upvotes: 3

Related Questions