Reputation: 89
I struggle with plotting a quadratic fit in ggplot, as the line between the overlapping x-values jumps back and forth between the upper and lower side of the curve.
However, doing the same in base plot, it works, which makes me think I am overlooking something (possibly really stupid) in ggplot. Could anybody guide me towards how to receive a propper line in ggplot?
I unfortunately don`t know how to reproduce the exact problem, so just add code for a similar shaped "curve":
library(ggplot2)
x1 <- log(c(1:100, 99:1))
y1 <- log(seq(0.22, 0.2, length.out = 199))
dat <- data.frame(x = x1, y = y1)
ggplot(data = dat, aes(x = x, y = y)) + geom_line()
plot(y1 ~ x1, type = "l")
Thanks a lot in advance!
Upvotes: 0
Views: 211
Reputation: 854
Try geom_path()
instead.
library(ggplot2)
x1 <- log(c(1:100, 99:1))
y1 <- log(seq(0.22, 0.2, length.out = 199))
dat <- data.frame(x = x1, y = y1)
ggplot(data = dat, aes(x = x, y = y)) + geom_path()
plot(y1 ~ x1, type = "l")
geom_path()
connects the observations in the order in which they appear in the data. geom_line()
connects them in order of the variable on the x axis.
Documentation.
Upvotes: 3