I want to make a plot (ggplot) with a date x axis, where the x axis is at y=0, but the x labels are at the bottom. It should look more or less like the graph in this picture:
I tried it with hline
like this:
ggplot(coe_melt, aes(x=time, y=value, color=score))+
geom_hline(yintercept=0)+
geom_line(size=2)+
scale_color_manual(values=c('blue','magenta','red','green'), breaks=c('Profitability', 'Growth', 'Safety','Payout'))+
theme_bw()+
theme(legend.position = 'bottom')+
theme(axis.ticks.x = element_blank())
I read in several threads that it can be done with scale_x_continuous()
, but the problem is that my x axis contains dates and not numbers. When I tried it with scale_x_continous()
I got an error (origin not supplied). I tried it with scale_x_date
, but I didn't manage to get the result.
With the code above I get the following plot:
In the end I want a horizontal line/axis with ticks at y=0, I want to remove the "lower x axis" and additionally I would like to have "tight" axes (like in the first picture).
My data looks like this:
> head(coe_melt)
time score value
1 1977-07-01 Profitability 0.4737371
2 1978-07-01 Profitability 0.4918117
3 1979-07-01 Profitability 0.4249600
4 1980-07-01 Profitability 0.3847234
5 1981-07-01 Profitability 0.3604534
6 1982-07-01 Profitability 0.4012554
> coe_melt[c(1,40,79,118),]
time score value
1 1977-07-01 Profitability 0.47373711
40 1977-07-01 Growth 0.51024065
79 1977-07-01 Safety 0.02525786
118 1977-07-01 Payout -0.12501210
See my answer below
ggplot(coe_melt, aes(x=time, y=value, color=score))+
geom_hline(yintercept=0)+
geom_line(size=2)+
scale_color_manual(values=c('blue','magenta','red','green'),
breaks=c('Profitability', 'Growth', 'Safety','Payout'))+
theme_bw()+
theme(legend.position = 'bottom')+
theme(axis.ticks.x = element_blank())+
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank() )+
theme(panel.border= element_blank())+
theme(axis.line.y = element_line(color="black", size = 0.5))+
expand_limits(y=c(-0.4, 0.8))+
scale_y_continuous(breaks=seq(-0.4, 0.8, 0.2))