在ggplot2中一起使用xlim(,)和scale_x_reverse()

时间:2014-11-21 08:49:04

标签: r ggplot2

我试图在绘图中一起使用xlim和scale_x_reverse,例如:

p <- ggplot(mtcars, aes(x = wt, y=mpg), . ~ cyl) + geom_point()
p + geom_line() +
  xlim(2, 5) +
  scale_x_reverse() 

两个命令都独立工作,但不能一起工作。相反,我得到:

Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.

有没有办法同时上班?

谢谢,

2 个答案:

答案 0 :(得分:4)

p + geom_line() +
  scale_x_reverse(limits = c(5, 2)) 
#Warning messages:
#1: Removed 7 rows containing missing values (geom_point). 
#2: Removed 7 rows containing missing values (geom_path).

first resulting plot

p + geom_line() +
  scale_x_reverse() +
  coord_cartesian(xlim = c(5, 2))

second resulting plot

答案 1 :(得分:0)

按照BondedDust的建议使用+ xlim(5,2)。

p <- ggplot(mtcars, aes(x = wt, y=mpg), . ~ cyl) + geom_point()
p + geom_line() +
  xlim(5, 2)