33

I am trying to draw this following graph using ggplot2 package, but somehow the axis won't show up. the ticks are there, just not the axis line. I have used the theme(axis.line=element_line()) function, but it wouldn't work.

Here is my code:

library(ggplot2)

ggplot(data = soepl_randsub, aes(x = year, y =satisf_org, group = id)) +
    geom_point() + geom_line() +ylab("Current Life Satisfaction") +theme_bw() +
    theme(plot.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank() ) +
    theme(panel.border= element_blank()) +
    theme(axis.line = element_line(color="black", size = "2")) 

I am not sure what went wrong. Here is the chart.

enter image description here

rawr
  • 20,481
  • 4
  • 44
  • 78
M.Zhao
  • 331
  • 1
  • 3
  • 4
  • in element_line(color="black", size = "2")) replace size= "2" by size =2 – MLavoie Mar 06 '16 at 22:25
  • if you feel like living on the bleeding edge, you can do `devtools::install_github("Katiedaisey/ggplot2")` -- or wait for the pull request to bring the fix into `hadley/ggplot2` or wait for a bug-fix release ... – Ben Bolker Mar 14 '16 at 02:41

2 Answers2

61

The bug was fixed in ggplot2 v2.2.0 There is no longer a need to specify axis lines separately.

I think this is a bug in ggplot2 v2.1.0. (See this bug report and this one.) A workaround is to set the x-axis and y-axis lines separately.

  library(ggplot2)

  ggplot(data = mpg, aes(x = hwy, y = displ)) + 
  geom_point() + 
  theme_bw() + 
  theme(plot.background = element_blank(),
         panel.grid.major = element_blank(),
         panel.grid.minor = element_blank() )+
  theme(panel.border= element_blank())+
  theme(axis.line.x = element_line(color="black", size = 2),
        axis.line.y = element_line(color="black", size = 2))
Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
4

You don't need to specify axis-size for X and Y separately. When you are specifying size="2", R is considering value 2 as non-numeric argument. Hence, axis-line parameter is defaulted to 0 size. Use this line of code:

ggplot(data = mpg, aes(x = hwy, y = displ)) + geom_point() +xlab("Date")+ylab("Value of Home")+theme_bw() +theme(plot.background = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank()) + theme(panel.border= element_blank()) + theme(axis.line = element_line(color="black", size = 2))

axis_line inherits from line in R, hence specifying size is mandatory for non-default values.

vivek
  • 356
  • 3
  • 7