R: Using "segment" command to put vertical lines between plotted points - r

I have a small dataframe from which I am plotting 3 columns in order to display a risk estimate and the 95% confidence intervals. Right now I have those 3 sets of vectors displayed as points, but I would like to connect them using "segment".
Here is a sample of the dataframe being plotted:
Diagnosis age.group X..change X..lower X..upper
1 Dysrythmia All adults 16 0 35
2 Heart failure All adults -4 -20 14
3 Asthma All adults 10 -5 28
Here is my plot code:
plot(dt[,4], pch="-", ylim=c(-20, 50), axes=F, ann=F, cex=1.5)
abline(h=0, col=1, lty=2)
points(dt[,3], pch=16, col="black", bg="black" )
points(dt[,5], pch="-", cex=1.5)
axis(1, at=1:10, lab=dt[,1], las=3, lwd=0, cex.axis=0.7, pos=-22)
axis(2, at=5*-20:54, las=1, cex.axis=0.7, cex.lab=0.7, col=1)
title(main="Risk estimates: All Adults", col.main="black", font.main=1)
title(ylab="Increase in risk (%)", col.lab=rgb(0,0.5,0))
box()
The points are the estimates and the dashes are the confidence intervals. I want to connect these three points for each diagnosis. I've looked at the R notation but it doesn't help me figure out how to tell R which xy "coordinates" I want to draw the segments connecting, because I have used vectors here instead of values? Can anyone help write a segment line of code? Thank you

OK, so you basically want to plot CIs. The arrows command is probably better for what you want. Here's a brief example that works with what you have for the lower CI.
plot(dt[,3], pch="•", ylim=c(-20, 50), axes=F, ann=F, cex=1.5, bty = 'o')
abline(h=0, col=1, lty=2)
arrows(1:3, dt[,3], 1:3, dt[,4], angle = 90, length = 0.08)
You can therefore leave out the extra points commands and any new segments commands. It's all much more concise.
But, if you insist on adding segments to what you have it's just...
segments(1:3, dt[,3], 1:3, dt[,5])
segments(1:3, dt[,3], 1:3, dt[,4])

Related

Dual Y-Axis Base R Plot with different x-ticks

I want to make a plot of 4 sets of data points using dual y-axis. The first two are on the left y-axis and last two are on the right y-axis. The first two belong to a set of numbers ranging from 5000 to 50,000. Second two sets of data belong range from 1-100. I want to plot it so that it is easily discernable that the two axis are not only on different scales but also the height between points from the two different sets with distinct ranges is obviously big. I don't want to be able to draw a horizontal line that would suggest some number from the left-y-axis can be mapped bijectively to some number on the right y-axis. I want to make it such that a horizontal line through any points from the left y-axis and right y-axis would belong to only one set related to either left or right axis.
How can I plot with 2 different y-axes?. There's
I'd use twoord.plot
From plotrix v3.7-5
by Jim Lemon but that has the disadvantage than base R beacause I can't add 4 sets of data into one plot. I can only use 2 sets of (x,y) pairs with 2--ord plot. I can theoretically plot n sets of (x,y) pairs using base R.
None
Here's what doesn't work:
time <- seq(0,72,12)
betagal.abs <- c(0.05,0.18,0.25,0.31,0.32,0.34,0.35)
cell.density <- c(0,1000,2000,3000,4000,5000,6000)
## add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 6) + 0.1)
## Plot first set of data and draw its axis
plot(time, betagal.abs, pch=16, axes=FALSE, ylim=c(0,1), xlab="", ylab="",
type="b",col="black", main="Mike's test data")
axis(2, ylim=c(0,1),col="black",las=1) ## las=1 makes horizontal labels
mtext("Beta Gal Absorbance",side=2,line=2.5)
box()
## Allow a second plot on the same graph
par(new=TRUE)
## Plot the second plot and put axis scale on right
plot(time, cell.density, pch=15, xlab="", ylab="", ylim=c(0,7000),
axes=FALSE, type="b", col="red")
## a little farther out (line=4) to make room for labels
mtext("Cell Density",side=4,col="red",line=4)
axis(4, ylim=c(0,7000), col="red",col.axis="red",las=1)
## Draw the time axis
axis(1,pretty(range(time),10))
mtext("Time (Hours)",side=1,col="black",line=2.5)
## Add Legend
legend("topleft",legend=c("Beta Gal","Cell Density"),
text.col=c("black","red"),pch=c(16,15),col=c("black","red"))
Not quite sure what you're after but you can add an extra 'line per plot' by using lines.
I've edited your code
## Plot first set of data and draw its axis
plot(time, betagal.abs, pch=16, axes=FALSE, ylim=c(0,1), xlab="", ylab="",
type="b",col="black", main="Mike's test data")
lines(seq(0, 1, 0.02), type = 'o')
axis(2, ylim=c(0,1),col="black",las=1) ## las=1 makes horizontal labels
mtext("Beta Gal Absorbance",side=2,line=2.5)
box()
## Allow a second plot on the same graph
par(new=TRUE)
## Plot the second plot and put axis scale on right
plot(time, cell.density, pch=15, xlab="", ylab="", ylim=c(0,7000),
axes=FALSE, type="b", col="red")
lines(seq(0, 5000, 10), type = 'o', col = 'red')
## a little farther out (line=4) to make room for labels
mtext("Cell Density",side=4,col="red",line=4)
axis(4, ylim=c(0,7000), col="red",col.axis="red",las=1)
which produces this:
Please let me know if this wasn't what you were after.

