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)
Related
Basically, I have trouble plotting the relative frequency histogram, as when I plot the data my y axis always becomes greater than one. I also want to superimpose a normal distribution on top however it never seems to work.
What I have produced so far: https://imgur.com/H9lWBVg
I have tried multiple methods in plotting the histogram such as hist(), truehist() and plot() etc.
truehist(aest,freq=TRUE, xlab = "Average Est", col="blue")
curve(dnorm(x,mean(aest),sd(aest)),col="red", add=TRUE, lwd=2)
legend("topright",legend=c(paste("median = ",toString(mean(aest))),paste("mean = ",toString(median(aest))),paste("SD = ",toString(sd(aest)))), cex=0.65)
You are looking for a density plot, not a frequency one. Try hist with
freq = FALSE
And you'll get the result you want. I don't have your data, but subbing some random data I have in it will look like so:
hist(move$dist,freq=FALSE, xlab = "Average Est", col="blue")
curve(dnorm(x,mean(move$dist),sd(move$dist)),col="red", add=TRUE, lwd=2)
legend("topright",
legend=c(paste("median = ",toString(mean(move$dist))),
paste("mean = ",toString(median(move$dist))),
paste("SD = ",toString(sd(move$dist)))),
cex=0.65)
Or you can do truehist, but then the parameter isn't freq it is
prob = TRUE
which will look something like this:
truehist(move$dist,prob = TRUE, xlab = "Average Est", col="blue", nbins = "fd")
curve(dnorm(x,mean(move$dist),sd(move$dist)),col="red", add=TRUE, lwd=2)
legend("topright",
legend=c(paste("median = ",toString(mean(move$dist))),
paste("mean = ",toString(median(move$dist))),
paste("SD = ",toString(sd(move$dist)))),
cex=0.65)
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 am new in R and would like to plot a normal distribution graph where the region of two standard deviation is selected by arrows, exactly as shown below.
This question lacks the effort but it piqued my interest, so there you have it:
#standard normal distribution data
x <- seq(-4, 4, length=100)
hx <- dnorm(x)
#plot a standard normal distribution
plot(x, hx, type="l", lty=2, xlab="x value")
#plot a vertical line at -2*std
abline(v=-2, col='red')
#plot a vertical line at 2*std
abline(v= 2, col='red')
#make the arrow
arrows(x0=-2, y0=0.35, x1=2, y1=0.35, code=3, col='blue')
#plot the text
text(x=0, y=0.37,labels='95%', col='red')
Result:
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 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])