rbonac
rbonac

Reputation: 125

Horizontal abline does not work in ggplot

I'm currently doing some plots for a paper I'm writing. In order to have a consistency in my graphs I use the same graphical design. Here's a small extract of my data:

head(dailymeanCDSbondbasis)
   Dates           CDS-bond basis
1 2007-01-02       6.686319
2 2007-01-03       6.246953
3 2007-01-04       7.115593
4 2007-01-05       7.404475
5 2007-01-08       6.901632
6 2007-01-09       6.815203

Now I want to plot the dates on the x-axis and the CDS-bond-basis on the y-axis which I did as follows:

d <- melt(dailymeanCDSbondbasis, "Dates")
d$Dates <- as.Date(d$Dates)


cdsbondbasisplot <- ggplot(d, aes(Dates, value, color = variable)) +
geom_line(aes(linetype = variable, size=variable)) +
scale_linetype_manual(values=c("solid"))+
labs(color = NULL, linetype = NULL, size = NULL) +
theme_classic() +
theme(legend.position = "bottom") +
ylab("bp") +
scale_colour_manual(values=c("gray10")) +
scale_size_manual(values=c(1)) +
theme(
legend.position = "bottom",
legend.direction = "vertical",
legend.box.margin = margin(t = 20),
axis.title.x = element_text(margin = margin(t = 20)),
axis.title.y = element_text(margin = margin(r = 20))
)

The only thing I want to insert now is a horizontal abline at 0. I tried it as follows.

cdsbondbasisplot +
abline(h=0, col="black", lty=2, lwd=0.5)

However nothing seems to happen, the abline is not displayed. Everything else is fine and looks like this:

cdsbondbasisplot

What am I doing wrong?

Upvotes: 1

Views: 662

Answers (1)

cody_stinson
cody_stinson

Reputation: 400

In ggplot2, there are a couple options to add a horiziontal line. You can try:

geom_abline() 
geom_hline()

Upvotes: 1

Related Questions