xlim不适用于离散值[ggplot2]

时间:2014-04-10 15:27:50

标签: r ggplot2

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

ylim=c(0, 1.5) +
xlim=c(0,100) +

OR

coord_cartesian(xlim = c(0, 100), ylim = (0,1.5)) +

似乎抛出此错误:

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

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

使用

scale_y_continuous(limits = c(0, 1.5)) +

似乎有效但却出错:

Warning message:
Removed 2 rows containing missing values (geom_path). 

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

完整代码:

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 = "%")

P.S。

在没有设置轴限制的情况下,上面的代码没有问题

1 个答案:

答案 0 :(得分: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 = "%")

适合我......(请参阅scale_x_discrete中的limits参数)。