1

This relates to an earlier post

The example given is of US and German presidents.

When I create my own timeline using ggplot. The timeline tags overlap. Please help. I'd either like the spacing longer along the x axis (I have events that occur only a day apart), or the tags alternating above and below the horizontal lines or tags slanting at 45 degrees.

Here's what I've got:

x <- data.frame(Entity = rep(c("Samsung", "Consumer", "Other"), each=3),
                event = c("Note7 unveiled", "Sales in 10 markets", 
                          "First battery fire", "Delay of Note7 shipped",
                          "Note7 China sales", "Global recall",
                          "FAA warns passengers", "Formal recall",
                          "Samsung being sued re burns"), 
                Year2016 = as.Date(c("2016-08-02", "2016-08-19", "2016-08-24", 
                                     "2016-08-31", "2016-09-01", "2016-09-02", 
                                     "2016-09-08","2016-09-15", "2016-09-16")))
library(ggplot2)

ggplot(x, aes(x=Year2016, y=Entity)) + 
geom_line() + 
geom_point() + 
geom_text(aes(label=event), hjust=0, vjust=0) + 
xlim(c(min(x$Year2016), max(x$Year2016)+10*0.25)) 

And here's the plot:enter image description here

Thanks for your help in advance.

Community
  • 1
  • 1
KMO
  • 21
  • 3

1 Answers1

1

As hrbrmstr suggested, the ggrepel package can work well in this case. You want to try something like this.

library(ggplot2)
library(ggrepel)

ggplot(x, aes(x=Year2016, y=Entity)) +
geom_line() +
geom_point() +
geom_text_repel(aes(label = event),
                segment.size = 0.5,
                size = 2,
                arrow = arrow(length = unit(0.02, 'npc')),
                nudge_x = -1,
                nudge_y = 0.5) +
xlim(c(min(x$Year2016), max(x$Year2016)+10*0.25)) 

enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76