使用ggplot2中的scale_x_discrete()自定义x轴时出错

时间:2016-03-08 01:46:05

标签: r ggplot2 axis-labels

我有下图: enter image description here

这来自这段代码:

ggplot(plot, aes(x=variable, y=value, colour=Species, group=Species)) + geom_line() +
  geom_point() + facet_grid(. ~ Treatment) + theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.title.y = element_blank(),
        legend.text = element_text(face = "italic"), legend.key = element_blank(),legend.title=element_blank(),
        legend.position="bottom", legend.background = element_rect(colour = 'black')) 

但是我想删除x轴上的一些年份。我试图将这段代码添加到主题(...)中,这应该可以解决问题,但它不起作用:

scale_x_discrete(breaks = c("1993","1994","1995","1996","1998","2000","2003","2006","2014"))

R返回以下错误,我不明白:

Error in (function (el, elname)  : "" is not a valid theme element name.

这是我的数据的str():

str(plot)
'data.frame':   616 obs. of  4 variables:
 $ Species  : Factor w/ 290 levels "Abarema jupunba",..: 21 24 32 48 54 60 62 81 117 139 ...
 $ Treatment: chr  "Conventional logging" "Conventional logging" "Conventional logging" "Conventional logging" ...
 $ variable : Factor w/ 22 levels "1993","1994",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value    : num  1.34808 0.62532 0.20571 2.69961 0.00427 ...

不确定我提供的信息是否足够,但我想知道是否有人知道为什么这不起作用?

1 个答案:

答案 0 :(得分:2)

scale_x_discrete应出现在theme(...)之外。