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.