简体   繁体   English

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

[英]Error in customizing the x axis with scale_x_discrete() in ggplot2

I have the following figure: 我有下图: 在此处输入图片说明

That came from this working piece of code: 这来自于这段代码:

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

However I would like to remove some of the YEARS in the x axis. 但是,我想删除x轴中的一些YEARS。 I tried to add this piece of code to the theme(...), which should do the trick, but it did not work: 我试图将这段代码添加到theme(...),这应该可以解决问题,但是没有用:

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

R returns the following error which I don't understand: R返回以下错误,我不明白:

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

This is the str() of my data: 这是我的数据的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 ...

Not sure if the information I provided is enough but I wonder if anyone has an idea why this is not working? 不知道我提供的信息是否足够,但是我想知道是否有人知道为什么它不起作用?

scale_x_discrete应该出现在theme(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM