Draw 3 curves together in the same graph and scale - r

I need to draw three curves together in the same graph with the same scale. I know how to draw two curves together, as the following code:
r=0.8
z=2
k=seq(0,5,by=0.1)
y1=(z^2+k*r)/(r*z+k)
y2=z*(z+k*r)/(r+k)
plot(k,y1,type='l',ylab=' ',col="red",ylim=range(c(y1,y2)))
par(new=TRUE)
plot(k,y2,type='l',col="green",ylim=range(c(y1,y2)))
It works fine, but I don't know how to add the third curve, means how to set ylim.
Any help is appreciated.

Use lines
r=0.8
z=2
k=seq(0,5,by=0.1)
y1=(z^2+k*r)/(r*z+k)
y2=z*(z+k*r)/(r+k)
y3=0.8*y2
ymin=min(c(y1,y2,y3))
ymax=max(c(y1,y2,y3))
plot(k,y1,type='l',ylab='lines',col="red",ylim=c(ymin,ymax))
par(new=TRUE)
lines(k,y2,type='l',col="green",ylim=range(c(y1,y2)))
lines(k,y3)
Here is another example, with data you have provided as a comment
r=0.8
z=3
p=seq(0.1,5,by=0.1)
y1=(p*z^2+r*z)/(p*r*z+1)
y2=z*(p+r)/(p*r+1)
y3=(p^2*z*2-1+sqrt((p^2*z*2)^2+4*p^2*r*2*z*2))/(2*p*2*r*z)
ymin=-1
ymax=max(c(y1,y2,y3))
plot(p,y1,type='l',ylab='lines',col="red",ylim=c(ymin,ymax))
par(new=TRUE)
lines(p,y2,type='l',col="green",ylim=range(c(y1,y2)))
lines(p,y3, col="blue")

Related

Creating shades between a cluster of line plots in R

