I want to add a curve to an existing plot.
This curve should be a poisson distribution curve that approaches the mean 3.
I've tried this code
points is a vector with 1000 values
plot(c(1:1000), points,type="l")
abline(h=3)
x = 0:1000
curve(dnorm(x, 3, sqrt(3)), lwd=2, col="red", add=TRUE)
I am getting a plot, but without any curve.
I would like to see a curve that approaches 3.
you can do something like this:
plot(0:20, 3+dpois( x=0:20, lambda=3 ), xlim=c(-2,20))
normden <- function(x){3+dnorm(x, mean=3, sd=sqrt(3))}
curve(normden, from=-4, to=20, add=TRUE, col="red")
running this code will produce the following:
is that what you intended?
Related
I would like to plot a figure like the attached one. Consider just one color, let's say, the blue line.
The figure shows correlation between twinA and twinB.
The line in the figure is the mean of 1000 lines obtained with a permutation.
I averaged slope and intercept and got the averaged regression line.
So far so good.
Then, I need to plot CIs.
I need to use CIs I get from the permutation itself rather than the average CIs.
Therefore, I computed the CIs of the correlation coefficients (between twinA and twinB) vector I obtained with the permutations.
Here's comes the issue.
With this code I am able to plot the line but I cannot find how to insert CI with abline function:
plot(1, type="n", xlab="", ylab="", xlim=c(0, 10), ylim=c(0, 20))
a=10.09773458
b = 0.183630788
abline(a, b)
CI=0.001940921
Any suggestion?
Thank you in advance!
Given data that looks like this:
x<-c(0.287,0.361,0.348,0.430,0.294)
y<-c(105,230,249,758,379)
I'm trying to fit several different methods to this data. For this question I'm looking at 2nd order polynomial fits vs Loess fits. To get a smoother curve, I'd like to expand the x data to give me more points to predict over. So for my Loess curve I do this:
Loess_Fit<-loess(y ~ x)
MakeSmooth<-seq(min(x), max(x), (max(x)-min(x))/1000)
plot(x,y)
#WithoutSmoothing
lines(x=sort(x), y=predict(Loess_Fit)[order(x)], col="red", type="l")
#With Smoothing
lines(x=sort(MakeSmooth), y=predict(Loess_Fit,MakeSmooth)[order(MakeSmooth)], col="blue", type="l")
When I attempt to do the same thing with a 2nd order polynomial fit- I get an error
Poly2<-lm(y ~ poly(x,2,raw=TRUE))
plot(x,y)
#WithoutSmoothing
lines(x=sort(x), y=predict(Poly2)[order(x)], col="red", type="l")
#With Smoothing
lines(x=sort(MakeSmooth), y=predict(Poly2,MakeSmooth)[order(MakeSmooth)], col="blue", type="l")
Obviously there is some difference between Poly2 and Loess_Fit, but I don't know what the difference is. Is there a way to smooth out the Poly2 fit as I did with the Loess_Fit?
For lm, the new data needs to be a data frame:
lines(x=sort(MakeSmooth), y=predict(Poly2,data.frame(x=MakeSmooth))[order(MakeSmooth)], col="blue", type="l")
In R I'm able to overlap a normal curve to a density histogram:
Eventually I can convert the density histogram to a probability one:
a <- rnorm(1:100)
test <-hist(a, plot=FALSE)
test$counts=(test$counts/sum(test$counts))*100 # Probability
plot(test, ylab="Probability")
curve(dnorm(x, mean=mean(a), sd=sd(a)), add=TRUE)
But I cannot overlap the normal curve anymore since it goes off scale.
Any solution? Maybe a second Y-axis
Now the question is clear to me. Indeed a second y-axis seems to be the best choice for this as the two data sets have completely different scales.
In order to do this you could do:
set.seed(2)
a <- rnorm(1:100)
test <-hist(a, plot=FALSE)
test$counts=(test$counts/sum(test$counts))*100 # Probability
plot(test, ylab="Probability")
#start new graph
par(new=TRUE)
#instead of using curve just use plot and create the data your-self
#this way below is how curve works internally anyway
curve_data <- dnorm(seq(-2, 2, 0.01), mean=mean(a), sd=sd(a))
#plot the line with no axes or labels
plot(seq(-2, 2, 0.01), curve_data, axes=FALSE, xlab='', ylab='', type='l', col='red' )
#add these now with axis
axis(4, at=pretty(range(curve_data)))
Output:
At first you should save your rnorm data otherwise you get different data each time.
seed = rnorm(100)
Next go ahead with
hist(seed,probability = T)
curve(dnorm(x, mean=mean(na.omit(seed)), sd=sd(na.omit(seed))), add=TRUE)
Now you have the expected result. Histogram with density curve.
The y-axis isn't a "probability" as you have labeled it. It is count data. If you convert your histogram to probabilities, you shouldn't have a problem:
x <- rnorm(1000)
hist(x, freq= FALSE, ylab= "Probability")
curve(dnorm(x, mean=mean(x), sd=sd(x)), add=TRUE)
I'm running into an odd problem; get my dataset here:dataset
All I need is a simple graph showing the best-fit regression (quadratic regression) between rao and obs_richness; but instead I am getting very different polynomial models. Any suggestions on how to fix this?
#read in data
F_Div<-read.csv('F_Div.csv', header=T)
str(F_Div)
pairs(F_Div[2:12], pch=16)
#richness vs functional diversity
par(mfrow=c(1,1))
lm1<-lm ( rao~Obs_Richness, data=F_Div)
summary (lm1)
plot (rao~Obs_Richness, data=F_Div, pch=16, xlab="Species Richness", ylab="Rao's Q")
abline(lm1, lty=3)
lines (lowess (F_Div$rao~F_Div$Obs_Richness))
poly.mod<- lm (F_Div$rao ~ poly (F_Div$Obs_Richness, 2, raw=T))
summary (poly.mod)
lines (F_Div$Obs_Richness, predict(poly.mod))
I need the line that best approximates the lowess line (a simple curve), not this squiggly mess.
I also tried this but not what need:
xx <- seq(0,30, length=67)
plot (rao~Obs_Richness, data=F_Div, pch=16, xlab="Species Richness", ylab="Rao's Q")
lines(xx, predict(poly.mod, data.frame(x=xx)), col="blue")
The squiggly mess happens because line(...) draws lines between successive points in the data's original order. Try this at the end.
p <- data.frame(x=F_Div$Obs_Richness,y=predict(poly.mod))
p <- p[order(p$x),]
lines(p)
I am having trouble understanding the Beta function in R. I want the y scale to display a relative value in percent (0->1). How do I achive this with the graph having the same form?
x = seq(0,1,0.001)
plot(x,dbeta(x,10,40), type="l", col="red", xlab="time", ylab="frequency")
It sounds like you're looking for the beta density, normalized so the maximum value is 1. This could be accomplished with:
x = seq(0,1,0.001)
density = dbeta(x, 10, 40)
plot(x, density/max(density), type="l", col="red", xlab="time", ylab="frequency")
Well, I am sure you looked the help page at the value page perhaps there is what you are looking for :
dbeta gives the density, pbeta the distribution function, qbeta the quantile function, and rbeta generates random deviates.
I think you want to plot the pbeta