ggplot2,使用主题后轴未显示(axis.line = element_line())

时间:2016-03-06 22:17:55

标签: r ggplot2 axis

我试图使用ggplot2包绘制以下图表,但不知何故轴不会显示。蜱虫在那里,而不是轴线。我使用了theme(axis.line=element_line())函数,但它不起作用。

这是我的代码:

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

我不确定出了什么问题。这是图表。

enter image description here

2 个答案:

答案 0 :(得分:50)

错误已在ggplot2 v2.2.0中修复不再需要单独指定轴线。

我认为这是ggplot2 v2.1.0中的一个错误。 (请参阅this bug reportthis one。)解决方法是分别设置x轴和y轴线。

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

答案 1 :(得分:3)

您无需分别为X和Y指定轴大小。当您指定size =“2”时,R将值2视为非数字参数。因此,轴线参数默认为0大小。使用以下代码行:

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 继承自R中的,因此指定大小对于非默认值是必需的。