-1

This is my data

  time               dprice
2018-03-05 09:00:00 113.34000
2018-03-05 09:05:00   0.00000
2018-03-05 09:10:00  98.47778
2018-03-05 09:15:00 127.85833
2018-03-05 09:20:00  42.33333

and I want to plot non-linear line into this data, and my code is:

library(ggplot2)

ggplot(df1_all, aes(df1_all$Time, df1_all$dprice)) + 
    geom_point() + 
    geom_smooth()

and the results like this:

enter image description here

there's no non - linear trend line

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • 2
    A few observations: 1) use `aes(Time, dprice)` instead of `aes(df1_all$Time,df1_all$dprice)`, since ggplot already knows you're using `df1_all`, and using the $ notation that way can create problems. 2) the x axis looks like `Time` might be a factor or char and not a date, so ggplot is interpreting it as a categorical variable, which might be breaking your trend line. 3) Please share your data by including in your question the output of `dput(df1_all)` or `dput(head(df1_all))` so that we can see your data *and* the format it's stored in. – Jon Spring Jan 21 '19 at 02:50
  • See this: https://stackoverflow.com/questions/15102254/how-do-i-add-different-trend-lines-in-r – Kristada673 Jan 21 '19 at 03:30

1 Answers1

0

Assuming the input shown reproducibly in the Note at the end, this produces a graph:

library(ggplot2)

ggplot(df1_all, aes(time, dprice)) + 
    geom_point() + 
    geom_smooth()

screenshot

Alternatively try it this way:

library(ggplot2)
library(zoo)

z <- read.zoo(df1_all)
autoplot(z) + 
  geom_smooth()

Note

The assumed input in reproducible form. Note that time is assumed to be POSIXct.

Lines <- "time,dprice
2018-03-05 09:00:00,113.34000
2018-03-05 09:05:00,0.00000
2018-03-05 09:10:00,98.47778
2018-03-05 09:15:00,127.85833
2018-03-05 09:20:00,42.33333"
df1_all <- read.csv(text = Lines)
df1_all$time <- as.POSIXct(df1_all$time)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341