0

I have a dataset in r with the following columns:

> names(dataset)
[1] "Corp.Acct.Name" "Product-name"   "Package.Type"   "Total.Quantity" "ASP.Ex.Works"  

What I am trying to do is create a scatter plot with Total.Quantity on the x axis and ASP.Ex.Works on the y axis, and then fit a power curve to the scatterplot.

I have tried the following using stat_smooth:

p <- ggplot(data = dataset,                # specify dataset
            aes(x = Total.Quantity, y = ASP.Ex.Works)) + # Quantity on x, ASP on Y
  geom_point(pch = 1) +                  # plot points (pch = 1: circles, type '?pch' for other options)
  xlim(0, xlimmax) +
  ylim(0, ylimmax) +
  xlab("Quantity (lbs)") +
  ylab("Average Sale Price Ex Freight ($)") + 
  #Add line using non-linear regreassion
  stat_smooth(method="nls",formula =  ASP.Ex.Works ~a*exp(-Total.Quantity*b),method.args=list(start=c(a=2,b=2)),se=F,color="red")
p

but am thrown the following error:

Warning message: Computation failed in stat_smooth(): parameters without starting value in 'data': ASP.Ex.Works, Total.Quantity

I have tried several different methods, including specifying the model outside of ggplot, but haven't had any luck. I am trying to recreate excel's power curve option in r for a dynamic visual in Power BI.

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
proff
  • 1
  • 1
  • 1
    Try switching your formula from `ASP.Ex.Works ~a*exp(-Total.Quantity*b)` to `y ~a*exp(-x*b)` , since `geom_smooth` works on the plotting data frame inside ggplot, and the columns are named after the aesthetics to which the variables are mapped, not the original variable names. – Allan Cameron Nov 19 '20 at 12:35
  • @AllanCameron this was exactly it... it didn't click to me that those column names were mapped to x and y, even though I typed it... now, I am getting a new error, 'Warning message: Computation failed in `stat_smooth()`: Missing value or an infinity produced when evaluating the model ' which I'm guessing is produced due to missing or bad values in my data. – proff Nov 19 '20 at 12:43
  • probably. I don't have your data, so I can't tell you for sure. – Allan Cameron Nov 19 '20 at 12:49

0 Answers0