Adding labels to stripchart? - r

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)

Related

Adjusting Plot aspect ratio in R

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

retrieve Y value from density function of given X value

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

How to plot histogram in R and have exact axis values.

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)

left/right bottom/top justification of tiles in ggplot2

Using ggplot2 geom_tile, the default location of the tiles is centred on values of x and y. Is there a way to get x and y values to be the bottom left corner of each tile.
From http://docs.ggplot2.org/current/geom_tile.html
x.cell.boundary <- c(0, 4, 6, 8, 10, 14)
example <- data.frame(
x = rep(c(2, 5, 7, 9, 12), 2),
y = factor(rep(c(1,2), each=5)),
z = rep(1:5, each=2),
w = rep(diff(x.cell.boundary), 2)
)
qplot(x, y, fill=z, data=example, geom="tile")
I do not like my answer, but I'll post it anyway while waiting for a better solution. I transform the data (x-axis +1 and y-axis +0.5) and use the real data as axis breaks.
example <- data.frame( x = rep(c(3, 6, 8, 10, 13), 2), y = (rep(c(1.5,2.5), each=5)), z = rep(1:5, each=2))
ggplot(example)+ geom_tile(aes(x,y,fill=z)) +
scale_x_continuous(breaks=c(2, 5, 7, 9, 12))+
scale_y_continuous(breaks=c(1,2))

How to rescale label on x axis in log2(n+1) format?

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)

Resources