2

While trying to get the answer to this question the obvious way, I ran into a rather unexpected error. I tried to set the option axis.line in the theme using the function opts(), tried to update it using theme_update(), but all to no avail. Each time when I tried it, I got the same error :

Error in validDetails.polyline(x) : 
  It is invalid to specify both 'id' and 'id.lengths'

Some example code giving this error:

require(ggplot2)

Data <- data.frame(autos=c(
  sample(1:4,8,TRUE),
  sample(6:9,4,TRUE),
  11,18
  ))

ggplot(Data,aes(autos)) + 
  geom_histogram(fill='lightblue',colour="black",binwidth=5) +
  opts(axis.line=theme_line(), panel.border=theme_blank())

Any idea what I am overlooking?

Community
  • 1
  • 1
Joris Meys
  • 106,551
  • 31
  • 221
  • 263
  • 1
    try `theme_segment()` instead – baptiste May 30 '12 at 09:52
  • [the reason being](https://github.com/hadley/ggplot2/blob/master/R/guides-axis.r#L56) that `axis.line` is fed four arguments with the intent that they be used by `segmentGrob`, not `polylineGrob` (hence the cryptic error) – baptiste May 30 '12 at 09:59

1 Answers1

2

The ggplot2 wiki at https://github.com/hadley/ggplot2/wiki/-opts()-List contains helpful information.

Each option is described, together with the type of theme it needs. From this page:

...
axis.title.y (text)
axis.line (segment)
...

So, use theme_segment in your opts:

ggplot(Data,aes(autos)) + 
  geom_histogram(fill='lightblue',colour="black",binwidth=5) +
  opts(axis.line=theme_segment(), panel.border=theme_blank())

enter image description here

Andrie
  • 176,377
  • 47
  • 447
  • 496