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)
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 have a barplot and would like to have the label showing different colours and the bars to be grey.
I tried:
col_lab <- c("red","green","grey","red","red","blue")
barplot(1:6,names.arg=1:6,main="barplot",las=1,horiz=TRUE,col="grey",xaxt="n",col.names=col_lab)
but I got an error.
Thank you for your help.
Use yaxt="n" and do an mtext. To get the right positions, use the coordinates barplot throws invisibly.
b <- barplot(1:6, names.arg=1:6, main="barplot", las=1, horiz=TRUE, col="grey",
xaxt="n", yaxt="n")
mtext(1:6, 2, .5, at=b, las=2, col=col_lab, font=2)
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)
I am having trouble with getting a simple plot to return a dotted line (lty=2). This is a very elementary problem, but I can't seem to find the solution. I would greatly appreciate if someone could help me with this. My code is below:
par(family="serif", yaxs="i", xaxs="i")
#Empty plot with axes labeled
plot(dataset1[,6]~dataset2[,5], ann=FALSE, cex.axis=1.5, xaxs="i", yaxs="i", lty=1, type="n")
title(xlab="X axis title (%)", ylab="Y axis title", cex.lab=1.5)
axis(side=1, at=c(0,10,20, 30,40,50,60,70,80,90,100), cex.axis=1.5)
#Add curves
lines(dataset1[,6]~dataset2[,5], lty=1)
lines(dataset1[,6]~dataset2[,5], lty=2)
##### PROBLEM IS HERE WITH LTY=2 ####
The graph gets returned as 2 solid curves (rather than 1 solid and 1 dashed) and I can't figure out where the problem lies. Can someone shed some light on this?
Thanks so much.
Perhaps you could provide your data. Here is an example of a plot with lines of various types.
plot(1:10, type='b',lty=2)
lines(1:2, lty=1)
lines(2:4, lty=2)
lines(3:6, lty=3)
lines(4:7, lty=4)
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)