Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

r - ggplot2, axis not showing after using theme(axis.line=element_line())

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...