0

Okay so I'm trying to create a smooth line between 4 points to get a prediction on daily temperature change between the increase in seasonal temperature.

Temperature4.5 <- c(8.1, 6.6, 3.8, 6.7, 8.1)
DayNumber <- c(45, 105, 197, 288, 410)
med4.5 <- data.frame(Season, Temperature4.5, Date, DayNumber)



ggplot(data = med4.5, aes(x=DayNumber, y = Temperature4.5)) +
  geom_point() +
  geom_smooth(method = loess) +
  scale_x_continuous(breaks = seq(45, 410, 1))

But when I use ggplot_build the x variables aren't whole numbers (days) but instead something like

45, 49.62025, 54.24051.... etc,

I need whole day numbers for every day for the x variable to the y variable.

How would I go about that?

I've already tried the above mentioned, so any steps forward would be a great help.

  • If you need to make preductions off the model, don't use the plotting functions for that. Use the methods in the answer your last question was closed as a dup of https://stackoverflow.com/a/53284459/2372064 There's an answer there that shows calling loess directly. Then you can predict for whatever values you want rather than what ggplot decided to draw to make the smooth line. You should avoid trying to do your modeling with ggplot -- it's a great tool for plotting, not modeling. – MrFlick Feb 22 '23 at 17:52
  • @MrFlick Thank you so much for pointing that out. I went through and did as the commented directed, but I am still unsure on how to get a variable for each day inbetween the 4 data points, as the comment I followed didn't provide anymore then the relative 4 points. – EcoQuestions Feb 22 '23 at 18:44
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 22 '23 at 19:28

1 Answers1

0

If you need to make predictions for your model, you should avoid going though ggplot. You can build the model then predict with that model. For example

med4.5 <- data.frame(
  Temperature4.5 = c(8.1, 6.6, 3.8, 6.7, 8.1), 
  DayNumber = c(45, 105, 197, 288, 410)
)

m <- loess(Temperature4.5~DayNumber, data=med4.5)
newvals <- data.frame(DayNumber=seq(min(med4.5$DayNumber), max(med4.5$DayNumber)))
cbind(newvals, value=predict(m, newvals))
#     DayNumber    value
# 1          45 8.100000
# 2          46 8.086882
# 3          47 8.072556
# 4          48 8.057062
# 5          49 8.040444
# ...

This will create a predicted value for each day from the min to max DayNumber

MrFlick
  • 195,160
  • 17
  • 277
  • 295