xlim不适用于离散值[ggplot2]

use*_*449 4 r ggplot2

我正在尝试使用以下内容在ggplot中设置xlim和ylim:

ylim=c(0, 1.5) +
xlim=c(0,100) +
Run Code Online (Sandbox Code Playgroud)

要么

coord_cartesian(xlim = c(0, 100), ylim = (0,1.5)) +
Run Code Online (Sandbox Code Playgroud)

似乎抛出这个错误:

Error in scale_x_discrete(breaks = c(0, 50, 100), labels = c(0,  : 
  non-numeric argument to binary operator
Run Code Online (Sandbox Code Playgroud)

这是因为我正在使用x的离散比例吗?我正在使用数字y.

运用

scale_y_continuous(limits = c(0, 1.5)) +
Run Code Online (Sandbox Code Playgroud)

似乎工作,但给出错误:

Warning message:
Removed 2 rows containing missing values (geom_path). 
Run Code Online (Sandbox Code Playgroud)

关于我可以尝试的任何建议?

完整代码:

cfr <- ggplot(sn, aes(x = mmad,y = fr, group=Plan, colour = Plan)) +
                  geom_line(size=0.5) +
                  #scale_y_continuous(limits = c(0, 1.5)) +
                  scale_x_discrete(breaks = c(0,50,100), labels= c(0,50,100)) +
                  labs(x = "mmad",y = "%")
Run Code Online (Sandbox Code Playgroud)

PS

在不设置轴限制的情况下,上述代码可以正常工作

Dan*_*iel 5

我不确定,但我猜离散比例只接受字符作为限制参数,每个字符代表一个因子级别(参见http://docs.ggplot2.org/0.9.3.1/discrete_scale.html).

df <- data.frame(x=sample(1:3, 10, replace=TRUE), y=sample(20:30, 10, replace=TRUE))
df$x <- as.factor(df$x)
cfr <- ggplot(df, aes(x = x,y = y)) +
  geom_line(size=0.5) +
  scale_x_discrete(breaks = c(0,50,100), labels= c(0,50,100), limits=c("1", "2", "3")) +
  labs(x = "mmad",y = "%")
Run Code Online (Sandbox Code Playgroud)

适合我...(参见limitsscale_x_discrete中的参数).