I'm trying to plot a histogram with two superimposed curve fits in R. So far, everything is working but the legend. I want a single legend that includes the boxes for the histogram and the lines for the fitted curves. I can get it working with the code below, but this creates two separate legends. Is it possible to combine the legends into a single box?
truehist(
data=DismissalRateData$DismissalRate,
breaks=seq(.6, 1, by=.025),
col="lightgreen",
prob=TRUE,
ylab="Density",
xlab="Claim Dismissal Rate",
main="Histogram of Historical Claim Dismissal Rates \n(With Superimposed Beta Curve Fits)",
xaxt='n',
yaxt='n',
ylim=c(0,14)
)
curve(
dbeta(x, FittedAlphaParameter, FittedBetaParameter),
add=TRUE,
col="red",
lwd=3
)
curve(
dbeta(x, 100.2753, 11.5809),
add=TRUE,
col="blue",
lwd=3
)
axis(
side=1,
at=seq(.6, 1, by=.025),
labels=formattable::percent(seq(.6, 1, by=.025),digits=0)
)
legend(x="topleft", legend="Actual Data", fill="lightgreen", col="black")
legend(x="left" ,
legend=c("Beta fitted with all data 2006-2017", "Beta fitted excluding 2006-2008"),
col=c("red", "blue"),
lty=1, lwd=3)
Related
I am using the histbackback function from the Hmisc package to generate two back-to-back histograms.
I managed to add colors to the bars, but I did not manage to add density lines to both histograms.
Could someone help me out?
Here is the code I have tried so far.
library(Hmisc)
hbb <- histbackback(pre, post, brks=seq(0.0001, 0.20, by=0.01))
barplot(-hbb$left, col="lightgrey", horiz=TRUE, space=0, add=TRUE, axes=FALSE)
barplot(hbb$right, col="darkgrey", horiz=TRUE, space=0, add=TRUE, axes=FALSE)
# works until here but now I cannot add lines
lines(density(pre), # density plot,
horiz=TRUE, space=0, add=TRUE, axes=FALSE)
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 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;
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)