I have a code in R
x=rnorm(1000,1,1)
quantile(x,0.05)
x1=rnorm(1000,-10,1)
sum(x1>quantile(x,0.05))/length(x1)
y=hist(x,plot=FALSE)$density
plot(y)
plot(y,type="l")
y1=hist(x1,plot=FALSE)$density
matplot(y1,type="l",add=TRUE)
I want to change it so that the plots do not overlap but are next to each other. Is it enough that I change the values for the mean and sd or I have to change something else in the code. I am new to this, so please help me
In order to plot both histograms, you need to set the correct x and y limits for the plot windows because base R graphics will not resize the window after the first set of data has been drawn. Here's one way to do that
x <- rnorm(1000,1,1)
x1 <- rnorm(1000,-10,1)
y <- hist(x,plot=FALSE)
y1 <- hist(x1, plot=FALSE)
plot(0,0,
ylim=range(c(y$counts, y1$counts)),
xlim=range(c(y$breaks, y1$breaks)),
xlab="x", ylab="counts", type="n")
plot(y, add=TRUE)
plot(y1, add=TRUE)
Related
I want to plot a line chart. Depending on values it should change its color.
What I found is:
plot(sin(seq(from=1, to=10,by=0.1)),type="p",
col=ifelse(sin(seq(from=1, to=10,by=0.1))>0.5,"red","yellow"))
That works. But as soon as I change from type="p" to type="l" the conditional colouring disappears.
Is that behavior intended?
What is a solution with base graphics to plot a functional line with different colors?
Use segments instead of lines.
The segments function will only add to an existing plot. To create a blank plot with the correct axes and limits, first use plot with type="n" to draw "nothing".
x0 <- seq(1, 10, 0.1)
colour <- ifelse(sin(seq(from=1, to=10,by=0.1))>0.5,"red","blue")
plot(x0, sin(x0), type="n")
segments(x0=x0, y0=sin(x0), x1=x0+0.1, y1=sin(x0+0.1), col=colour)
See ?segments for more detail.
Here is a little different approach:
x <- seq(from=1, to=10, by=0.1)
plot(x,sin(x), col='red', type='l')
clip(1,10,-1,.5)
lines(x,sin(x), col='yellow', type='l')
Note that with this method the curve changes colors at exactly 0.5.
After you've drawn a line plot, you can color it with segments():
seq1 <- seq(from=1, to=10, by=0.1)
values <- sin(seq1)
s <- seq(length(seq1)-1)
segments(seq1[s], values[s], seq1[s+1], values[s+1], col=ifelse(values > 0.5, "red", "yellow"))
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 am able to plot data and and everything seems to work. The only problem is that R seems to decide if line markers are inserted or not. I have several different datasets, for the dataset with 1500 the plot works fine and I can see the markers. Any other dataset, all of them with 3000+ points the plot ignores all markers and just the line can be seen.
Bellow you guys can see the code used to plot the data and example plot Figures.
My question is, how can I assure that R will plot the lines with markers? Am I doing something wrong?
Thanks for your time and help.
png(filename="figures/all.normdtime.png", width=800, height=600)
plot(ecdf(data1[,10]), col="blue", ann=FALSE, pch=c(1,NA,NA,NA,NA,NA,NA,NA,NA), cex=2)
lines(ecdf(data2[,10]), col="green", pch=c(3,NA,NA,NA,NA,NA,NA,NA,NA), cex=2)
lines(ecdf(data3[,10]), col="red", pch=c(8,NA,NA,NA,NA,NA,NA,NA,NA), cex=2)
lines(ecdf(data4[,10]), col="orange", pch=c(2,NA,NA,NA,NA,NA,NA,NA,NA), cex=2)
title(xlab="Transfer rate (bytes/ms)")
title(main="ECDF Normalized Download Time")
dev.off()
No markers, 21100 points plotted
With markers, 1400 points plotted
I would try something like this:
data1 <- dnorm(seq(-5,5,.001))
x <- ecdf(data1)
plot(ecdf(data1), col="blue", ann=FALSE, pch=c(1,rep(NA,10000)), cex=2)
points(x=knots(x)[seq(1,length(knots(x)),5)], y=ecdf(data1)(knots(x)[seq(1,length(knots(x)),5)]), col="red",pch=3)
title(xlab="Transfer rate (bytes/ms)")
title(main="ECDF Normalized Download Time")
The original ECDF is not visible since we plotted approx. 1500 points.
If you want less just change the value 5 inside the x and y argument of pointsto a bigger number i.e. 100. Then we have ~70 points plotted:
I don't have your data available but I think this should work for you:
ecdf1 <- ecdf(data1[,10])
ecdf2 <- ecdf(data2[,10])
ecdf3 <- ecdf(data3[,10])
ecdf4 <- ecdf(data4[,10])
knots1 <- knots(ecdf1)
knots2 <- knots(ecdf2)
knots3 <- knots(ecdf3)
knots4 <- knots(ecdf4)
n <- 10 # every 10th point
png(filename="figures/all.normdtime.png", width=800, height=600)
plot(ecdf1, col="blue", ann=FALSE)
points(x=knots1[seq(1,length(knots1),n)], y=ecdf1(knots1[seq(1,length(knots1),n)]), col="blue",pch=1)
lines(ecdf2, col="green")
points(x=knots2[seq(1,length(knots2),n)], y=ecdf2(knots2[seq(1,length(knots2),n)]), col="green",pch=3)
lines(ecdf3, col="red",)
points(x=knots3[seq(1,length(knots3),n)], y=ecdf3(knots3[seq(1,length(knots3),n)]), col="red",pch=8)
lines(ecdf4, col="orange")
points(x=knots4[seq(1,length(knots4),n)], y=ecdf4(knots4[seq(1,length(knots4),n)]), col="orange",pch=2)
title(xlab="Transfer rate (bytes/ms)")
title(main="ECDF Normalized Download Time")
dev.off()
I either seem to be running in circles or there is something wrong with my data. I want to plot some data and use ?axisto modify the labels on the X axis.
I have two issues however:
The axis labels begin in the center of the x-axis instead of at the beginning
The axis labels do not match the data points in the plot
I would like to have X axis labels ranging from 10 to 90 by 5.
This is the code that I use and came up with so far:
values <- cbind(1:180,1)
l <- list(1:10,11:20,21:30,31:40,41:50,51:60,61:70,71:80,81:90,91:100,101:110,111:120,121:130,131:140,141:150,151:160,161:170,171:180)
# compute mean across the intervals in l
meanqual <- sapply(l, function(x) mean(values[x,1]))
meanqual
plot <- plot(meanqual, type="o", xlab="% Size of Wave", ylab="Values",xaxt='n', lty=1)
legend('bottomright', c("Values"),pch=21, lty=1, cex=1)
axis(side=1, at= seq(10,90,5))
If you only give one numeric vector to plot it "assumes" you meant to use the position or index of the values in that vector as the x values, so the plot call plotted meanqual against 1:length(meanqual). If you wanted to plot against the seq() argument you latter used in the axis call you should supply it (or rather something similar in scale with the same length as meanqual) to plot:
plot <- plot(x=seq(5,90,5), y=meanqual, type="o",
xlab="% Size of Wave", ylab="Values",xaxt='n', lty=1)
legend('bottomright', c("Values"),pch=21, lty=1, cex=1)
axis(side=1, at= seq(10,90,5), labels=seq(10,90,5))
I am trying to plot several points with error bars, with two y axes.
However at every call of the plotCI or errbar functions, a new plot is initialized - with or without par(new=TRUE) calls -.
require(plotrix)
x <- 1:10
y1 <- x + rnorm(10)
y2<-x+rnorm(10)
delta <- runif(10)
plotCI(x,y=y1,uiw=delta,xaxt="n",gap=0)
axis(side=1,at=c(1:10),labels=rep("a",10),cex=0.7)
par(new=TRUE)
axis(4)
plotCI(x,y=y2,uiw=delta,xaxt="n",gap=0)
I have also tried the twoord.plot function from plotrix, but I don't think it's possible to add the error bars.
With ggplot2 I have only managed to plot in two different panels with the same Y axis.
Is there a way to do this?
Use add=TRUE,
If FALSE (default), create a new plot; if TRUE, add error bars to an
existing plot.
For example the last line becomes:
plotCI(x,y=y2,uiw=delta,xaxt="n",gap=0,add=TRUE)
PS: hard to do this with ggplot2. take a look at this hadley code
EDIT
The user coordinate system is now redefined by specifying a new user setting. Here I do it manually.
plotCI(x,y=y1,uiw=delta,xaxt="n",gap=0)
axis(side=1,at=c(1:10),labels=rep("a",10),cex=0.7)
usr <- par("usr")
par(usr=c(usr[1:2], -1, 20))
plotCI(x,y=y2,uiw=delta,xaxt="n",gap=0,add=TRUE,col='red')
axis(4,col.ticks ='red')