I am currently working with a line plot in R that would contain a large number of separate plots (which does make it difficult to read). What I would like to do instead is to somehow create a light-colored shade that captures the range of individual line plots. Would that be possible? This is what I have for my plot:
plot(get,Hope2, type="l",col="red", lwd="3", xlab="Cumulative CO2
emissions (TtC)", ylab="One-day maximum precipitation (mm/day)",
main="One-day maximum precipitation for South Sudan for CanESM2
scenarios")
lines(get2.teratons, Hope3, type="l", lwd="3", col="red")
lines(get3.teratons, Hope4, type="l", lwd="3", col="red")
lines(get4.teratons, Hope5, type="l", lwd="3", col="red")
So, this gives 4 separate red lines on the same plot (I am likely going to place up to 10 lines, so you can imagine how cluttered that would look without shading them in the background!). Now, let's say that I wanted to create a light red shade that fills from the upper to lower red curves. How should I go about doing that? The idea would be to capture all of the line plots by doing something like this:
http://jvoigts.scripts.mit.edu/blog/assets/plot_shaded_pretty.png
Thanks, and I would greatly appreciate any help!

Contour plot via Scatter plot

Scatter plots are useless when number of plots is large.
So, e.g., using normal approximation, we can get the contour plot.
My question: Is there any package to implement the contour plot from scatter plot.
Thank you #G5W !! I can do it !!
You don't offer any data, so I will respond with some artificial data,
constructed at the bottom of the post. You also don't say how much data
you have although you say it is a large number of points. I am illustrating
with 20000 points.
You used the group number as the plotting character to indicate the group.
I find that hard to read. But just plotting the points doesn't show the
groups well. Coloring each group a different color is a start, but does
not look very good.
plot(x,y, pch=20, col=rainbow(3)[group])
Two tricks that can make a lot of points more understandable are:
1. Make the points transparent. The dense places will appear darker. AND
2. Reduce the point size.
plot(x,y, pch=20, col=rainbow(3, alpha=0.1)[group], cex=0.8)
That looks somewhat better, but did not address your actual request.
Your sample picture seems to show confidence ellipses. You can get
those using the function dataEllipse from the car package.
library(car)
plot(x,y, pch=20, col=rainbow(3, alpha=0.1)[group], cex=0.8)
dataEllipse(x,y,factor(group), levels=c(0.70,0.85,0.95),
plot.points=FALSE, col=rainbow(3), group.labels=NA, center.pch=FALSE)
But if there are really a lot of points, the points can still overlap
so much that they are just confusing. You can also use dataEllipse
to create what is basically a 2D density plot without showing the points
at all. Just plot several ellipses of different sizes over each other filling
them with transparent colors. The center of the distribution will appear darker.
This can give an idea of the distribution for a very large number of points.
plot(x,y,pch=NA)
dataEllipse(x,y,factor(group), levels=c(seq(0.15,0.95,0.2), 0.995),
plot.points=FALSE, col=rainbow(3), group.labels=NA,
center.pch=FALSE, fill=TRUE, fill.alpha=0.15, lty=1, lwd=1)
You can get a more continuous look by plotting more ellipses and leaving out the border lines.
plot(x,y,pch=NA)
dataEllipse(x,y,factor(group), levels=seq(0.11,0.99,0.02),
plot.points=FALSE, col=rainbow(3), group.labels=NA,
center.pch=FALSE, fill=TRUE, fill.alpha=0.05, lty=0)
Please try different combinations of these to get a nice picture of your data.
Additional response to comment: Adding labels
Perhaps the most natural place to add group labels is the centers of the
ellipses. You can get that by simply computing the centroids of the points in each group. So for example,
plot(x,y,pch=NA)
dataEllipse(x,y,factor(group), levels=c(seq(0.15,0.95,0.2), 0.995),
plot.points=FALSE, col=rainbow(3), group.labels=NA,
center.pch=FALSE, fill=TRUE, fill.alpha=0.15, lty=1, lwd=1)
## Now add labels
for(i in unique(group)) {
text(mean(x[group==i]), mean(y[group==i]), labels=i)
}
Note that I just used the number as the group label, but if you have a more elaborate name, you can change labels=i to something like
labels=GroupNames[i].
Data
x = c(rnorm(2000,0,1), rnorm(7000,1,1), rnorm(11000,5,1))
twist = c(rep(0,2000),rep(-0.5,7000), rep(0.4,11000))
y = c(rnorm(2000,0,1), rnorm(7000,5,1), rnorm(11000,6,1)) + twist*x
group = c(rep(1,2000), rep(2,7000), rep(3,11000))
You can use hexbin::hexbin() to show very large datasets.
#G5W gave a nice dataset:
x = c(rnorm(2000,0,1), rnorm(7000,1,1), rnorm(11000,5,1))
twist = c(rep(0,2000),rep(-0.5,7000), rep(0.4,11000))
y = c(rnorm(2000,0,1), rnorm(7000,5,1), rnorm(11000,6,1)) + twist*x
group = c(rep(1,2000), rep(2,7000), rep(3,11000))
If you don't know the group information, then the ellipses are inappropriate; this is what I'd suggest:
library(hexbin)
plot(hexbin(x,y))
which produces
If you really want contours, you'll need a density estimate to plot. The MASS::kde2d() function can produce one; see the examples in its help page for plotting a contour based on the result. This is what it gives for this dataset:
library(MASS)
contour(kde2d(x,y))

R: Creating graphs with two y-axes

I'm looking to display two graphs on the same plot in R where the two graphs have vastly different scales i.e. the one goes from -0.001 to 0.0001 and the other goes from 0.05 to 0.2.
I've found this link http://www.statmethods.net/advgraphs/axes.html
which indicates how to display two y axes on the same plot, but I'm having trouble.
My code reads as follows:
plot(rateOfChangeMS[,1],type="l",ylim=c(-0.01,.2),axes = F)
lines(ratios[,1])
x = seq(-0.001,0.0001,0.0001)
x2 = seq(0.05,0.2,0.01)
axis(2,x)
axis(4,x2)
The problem I'm having is that, although R shows both axes, they are not next to each other as I would like, with the resulting graph attached. The left axis is measuring the graph with the small range, while the right is measuring the graph from 0.05 to 0.2. The second graph is, in fact, on the plot, but the scaling is so small that you can't see it.
Not sure if there is some etiquette rule I'm violating, never uploaded an image before so not quite sure how best to do it.
Any help would be greatly appreciated!
Thanks
Mike
Since you don't provide a reproducible example, or a representative dataset, this is a partial answer.
set.seed(1)
df <- data.frame(x=1:100,
y1=-0.001+0.002/(1:100)+rnorm(100,0,5e-5),
y2=0.05+0.0015*(0:99)+rnorm(100,0,1e-2))
ticks.1 <- seq(-0.001,0.001,0.0001)
ticks.2 <- seq(0.05,0.2,0.01)
plot(df$x, df$y1, type="l", yaxt="n", xlab="X", ylab="", col="blue")
axis(2, at=ticks.1, col.ticks="blue", col.axis="blue")
par(new=T)
plot(df$x, df$y2, type="l", yaxt="n", xlab="", ylab="", col="red")
axis(4, at=ticks.2, col.ticks="red", col.axis="red")
The reason your left axis is compressed is that both axes are on the same scale. You can get around that by basically superimposing two completely different plots (which is what having two axes does, after all). Incidentally, dual axes like this is not a good way to visualize data. It creates a grossly misleading visual impression.

How to draw a smooth curve passing through some points

I have
plot(rnorm(120), rnorm(120), col="darkblue", pch=16, xlim=c(-3,3), ylim=c(-4,4))
points(rnorm(120,-1,1), rnorm(120,2,1), col="darkred", pch=16)
points(c(-1,-1.5,-3), c(4,2,0), pch=3, cex=3)
I want to delineate a part of a graph, by drawing a smooth curve passing through a set of points.I can define 3-4 set of points but i cannot define a function. I would like to do this in R (as opposed to GIMP) as I would like to submit as SVG. What I would like to achieve is the following
Is this possible? I know this is not a sophisticated graphing question but any base R solution will do.
if I understood the question right, drawing a spline through control points should do the job:
xspline(c(-1,-1.5,-3), c(4,2,0), shape = -1)

Graphics using plot.mc

I need help with some of the graphics on a cumulative distribution plot using plot.mc
An example of what I am doing is as follows:
Require(mc2d)
ndvar(10000)
test<-mcstoc(rbetagen,type="V",shape1=48, shape2=66.42, min=0, max=4000)
hist(test)
test<-mc(test)
plot(test)
This plots the cumulative distribution of "test".
What I want to do, is draw a verticle line up from a specific point on the x-axis (say 1800), which touches the cumulative distribution line, then draws a line across to the Y axis at the point corresponding to 1800 on the x axis.
I have looked up help(plot.mc) and help(plot.stepfun) but neither seem to be able to do this.
Many thanks,
Timothy
See ?arrows
plot(test)
arrows(x0=0, y0=.745, x1=1800, y1=.745, code=0, lty=2)
arrows(x0=1800, y0=0, x1=1800, y1=.745, code=0, lty=2)

Resources