plotting a quadratic function in R [duplicate] - r

This question already has an answer here:
Messy plot when plotting predictions of a polynomial regression using lm() in R
(1 answer)
Closed 5 years ago.
Here's how I plotted a quadratic curve:
factor <- 1:7
data <- c(0.1375000,0.2500000,0.3416667,0.4583333,0.7250000,0.9166667,1.0000000)
plot(factor, fitted(lm(data~factor+I(factor^2))), type="l")
I try to do the same with my another data.
factor1<-c(2833,2500,2437,2124,1382,3736,2100,1844,2740,957,1614,1100,1550,3858,2430,2139,1812,1757,1847,945)
data1<-c(0.95,0.88,0.88,0.93,0.81,0.67,0.55,0.53,0.52,0.90,0.87,0.20,0.28,-0.16,0.23,0.11,0.26,0.08,0.73,0.76)
plot(factor1,fitted(lm(data1~factor1+I(factor1^2))), type="l")
I think this is because the second dataset is not sorted but I thought R automatically sorts them before plotting them.
Could anyone tell me how to plot a quadratic line in the second plot.

You can order it first
i<-order(factor1)
plot(factor1[i],fitted(lm(data1[i]~factor1[i]+I(factor1[i]^2))), type="l")

Related

Histogram and distribution function in R [duplicate]

This question already has answers here:
Fitting a density curve to a histogram in R
(7 answers)
Closed 4 years ago.
i would like to plot a histogram of a dataset and the plot of the fitting lognormal distribution - I only see the histogram and not the distribution function...
hist(kmugesamt, prob=TRUE)
curve(plnorm(x, mean=mean(kmugesamt), sd=sd(kmugesamt)), add=TRUE)
Many thanks in advance
Does this help?
k<-rlnorm(1000)
hist(k, freq=F)
curve(dlnorm(x, mean=mean(k), sd=sd(k)), add=TRUE)

Add exponential curve to plot [duplicate]

This question already has answers here:
Exponential regression in R
(2 answers)
Closed 6 years ago.
I am looking to add a curve to my plot to show an exponential decrease over time. Ive plotted two small sets of data with the basic plot() function and just for clarity I wanted to add a smoothed line.
The data points for the two datasets are
1.00000 0.37360 0.27688 0.22992 0.17512 0.13768 0.08048
1.00000000 0.44283122 0.30871143 0.23647913 0.22586207 0.09800363 0.06206897
with the x values showing the decay over time (0,1,2,3,4,5,6)
I like to use ggplot2 as it makes adding lines from fitted models so simple.
Without to much to go on the following may help you out....
#prepare the data
y <- c(1.00000, 0.37360, 0.27688, 0.22992, 0.17512, 0.13768, 0.08048,
1.00000000, 0.44283122, 0.30871143, 0.23647913, 0.22586207, 0.09800363, 0.06206897)
x <- c(0,1,2,3,4,5,6,0,1,2,3,4,5,6)
z <- c(1,1,1,1,1,1,1,0,0,0,0,0,0,0)
dat <- as.data.frame(cbind(x,y,z))
#load the library
library(ggplot2)
#plot the data
ggplot(data=dat,aes(x=x,y=y))+
#add Points with different shapes depending on factor z
geom_point(aes(shape=factor(z)))+
#Add line using non-linear regreassion
stat_smooth(method="nls",formula = y~a*exp(-x*b),method.args=list(start=c(a=2,b=2)),se=F,color="red")+
#add line using linear regression
stat_smooth(method="lm",formula = y~exp(-x),se=F,color="blue")

How to plot a maths function as a heatmap [duplicate]

This question already has answers here:
3D equivalent of the curve function in R?
(4 answers)
Closed 8 years ago.
If x and y can vary from 0 to 10, how can I plot a maths function z = x*(y^2) as an image plot or heatmap ? The x and y should come to their respective axes and z value should be shown as colour on the plot. I could find method to plot only discrete values, not a continuous function. Thanks for your help.
For example
library(emdbook)
curve3d(x*y^2,xlim=c(0,10),ylim=c(0,10),sys3d="image")
curve3d is just a wrapper (the sys3d argument gives you a range of plotting possibilities) -- more generally, you can create a function and use outer() (if it's vectorized) to create a matrix, e.g.
xvec <- yvec <- seq(0,10,length.out=41)
z <- outer(xvec,yvec,function(x,y) x*y^2)
image(xvec,yvec,z)

How do I create a histogram in R with logarithmic x and y axes? [duplicate]

This question already has answers here:
Histogram with Logarithmic Scale and custom breaks
(7 answers)
Closed 10 years ago.
So I have a vector of integers, quotes, which I wish to see whether it observes a power law distribution by plotting the frequency of data points and making both the x and y axes logarithmic. However, I am not quite sure how to accomplish this in R. I can currently create a histogram using
hist(quotes, breaks = max(quotes))
But the axes are all linear.
There's probably a better way to do this, but this (basically) works:
data = rnorm(1000,0,1)
r <- hist(log(data))
plot(r$breaks[-1],log(r$counts))
EDIT: Better solution:
r <- hist(data)
plot(r$breaks[-1], r$counts, log='xy', type='h')
# or alternatively:
barplot(r$counts, log="y", col="white", names.arg=r$breaks[-1])
The barplot version doesn't have a transformed x axis for reasons that will become clear if you try it with the x axis transformed.

Adding a best fit normal on top of a histogram in R [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Fitting a density curve to a histogram in R
I'm trying to add a best fit normal over a histogram in R. Right now, I have the following:
x<-table[table$position==1,]$rt
hist(x,breaks=length(x))
And now I'd like to plot a normal curve over this plot which allows for skew and for kurtosis. How can I do this? This is what my curve looks like:
I would suggest not using the terms "skew" and "kurtosis" in the same sentence with "Normal curve", since Normal curves have neither. Perhaps you are looking for one or two parameter continuous density distribution that might be comparable to a function that was bounded at zero and had right skewing? If so then you should think about a) posting the data, b) consider plotting a Poisson, a log-Normal, or a gamma density on top of a histogram.
set.seed(123)
xpois <- trunc(rpois(100, 4))
hist(xpois)
lines(seq(0,10), 100*dpois(seq(0,10), 4))

Resources