0

I am making a chart of border entries and exits, and I would like to add an annotation of events (migration policies) to the chart.

I have two dataframes: one of entries and exits (Flows), and another for the events, with dates(policies).

I have managed to plot the entries and exits plus the events (as points) with the following code:

    chart7<-ggplot()+
  geom_line(data=Flow, 
            aes(x=date,
                color=flujo), stat="count") +
  geom_point(data = policies,
             aes(x=dates, y=150000, color=type_events))+
  geom_vline(data=policies, 
                aes(xintercept=dates))+
  scale_x_date(date_minor_breaks = "1 month",
               date_labels = "%Y (%b)")+
  labs(color="Type of Flow")+
  ggtitle("Number of Exits and Entrances, by Month, 2017-2021")+
  xlab("Date")+
  ylab("Number or People")
ggplotly(chart7)

This is the result:Chart 1

However, I would like something like this:

Chart2

I tried with segment and vertical line but I can't get it right. Appreciate any help!

ntcha
  • 41
  • 3
  • 1
    Welcome to SO! To help us to help you, would you mind to provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data? To share your data, type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 20))` for the first twenty rows of data. – stefan Dec 01 '21 at 16:28
  • 1
    Hi @ntcha, This might be a ggplotly thing does the line show up if you just look at the static plot? Also it's super useful if you can provide a running example (reprex https://reprex.tidyverse.org/) so it's easier for people here to help you. – Richard J. Acton Dec 01 '21 at 16:32

2 Answers2

0

I updated the geom_vline() with col="yellow", lwd=1, lty=1

  (ggplot()+
  geom_line(data=Flow, 
            aes(x=date,
                color=flujo), stat="count") +
  geom_point(data = policies,
             aes(x=dates, y=150000, color=type_events))+
  geom_vline(data=policies, 
             aes(xintercept=dates), col="yellow", lwd=1, lty=1)+
  scale_x_date(date_minor_breaks = "1 month",
               date_labels = "%Y (%b)")+
  labs(color="Type of Flow")+
  ggtitle("Number of Exits and Entrances, by Month, 2017-2021")+
  xlab("Date")+
  ylab("Number or People"))+
  theme_bw()

enter image description here

Rfanatic
  • 2,224
  • 1
  • 5
  • 21
0

I think you were on the right track with using geom_segement() I created a reproducible example below with some fake data.

library(ggplot2)

set.seed(43)
Dates<-seq.Date(as.Date("2019/01/01"), as.Date("2019/12/31"), by="day")
Exits<-rnorm(length(Dates), 140000, 5000)
Enter<-rnorm(length(Dates), 140000, 8000)
Policy.Date<-c(as.Date("2019/02/07"), as.Date("2019/5/31"), as.Date("2019/8/12"), as.Date("2019/10/22"))
tmp<-c(1:4)
Policy<-paste("Policy", tmp, sep=" ")

DF.Exit<-data.frame(date=Dates, count=Exits)
DF.Exit[,"flujo"]<-"Exit"
DF.Enter<-data.frame(date=Dates, count=Enter)
DF.Enter[,"flujo"]<-"Enter"

Flow<-rbind(DF.Enter, DF.Exit)
policies<-data.frame(type_events=Policy, dates=Policy.Date)

chart7<-ggplot()+
  geom_line(data=Flow, 
            aes(x=date, y=count,
                color=flujo)) +
  geom_point(data = policies,
             aes(x=dates, y=200000, color=type_events))+
  geom_segment(data=policies, 
             aes(x=dates, xend=dates, y=0, yend=200000))+
  scale_x_date(date_minor_breaks = "1 month",
               date_labels = "%Y (%b)")+
  annotate("text", x=policies$date, y=210000, label=policies$type_events)+
  labs(color="Type of Flow")+
  ggtitle("Number of Exits and Entrances, by Month, 2017-2021")+
  xlab("Date")+
  ylab("Number or People")

chart7

Here is what the result looks like Example Chart

Sean McKenzie
  • 707
  • 3
  • 13