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;
Related
I have a question regarding LHS/PRCC in R. I need to change the "points" in my graph to become a "bar/histogram bar" (see the photo). I have solved the LHS/PRCC and plot the values but I require a bar plot of the coefficients. Below is my code.
Below is my code:
plot(summary$original, main='Partial rank correlation coefficients', ylim=c(-1,1),
xlab='', ylab='Coefficient',
axes=FALSE)
axis(2)
axis(1, at=seq(1:8), labels=c("A",expression(lambda[d]),expression(sigma[d]),expression(m[d]),expression(k[d]),expression(mu[d]),'c',expression(beta[d])), las=1)
mtext(text='Parameter', side=1, line=2.5)
box()
#for(i in 1:8) lines(c(i,i),c(summary[i,4], summary[i,5])) #the lines for CI
abline(h=0)
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.
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)
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)
I have two univariate time series that would like to plot in the same screen chart. The problem is that they have very different scales and therefore the chart becomes very difficult to interpret. How can I plot each series superimposed but each of them using a different vertical axis?
library(xts)
mytime <- as.POSIXlt(seq(Sys.time()-100*60+1,Sys.time(),by=60), origin= '1970-01-01')
x <- xts(rnorm(1:100),mytime)
y <- xts(rnorm(1:100,100,10),mytime)
plot(as.zoo( merge(x,y)), screens=1)
I'm not so sure this is what you want, but here's an idea:
plot(as.zoo(x), las=1)
par(new=TRUE)
plot(as.zoo(y),
col=2,
bty='n',
xaxt="n",
yaxt="n",
xlab="", ylab="")
axis(4, las=1)
legend("topleft",
legend=c("x","y"),
col=1:2,
lty=1,
cex=0.85)