putting a trend line and r squared value in a graph

I made line graphs of two different scales and i want to draw two trend lines in each line graph and display r squared value in the graph. R squared value of temperature and Year, precipitation and year. I am also having problem on showing legend on the precipitation axis. Please help me fix it.
library(lattice)
data=read.table("temp_pcp.csv",header=TRUE, sep=",")
frame=data.frame(data[1:3])
year <- data[1]
Temperature <- data[3]
Precipitation <- data[2]
gm<-data.frame(year,Temperature)
mg<-data.frame(year,Precipitation)
plot(gm, axes=FALSE, ylim=c(0,20), xlab="", ylab="",
type="l",col="black", main="Climograph")
axis(2, ylim=c(0,20),col="black",las=1)
mtext("Temperature in Degree Celcius ",side=2,line=2.5)
box()
grid()
par(new=TRUE)
plot(mg, xlab="", ylab="", ylim=c(0,60),
axes=FALSE, type="l", col="red")
mtext("Precipitation in mm",side=4,col="black",line=4)
axis(4, ylim=c(0,60), col="black",col.axis="black",las=1)
axis(1,pretty(range(year),10))
mtext("Year",side=1,col="black",line=2.5)
legend("topleft",legend=c("Tempreature","Precipitation"),
text.col=c("black","red"),col=c("black","red"))
The line graph i created is as follow;

Bid Rent Curves - Plotting Circles of Projected Radii from Another Dimension

