Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

annotation_logticks() and coord_flip() seem incompatible

Tags:

r

ggplot2

I get an error that seems to be combined by using annotation_logticks() and coord_flip() on the same plot. For instance:

ggplot(mtcars, aes(x=mpg, y=disp)) + 
  geom_line() + 
  annotation_logticks(sides="l") +
  coord_flip()

gives the error Error in unit(yticks$y, "native") : 'x' and 'units' must have length > 0. traceback() gives results that I don't totally understand, but which seem to have something to do with assigning units.

On the other hand, annotation_logticks() or coord_flip() alone doesn't cause any problem.

ggplot(mtcars, aes(x=mpg, y=disp)) + 
  geom_line() + 
  annotation_logticks(sides="l") #+
  #coord_flip()

works fine, as does

ggplot(mtcars, aes(x=mpg, y=disp)) + 
  geom_line() + 
  #annotation_logticks(sides="l") #+
  coord_flip()

I could switch the x and y mappings to avoid coord_flip(), but this is not ideal (I have to rewrite old plots if I want to add annotation_logticks() for instance).

like image 419
Drew Steen Avatar asked Dec 08 '13 22:12

Drew Steen


1 Answers

This has now been fixed in ggplot2 version 3.3.4 by this PR.

library(ggplot2)

ggplot(mtcars, aes(x=mpg, y=disp)) + 
  geom_line() + 
  annotation_logticks(sides="l") +
  coord_flip()

Created on 2021-08-29 by the reprex package (v2.0.0)

like image 168
teunbrand Avatar answered Oct 13 '22 17:10

teunbrand