Search code examples
rggplot2axis-labels

Error in customizing the x axis with scale_x_discrete() in ggplot2


I have the following figure: enter image description here

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. I tried to add this piece of code to the theme(...), which should do the trick, but it did not work:

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

R returns the following error which I don't understand:

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

This is the str() of my data:

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?


Solution

  • scale_x_discrete should appear outside of theme(...).