Given a simple density histogram and curve like the one below, how can I retrieve a Y-value for a given X-value. For example the y value at mean(dat)?
dat<-c(5,7,4,6,4,3,55,6,7,5,4,3,33,44,5,2,33,22)
hist (dat,freq=F)
lines(density(dat), col="red", lwd=2)
Thanks.
You can use approxfun() with the results of density to get a function that approximates the density
dat <- c(5, 7, 4, 6, 4, 3, 55, 6, 7, 5, 4, 3, 33, 44, 5, 2, 33, 22)
hist(dat, freq=F)
lines(d<-density(dat), col="red", lwd=2)
#get density function
dd <- approxfun(d$x, d$y)
dd(mean(dat))
# [1] 0.015039
#plot results
abline(v=mean(dat), lty=2)
points(mean(dat), dd(mean(dat)), cex=1.2, pch=20, col="blue")
Related
Trying to change the aspect ratio of this plot so that its twice as long as it is tall, here is the code
plot(X,vw,
ylab= "Stress (MPa)",
xlab= "Strain (mm)")
title("Veronda-Westmann")
lines(X,s,col="red")
legend(x=0, y=17, c("Veronda-Westmann", "Experimental"),cex=.8,col=c("black","red"),pch=c(1,NA),lty=c(NA,1))
I used code to try and specify height and width, but this didtn appear to work. New to R so really sorry if this is a stupid Q
Without reproducible data it is a bit hard to exactly reproduce your plot, but you can set asp to your plot which means you can modify the width of the x-axis in any ratio to the y-axis you want. You can use the following code:
X <- c(0,1,2,3,4,5, 6, 7, 8, 9, 10)
vw <- c(0, 0.5, 0.75, 1, 1.5, 3, 5, 8, 10, 15, 22)
s <- c(0, 0.5, 0.75, 1, 1.5, 3, 5, 8, 10, 15, 22)
plot(X,vw,
ylab= "Stress (MPa)",
xlab= "Strain (mm)", asp = 2)
title("Veronda-Westmann")
lines(X,s,col="red")
legend("topleft",
c("Veronda-Westmann", "Experimental"),
cex=.8,col=c("black","red"),
pch=c(1,NA),
lty=c(NA,1))
Output:
Are you looking to export this figure? If so, you can simply specify this on export:
x <- seq(0,5, 0.1)
y <- seq(0,15, 0.3)
Plot 1: native aspect ratio
png("test.png") # or pdf, etc
plot(x, y)
dev.off()
Plot 2: twice as wide:
png("test2.png", width = 1000, height = 500) # random dimensions
plot(x, y)
dev.off()
Basically I want to plot a histogram of data in R, but don't want to x axis scale to be (0, 20, 40, 60, 80). I want it to have the exact values as they appear in my data. So for example, if my data contained these values: (0, 0, 2, 5, 7, 12, 14), I want my x axis to have these numbers and not a general scale. The y axis will then have the frequencies as it already does. How do I do this?
If you are just interested in counts of possible values, and not a true histogram, then barplot would be appropriate:
x <- c(0, 0, 2, 5, 7, 12, 14)
uval <- sort(unique(x))
counts <- rowSums(sapply(x, function(x) x==uval))
barplot(counts, names=uval)
I have a problem using loess and loess.smooth with a time series with missing data.
Both commands don't work with this toy data.
x <- as.Date(c(1, 2, 4, 5, 6), origin="2010-1-1")
y <- c(4, 8, 8, 28, 11)
plot(x, y, ylim=c(1,30))
lines(loess(y ~ x), col="red")
lines(loess.smooth(y=y, x=x), col="blue")
I ended up using the following code:
# Data
x.1 <- as.Date(c(1, 2, 4, 5, 6), origin="2010-1-1")
x.2 <- c(1, 2, 4, 5, 6)
y <- c(4, 8, 8, 28, 11)
# x.2 - x is numeric variable
plot(x.2, y, ylim=c(1,30))
lines(loess(y ~ x.2, span=1.01), col="black", lwd=2, lty=2) # neccessary to change span default to avoid warnings (span = 0.75)
lines(loess.smooth(x.2, y, span=1.01), col = "orange", , lwd=2) # neccessary to change span default to avoid warnings (span = 2/3)
lines(smooth.spline(x.2,y), col="blue", lwd=2)
# x.1 - x is date variable
plot(x.1, y, ylim=c(1,30))
# loess() cannot deal with date variables, thus convert it to
lines(loess(y~as.numeric(x.1), span=1.01), col="red", lwd=2) # neccessary to change span default to avoid warnings (span = 0.75)
lines(loess.smooth(x.1, y, span=1.01), col = "orange", lwd=2) # neccessary to change span default to avoid warnings (span = 2/3)
lines(smooth.spline(x.1,y), col="blue", lwd=2)
The problems were:
(1) loess is unable to deal with date variables.
(2) The span parameter had to be adjusted (>1).
I want to format my x-axis in log2(n+1) format so the x-axis labels correspond to 1, 2, 4, 16 and so on.
Input:
x <- c(1, 2, 3, 11, 15)
y <- c(1.1, 1.2, .4, 2.1, 1.5)
plot(log2(x + 1), y, axes=FALSE)
axis(1, at=(labels=as.character(formatC(x))), cex.axis=0.9)
But plot I get still has the original x-axis values.
How can I make my x-axis powers of 2 (1, 2, 4, 16, etc.)?
I guess this is what you want.
x<-c(1,2,3,11,15)
y<-c(1.1,1.2,.4,2.1,1.5)
lab<-c(1,2,4,16)
plot(log2(x+1),y,xaxt="n",xlab="x")
axis(1,at=log2(lab+1),labels=lab)
It might also be useful to calculate equally spaced labels:
lab<-round(2^seq(min(log2(x+1)),max(log2(x+1)),length.out=4)-1)
stripchart :
x <- c(2, 8, 11, 19)
stripchart(x)
How do you add labels 2, 8, 11, 19 next to the points?
Use text and specify the y position. The stripchart is drawn with y=1, so text(x, y=1.1, ...) will draw the labels slightly above the points.
x <- c(2, 8, 11, 19)
stripchart(x)
text(x, 1.1, labels=x)