xlim对于离散值不起作用[ggplot2] [英] xlim not working for discrete values [ggplot2]
问题描述
我正在尝试使用以下方法在ggplot中设置xlim和ylim:
I'm trying to set xlim and ylim in ggplot put using the below:
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使用数字.
Is this because I'm using a discrete scale for x? I am using a numeric for y.
使用
scale_y_continuous(limits = c(0, 1.5)) +
似乎可以正常工作,但会显示错误消息:
seems to work but gives the error:
Warning message:
Removed 2 rows containing missing values (geom_path).
关于我可以尝试的任何建议吗?
Any suggestions on what I can try?
完整代码:
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.
在不设置轴限制的情况下,上述代码可以正常工作
Without putting the axis limits in, the above code worked without a problem
推荐答案
我不确定,但是我猜想离散刻度只接受字符作为极限参数,每个字符代表一个因子水平(请参阅
I'm not sure, but I guess that discrete scales only accept characters as limit-parameters, each character representing a factor level (see 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
参数).
Works for me... (see limits
parameter in scale_x_discrete).
这篇关于xlim对于离散值不起作用[ggplot2]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!