I am trying to plot observed values as points against expected values as a line like this:
d <- data.frame(
ranks = 1:9,
observed = c(0.736, 0.121, 0.067, 0.034, 0.026, 0.015, 0.001, 0.001, 0.000),
expected = c(0.735, 0.136, 0.051, 0.025, 0.015, 0.009, 0.006, 0.005, 0.003)
)
ggplot(d, aes(x=ranks, y=observed)) +
geom_point(size=2.2) +
geom_line(aes(x=ranks, y=expected), size=0.8, colour='red')
It is correct but I would prefer to have the line nicely smoothed (no elbows). Using geom_smooth()
with loess
or gam
does not really help as both overdo the smoothing (in different ways). Any suggestion?
Update: In case this is useful, here is how I've generated the expected values:
# BACIS POWER FUNCTION:
fPow <- function(x, a, b) {a * x^b}
# INITIALIZE PARAMETERS:
est1 <- coef(nls(observed ~ fPow(ranks, a, b),
start=c(a=1, b=1), data=d))
# FITTING:
nlfit1 <- nls(observed ~ fPow(ranks, a, b),
start=est1, data=d)
# EXPECTED VALUES:
expected <- predict(nlfit1)