Hillary
Hillary

Reputation: 795

I want to add other data points to this plot

I have this data:

 d <- data.frame(date=as.Date(c("1971-09-01", "1991-12-01", "1994-12-01",       
                                "2000-01-01", "2002-08-01", "2005-08-01")), 
                 event=c("birth", "entered college", "BS", 
                         "entered grad school", "MS", "PhD"), 
                 big_events=c(" ", "first bf", "married", 
                              " ", " ", "kids"))

 ggplot() +
   scale_x_date(limits=as.Date(c("1970-1-1", "2010-12-31"))) +
   scale_y_continuous(name="", breaks=NULL, limits=c(0,1)) +
   geom_vline(data=d, mapping=aes(xintercept=as.numeric(date)),    
   color="blue") +
   geom_text(data=d, mapping=aes(x=date, y=0, label=event), size=4,     
   angle=90, vjust=-0.4, hjust=0)

And the above "ggplot" section plots out "events" over time as vertical bars which is what I want, but I can't figure out how to add the dots over time within the same plot.

Upvotes: 1

Views: 68

Answers (2)

Mehdi Nellen
Mehdi Nellen

Reputation: 8964

add geom_point:

d <- data.frame(date=as.Date(c("1971-09-01", "1991-12-01", "1994-12-01", 
                               "2000-01-01", "2002-08-01", "2005-08-01")), 
                event=c("birth", "entered college", "BS", 
                        "entered grad school", "MS",  "PhD"), 
                big_events=c(" ", "first bf", "married", " ", 
                             " ", "kids"))
# Some random dates and heights on the y-axis
b <- data.frame(date=as.Date(c("1972-09-01", "1992-12-01", "1992-12-01", 
                               "2004-01-01", "2003-08-01", "2002-08-01")),
                height=c(0.6, 0.8, 0.3, 0.2, 0.1, 0.4))

ggplot() +
  scale_x_date(limits=as.Date(c("1970-1-1", "2010-12-31"))) +
  scale_y_continuous(name="", breaks=NULL, limits=c(0,1)) +
  geom_vline(data=d, mapping=aes(xintercept=as.numeric(date)),    
             color="blue") +
  geom_text(data=d, mapping=aes(x=date, y=0, label=event), size=4,     
            angle=90, vjust=-0.4, hjust=0) + 
  # Add this line with your new data and specify the x,y data
  geom_point(data=b, aes(x=date, y=height))

It produces this: data

Upvotes: 1

Edwin
Edwin

Reputation: 3242

You have defined your y range as 0-1. Just add the point on the y axis at which you want to have your points to the data and add geom_point. Given that you only want dots at the big events, this would do:

d=data.frame(date=as.Date(c("1971-09-01", "1991-12-01", "1994-12-01",       
                            "2000-01-01", "2002-08-01", "2005-08-01")), 

             event=c("birth", "entered college", "BS", "entered grad school", "MS",    
                     "PhD"), 

big_events=c(" ", "first bf", "married", " ", " ",    
             "kids"),
y_dot = .5)


ggplot() +
  scale_x_date(limits=as.Date(c("1970-1-1", "2010-12-31"))) +
  scale_y_continuous(name="", breaks=NULL, limits=c(0,1)) +
geom_vline(data=d, mapping=aes(xintercept=as.numeric(date)),    
            color="blue") +
geom_text(data=d, mapping=aes(x=date, y=0, label=event), size=4,     
          angle=90, vjust=-0.4, hjust=0) +
geom_point(data = d[d$big_events != ' ', ], aes(y = y_dot, x = date))

Upvotes: 0

Related Questions