我正在绘制残差与时间的关系,我在情节上添加了一条黄土平滑曲线。当我将x刻度更改为对数刻度时,黄土平滑消失,之后我无法添加它。当我要求在x轴的对数转换后显示图形时,RStudio中出现以下错误:
Error in seq.default(range[1], range[2], length = n) :'from' cannot be NA, NaN or infinite.
我做错了什么?如何在log x-scale上添加平滑的黄土? 他是我的情节代码:
max.CWRES <- max(abs(data$CWRES),na.rm=T)
plotobj3 <- NULL
plotobj3 <- ggplot(data[data$CONC!=0,])
plotobj3 <- plotobj3 + geom_point(aes(x=TIME, y=CWRES, colour=DOSE), shape=1, size=3)
plotobj3 <- plotobj3 + geom_abline(aes(x=TIME, y=CWRES),intercept=0, slope=0, colour="black")
#Add loess smoothing line
plotobj3 <- plotobj3 + geom_smooth(aes(x=TIME, y=CWRES), method="loess", se=F, span=1.5, colour="green")
plotobj3 <- plotobj3+ scale_x_continuous(name="Time (hours)")
plotobj3 <- plotobj3+ scale_y_continuous(name="CWRES", limits=c(-max.CWRES ,max.CWRES))
# log scale for x-axis
plotobj3 <- plotobj3+ scale_x_log10(name="TIME (hours)")
plotobj3
答案 0 :(得分:4)
此处的问题似乎是您从TIME = 0
开始。当您log10
- 转换x
轴时,日志时间将从-Inf
开始。您应该(至少对于loes部分)仅使用TIME > 0
:
plotobj3 + geom_smooth(aes(x = TIME, y=CWRES),
data = data[data$TIME > 0 & data$CONC != 0, ], #this is the crucial part
method = "loess",
se = FALSE,
span = 1.5,
colour = "green")
请注意,这会使用原始数据进行模型拟合。如果您想使用log10
数据进行模型拟合,可以在任何地方使用aes(x=log10(TIME), y=CWRES)
代替aes(x=TIME, y=CWRES)