Add exponential curve to plot [duplicate] - r

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")

Related

plotting a quadratic function in R [duplicate]

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")

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.

Extended Survival Plot Lines in R

I've obtained a survival plot from the following code:
s = Surv(outcome.[,1], outcome.[,2])
survplot= (survfit(s ~ person.list[,1]))
plot(survplot, mark.time = FALSE)
person.list is just a list of 15 people.
When I plot this, the lines on my plot all end at different time points. Is there a way to extend all the lines to make them end at a certain time point? (i.e outcome.[,1] is a time to event variable and I would like the survival lines on the plot to extend out to say 5(years) )
Thanks,
Matt
This isn't an answer of how to do what you ask, but rather an explanation of why you should not do what you ask.
The lines stop where the data stops. Beyond that time, you have no information in order to make an estimate of the survival (this is in a traditional Kaplan-Meier survival analysis, as you have set it up). Therefore, the Kaplan-Meier estimate is not well defined beyond that time, and so extending that curve does not have any particular meaning. While graphically you could just draw a horizontal line at the same level as the last survival value, this is not really meaningful.
This is code I posted to a similar question on rhelp a while ago:
http://finzi.psych.upenn.edu/Rhelp10/2010-September/253817.html
?survfit # to get a working example since you did not provide one
lsurv2 <- survfit(Surv(time, status) ~ x, aml, type='fleming')
plot(lsurv2, lty=2:3, xmax=300) # drats, no effect of xmax
str(lsurv2) # so see the structure of the survfit object
lsurv2$time[21] <- 300 #add a time value
lsurv2$n.censor[21] <- 1 # mark as censoring time
lsurv2$strata[2] <- 11 # add to count of group 2
plot(lsurv2, lty=2:3, xmax=300) # horizontal line to 300 for group 2
And this was Therneau's later response (presumably better than mine): http://finzi.psych.upenn.edu/Rhelp10/2010-September/253879.html
plot(surv, mark.time=F, fun='event', xlim=c(0, 54))
for (i in 1:length(surv$strata)) { #number of curves
temp <- surv[i]
lines(c(max(temp$time), 54), 1- rep(min(temp$surv),2))
}

Overlaying a normal distribution on a histogram [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
ggplot2: Overlay histogram with density curve
sorry for what is probably a simple question, but I have a bit of a problem.
I have created a histogram that is based on a binomial distribution with mean=0.65 and sd=0.015 with 10000 samples. The histogram itself looks fine. However, I need to overlay a normal distribution on top of this (with the same mean and standard deviation). Currently, I have the following:
qplot(x, data=prob, geom="histogram", binwidth=.05) + stat_function(geom="line", fun=dnorm, arg=list(mean=0.65, sd=0.015))
A distribution shows up, but it is TINY. This is likely because the mean's count goes up to almost 2,000, while the normal distribution is much smaller. Simply put, it is not fitted with the data the way that R automatically would do. Is there a way to specify the line of the normal distribution to fit the histogram, or is there some way to manipulate the histogram to fit the normal distribution?
Thanks in advance.
"The distribution is tiny" because you are plotting a density function over counts. You should use the same metric in both plot, eg.:
I try to generate some data for your example:
x <- rbinom(10000, 10, 0.15)
prob <- data.frame(x=x/(mean(x)/0.65))
And plot both as density functions:
library(ggplot2)
ggplot(prob, aes(x=x)) + geom_histogram(aes(y = ..density..), binwidth=.05) + stat_function(geom="line", fun=dnorm, arg=list(mean=0.65, sd=0.015))
#daroczig's answer is correct about needing to be consistent in plotting densities rather than counts, but: I'm having trouble seeing how you managed to get a binomial sample with those properties. In particular, the mean of the binomial is n*p, the variance is n*p*(1-p), the standard deviation is sqrt(n*p*(1-p)), so ..
b.m <- 0.65
b.sd <- 0.015
Calculate variance:
b.v <- b.sd^2 ## n*p*(1-p)
Calculate p:
## (1-p) = b.v/(n*p) = b.v/b.m
## p = 1-b.v/b.m
b.p <- 1-b.v/b.m
Calculate n:
## n = n*p/p = b.m/b.p
b.n <- b.m/b.p
This gives n=0.6502251, p=0.9996538 -- so I don't see how you can get this binomial distribution without n<1, unless I messed up the algebra ...

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