3

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: enter image description here

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: enter image description here

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
kayB
  • 151
  • 2
  • 9
  • can you check what does `is.Date(coe_melt$time)` return? – AK88 Jun 13 '17 at 10:08
  • You say you have tried things with scales, but you don't show what you have tried. That would useful. As far as I know, this isn't easily possible in ggplot, but you can have a look [at this similar question](https://stackoverflow.com/questions/39071002/moving-x-or-y-axis-together-with-tick-labels-to-the-middle-of-a-single-ggplot-n). – Axeman Jun 13 '17 at 11:01

2 Answers2

4

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))
Al14
  • 1,734
  • 6
  • 32
  • 55
2

With a combination of the answer of Al14 and the answer of baptiste from the linked (similar) question provided by Axeman, I managed to get pretty close to the wished result with the following code:

   shift_axis <- function(p, y=0){
  g <- ggplotGrob(p)
  dummy <- data.frame(y=y)
  ax <- g[["grobs"]][g$layout$name == "axis-b"][[1]]
  p + annotation_custom(grid::grobTree(ax, vp = grid::viewport(y=1, height=sum(ax$height))), ymax=y, ymin=y)+

    geom_hline(aes(yintercept=y), data = dummy) +
    theme(axis.ticks.x=element_blank())+
    theme(axis.line.y = element_line(color='black'), axis.text.x = element_blank(), legend.title=element_blank(),
    plot.background = element_blank(), panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(), panel.border= element_blank())
}
colo2 <- c("#E41A1C", "#984EA3", "#377EB8",  "#4DAF4A")
p <- ggplot(coe_melt, aes(x=time, y=value, color=score))+geom_line(size=2)+
scale_color_manual(values=colo2, breaks=c('Profitability', 'Growth', 'Safety','Payout'))+
  theme_bw()+theme(legend.position = 'bottom', axis.title.x = element_blank(), axis.title.y = element_blank())+
  scale_x_date(limit=as.Date(c('1977-07-01', '2015-07-01')), expand=c(0,0))
shift_axis(p, 0)

enter image description here

For me that's close enough, thanks for your help everybody ;)

kayB
  • 151
  • 2
  • 9