I basically want to shade an area behind a graph.
It's easy enough to do in linear scale.
x <- 1:20
y <- x^2
plot(x, y, type="l")
polygon(c(10,10,15,15),
c(-100,600,600,-100),
col=rgb(0,1,0,0.3),border=FALSE)
Produces this:
But once you put y on a log scale,
plot(x, y, type="l", log="y")
polygon(c(10,10,15,15),
c(-100,600,600,-100),
col=rgb(0,1,0,0.3),border=FALSE)
Nothing shows up.
Be careful when playing with log = "y". If your y value is negative, you get NaN. This is exactly what happened here. Try
plot(x, y, type="l", log="y")
polygon(c(10,10,15,15),
c(1e-7,600,600,1e-7), ## log(1e-7) is small enough
col=rgb(0,1,0,0.3),border=FALSE)
Related
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)
The graph is smashed into about 10% of the total area available.
I have not encountered this before.
What is going on?
How do I produce a reasonably sized graph?
# This is a plot
par(mfrow=c(1,1))
x<-1:10; y=x*x
plot(x, y, type="b")
# Here is how the plot shrinks
par(mfrow=c(2,2))
x<-1:10; y=x*x
plot(x, y, type="b")
# With dev.off() you clean the settings and the graph should be big again
x<-1:10; y=x*x
plot(x, y, type="b")
vs.
x <- sin(1:20)
y <- (dplyr::lag(x)-x)-x
plot(x, y, type="l")
Yields this plot:
How do I get a smooth, cyclical trajectory? The smoothing functions I've tried all want to make a single smooth function.
Example 1 (throws an error):
lines(smooth.spline(x, y))
Example 2 (draws a function):
lo <- loess(y~x)
lines(predict(lo), col='red', lwd=2)
Example 3 (draws a function):
xspline(c(x,y), shape=1, border='blue' )
I think you're asking for both (a) smoother curves and (b) cylindrical/symmetric circles, correct?
Increase the count of points to make it smoother. Fix the aspect ratio to make it a circle (i.e., asp=1).
x <- sin((1:20000) / 1000)
y <- cos((1:20000) / 1000)
plot(x, y, type="l", asp=1)
EDIT, responding to OP's comments below.:
This uses dplyr::lag(), while increasing the number of points. Don't conceptually shift the lag though (i.e., y should be a full value of 1 from x).
(x=1, y=0), (x=1.001, y=0.001), ..., (x=20, y=19)
x <- sin((1:20000) / 1000)
y <- (dplyr::lag(x, 1000)-x)-x
plot(x, y, type="l", asp=1)
If that's not what you want, maybe sketch a picture and attach in (in like MS Paint or Inkscape).
I am trying to get only power 10 scale on the y axis, and I have tried many approaches offered on StackOverflow but none produce what I want.
I want to define the scale of y-axis to start from 10^2 and then next tick be at 10^4 and then on 10^6.
I have tried xaxt and at and axis and everything mentioned here, but none work for 10^x..
The plot.default() function provides a log argument which can be used to easily get a logarithmic scale of the x-axis, y-axis, or both. For example:
x <- 2:6;
y <- 10^x;
plot(x,y,log='y');
If you want to control the y-axis ticks, you can override the default axis and draw your own in the normal way:
## generate data
x <- 2:6;
y <- 10^x;
## precompute plot parameters
xlim <- c(2,6);
ylim <- c(10^2,10^6);
xticks <- 2:6;
yticks <- 10^seq(2L,6L,2L);
## draw plot
plot(NA,xlim=xlim,ylim=ylim,log='y',axes=F,xaxs='i',yaxs='i',xlab='x',ylab='y');
abline(v=xticks,col='lightgrey');
abline(h=yticks,col='lightgrey');
axis(1L,xticks,cex.axis=0.7);
axis(2L,yticks,sprintf('10^%d',as.integer(log10(yticks))),las=2L,cex.axis=0.7);
##axis(2L,yticks,sprintf('%.0e',yticks),las=2L,cex.axis=0.7); ## alternative
points(x,y,pch=19L,col='red',xpd=NA);
axis() works:
x <- 2:6
y <- 10 ^ x
plot(x, y, yaxt = "n")
axis(2, at = 10^(c(2, 4, 6)))
The only problem is that, this is certainly not a good way for presentation. Do you know how fast 1e+n grows?? As you have already seen in the plot, the ticks for 1e+2 and 1e+4 almost coincide, because 1e+6 is so big. If your data range from 1 ~ 1e+8 or even greater, then you'd better plot them on log scale.
plot(x, log10(y), yaxt = "n")
axis(2, at = c(2,4,6))
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