The goal is to reproduce this Bid-Rent graph in R:
The challenge is to draw the projected circles. So far I got:
The 2D part is created by the R code below with the traditional graphic system in base R:
#Distance
X <- seq(0,7,1)
#Bid Rent Curves: Commercial, Industrial, Residential
com <- -5*X + 10
ind <- -2*X + 7
res <- -0.75*X + 4
graph <- plot(X, com, type="l", col="green", ylim=c(0,10), xlab="", ylab="", axes=FALSE)
lines(X, ind, col="red")
lines(X, res, col="blue")
abline(v=0, h=0)
segments(1,0, 1,5, lty=2)
segments(2.5,0, 2.5,2, lty=2)
title(main="Bid Rent Curves", sub="Alonso Model",
xlab="Distance from CBD", ylab="Rent per m2")
text(2.5,7.5, "Commercial", col="green")
text(3.5,4, "Industrial", col="red")
text(5.5,2, "Residential", col="blue")
(Detail: Why the curves do not respect the ylim = 0 ?)
How make the projection and draw the semi-circles?
It is not exactly a 3D plot. I have looked into plot3D and rgl. I am not sure which packages or strategy to use from here.
I'm taking you at your word that you want circles, so you need to push the plot area into the upper right corner:
outHalfCirc <- function(r,colr) {opar=par(xpd=TRUE, new=TRUE) #plot ouside plot area
polygon(x=seq(r,-r,by=-0.1),
y= -sqrt(r^2 - seq(r,-r,by=-0.1)^2) , # solve r^2 = x^2 +y^2 for y
xlim =c(0,7 ), ylim=c(0,10), col=colr, # need xlim and ylim to match base plot ranges
yaxs="i", yaxt="n", xaxs="i") # yaxis off; x and y axes meet at origin
par(opar)}
Then push plot up and to the right: This will draw a colored half-circles (largest first so they overlap) below the y=0 line.
png() # send to image file; not needed for testing
opar <- par(mar=c(15, 15, 2,2) ) # default units are in widths of text-"line".
# the margins start at lower, then clockwise
# run your code
outHalfCirc(5.5, "blue")
outHalfCirc(2.5, "red")
outHalfCirc(1, "green")
dev.off() # complete image production
par(opar) # different than the 'opar' inside the function
Voila! Although not really circles because the aspect ratio is not 1. That can be fixed (or you could set the xlim and ylim to be equal.

Adding nonlinear line with stacked bar plot in r

I would like to add a curved line to fit the dark bars of this supply cost curve (like the red line that appears in image). The height of the dark bars represent the range in uncertainty in their costs (costrange). I am using fully transparent values (costtrans) to stack the bars above a certain level
This is my code:
costtrans<-c(10,10,20,28,30,37,50,50,55,66,67,70)
costrange<-c(15,30,50,21,50,20,30,40,45,29,30,20)
cost3<-table(costtrans,costrange)
cost3<-c(10,15,10,30,20,50,28,21,30,50,37,20,50,30,50,40,55,45,66,29,67,30,70,20)
costmat <- matrix(data=cost3,ncol=12,byrow=FALSE)
Dark <- rgb(99/255,99/255,99/250,1)
Transparent<-rgb(99/255,99/255,99/250,0)
production<-c(31.6,40.9,3.7,3.7,1,0.3,1.105,0.5,2.3,0.7,0.926,0.9)
par(xaxs='i',yaxs='i')
par(mar=c(4, 6, 4, 4))
barplot(costmat,production, space=0, main="Supply Curve", col=c(Transparent, Dark), border=NA, xlab="Quantity", xlim=c(0,100),ylim=c(0, 110), ylab="Supply Cost", las=1, bty="l", cex.lab=1.25,axes=FALSE)
axis(1, at=seq(0,100, by=5), las=1, cex.axis=1.25)
axis(2, at=seq(0,110, by=10), las=1, cex.axis=1.25)
Image to describe what I am looking for:
I guess it really depends how you want to calculate the line...
One first option would be:
# Save the barplot coordinates into a variable
bp <- barplot(costmat,production, space=0, main="Supply Curve",
col=c(Transparent, Dark), border=NA, xlab="Quantity",
xlim=c(0,100), ylim=c(0, 110), ylab="Supply Cost", las=1,
bty="l", cex.lab=1.25,axes=FALSE)
axis(1, at=seq(0,100, by=5), las=1, cex.axis=1.25)
axis(2, at=seq(0,110, by=10), las=1, cex.axis=1.25)
# Find the mean y value for each box
mean.cost <- (costmat[1,]+colSums(costmat))/2
# Add a line through the points
lines(bp, mean.cost, col="red", lwd=2)
Which gives
Now, you could do some smoother line, using some sort of regression
For instance, using a LOESS regression.
# Perform a LOESS regression
# To allow for extrapolation, you may want to add
# control = loess.control(surface = "direct")
model <- loess(mean.cost~bp, span=1)
# Predict values in the 0:100 range.
# Note that, unless you allow extrapolation (see above)
# by default only values in the range of the original data
# will be predicted.
pr <- predict(model, newdata=data.frame(bp=0:100))
lines(0:100, pr, col="red", lwd=2)

Histogram with density fit and bars labeled with frequency

I would like to show the probability for the histogram, with a density curve fit, and with the bars labeled by the count. The code below generates two figures, the top shows the frequency bars (labeled by frequency) with the density curve. The bottom shows the probability bars (labeled by probability) with the density curve. What I would like to have is the probability bars labeled by frequency, so we can read probability and frequency. Or, I would like to have the second plot, with the bar labels from the first plot.
coeff_value = c(6.32957806, 3.04396650, 0.02487562, 3.50699592, 5.03952569, 3.05907173,
0.41095890, 1.88648325, 5.04250569, 0.89320388, 0.83732057, 1.12033195,
2.35697101, 0.58695652, 4.83363583, 7.91154791, 7.99614644, 9.58737864,
1.27358491, 1.03938247, 8.66028708, 6.32458234, 3.85263158, 1.37299546,
0.53639847, 7.63614043, 0.51502146, 9.86557280, 0.60728745, 3.00613232,
6.46573393, 2.60848869, 2.34273319, 1.82448037, 6.36600884, 0.70043777,
1.47600793, 0.42510121, 2.58064516, 3.45377741, 6.29475205, 4.97536946,
2.24637681, 2.12000000, 1.92792793, 0.97613883, 6.01214190, 4.47316103,
1.87272727, 10.08896797, 0.09049774, 1.93779904, 6.53444676, 3.46590909,
6.52730822, 7.23229671, 4.91740279, 5.24545125)
h=hist(coeff_value,plot=F,freq=T,breaks=10)
h$density = h$density*100
par(mfrow=c(2,1))
plt=plot(h, freq=T, main="Freq = T",xlab="rate",
ylab="Frequency", xlim=c(0, 20), ylim=c(0, 30),
col="gray", labels = TRUE)
densF=density(coeff_value)
lines(densF$x, densF$y*length(coeff_value), lwd=2, col='green')
plt=plot(h, freq=F, main="Freq = F",xlab="rate",
ylab="Probability (%)", xlim=c(0, 20), ylim=c(0, 30),
col="gray", labels = TRUE)
densF=density(coeff_value)
lines(densF$x, densF$y*100, lwd=2, col='green')
paste("bar sum =",sum(h$density))
paste("line integral =",sum((densF$y[-length(densF$y)]*100)*diff(densF$x)))
Just plot your histogram and capture the output (you'll still need to multiply the density by 100 to get to % before plotting):
h <- hist(coeff_value,plot=F,breaks=10)
h$density <- h$density*100
plot(h, freq=F, xlab="rate",
ylab="Probability (%)", ylim=c(0, 25),
col="gray")
densF <- density(coeff_value)
lines(densF$x, densF$y*100, lwd=2, col='green')
Now h contains all the information you need:
text(h$mids,h$density,h$counts,pos=3)

Resources