Ignacio Ignacio - 2 months ago 21
R Question

line from -infinity to infinity with ggplot2?

I would like to plot something like this with ggplot2

enter image description here

I can do this:

library(ggplot2)
ggplot() + geom_hline(yintercept=1) +
ylab("Likelihood") +
theme(axis.ticks = element_blank(), axis.text.y = element_blank(),
panel.background = element_blank())


enter image description here

But I still need to add the line with -infinity 0 and infinity

Answer

Using a little help from this post, and a somewhat hacky solution, we can come up with something:

ggplot() + geom_hline(yintercept=1) + 
    ylab("Likelihood") + 
    xlab('')+
    theme(axis.ticks = element_blank(), axis.text.y = element_blank(), 
          panel.background = element_blank())+
    scale_x_continuous(breaks = c(-5,0,5),
                       labels = c(expression(-infinity), 0, expression(infinity)),
                       limits = c(-5,5))+
    geom_segment(aes(x = -Inf, xend = Inf, y = -1, yend = -1),
                 arrow = arrow(length = unit(.2, 'cm')))+
    geom_segment(aes(x = Inf, xend = -Inf, y = -1, yend = -1),
                 arrow = arrow(length = unit(.2, 'cm')))+
    ylim(c(-1,3))

enter image description here