legend outside plot in R and use pty="s" - r

I want to plot a roc curve in R but with legends outside the plot.
I want the axes labels to be from 0-1. So I used pty="s" but I am trying to fit the legend its not working.
par(pty="s",xpd=T, mar=par()$mar+c(0,0,0,3))
plot(g1,main="BPH vs G6", lty=1,xlab="Specificity", ylab="Sensitivity",
lwd=2,cex.main=2,font.axis=2,font.lab=2,cex.lab=2,cex.axis=1.2)
lines(g2, lty=1, col="red", lwd=2)
legend(-0.1,0.8,
legend=c("301+CN","CN"),
lty=c(1,1),col=c("red", "black"),cex=0.5)
I do not want the diagonal line to go outside the plot. Also, I want to increase the legend size, but by adjusting cex its not fitting here.

Related

Forcing the size of a barplot in R

I plotted this in R using barplot:
Is there a way, to force the plot to be higher, or each of the "boxes" to be higher?
This is my code:
barplot(setNames(rev(c(100,100,...)), rev(names(data))),cex.names=0.6, las=2, horiz=TRUE, width=20)

Y-Axis positions of barplot and base plot do not match

I was trying to plot a climate diagram and ran into the following problem:
After using barplot(...) for precipitation I superimposed another plot for the temperature. It is necessary for climate diagrams that the two y-axes (mm, °C) align at zero and that the precipitation/temperature ratio is 2:1 (e.g. 20mm precipitation corresponds to 10°C).
The problem: barplot(...) draws the axis to the plot's box while plot(...) leaves some space between the box and the axis margins.
Here is a simplified example. From the grid lines you see that the 0-values do not align:
barplot(0:10)
grid(col=1)
par(new=TRUE)
plot(0:10, xlim=c(-2,14), axes=FALSE)
axis(4,at=c(0:10), labels=c(0:10))
How can I get the right position and scaling of the two axes?
Don't use par(new = TRUE):
barplot(0:10)
grid(col=1)
lines(0:10, type = "p")
axis(4,at = c(0:10), labels = seq(0,20,2))
The function lines() is the right one here. The argument type = p is needed to plot points.
You need to adjust the y-values for the temperature, but now the second y-axis is in the right way, I think.

Add horizontal error bars on a single point on a graph (R)

I have plotted a graph using
plot(data.exoplanets$loga, data.exoplanets$logMass, ylab="Log of Mass", xlab="Log of Semi Major Axis")
highlighted a single point using
points(data.exoplanets$loga[1535], data.exoplanets$logMass[1535], col="red", pch=19)
and used this to generate a vertical set of error bars, where KepError was the error.
lines(rep(data.exoplanets$loga[1535],2), c(data.exoplanets$loga[1535]-KepError, data.exoplanets$loga[1535]+KepError), col="red", type="o", pch="_")
How can I tweak this to give horizontal error bars for the x-axis?
This will produce x-axis error bars.
lines(c(data.exoplanets$loga[1535]-xerr,data.exoplanets$loga[1535]+xerr),
rep(data.exoplanets$logMass[1535],2), col="red", type="o", pch="|")

R: Two axis chart adjustments

I am trying to plot a chat with two axis, here is the code and attached is the plot,
I have to do two adjustments to it.
I want to plot a line with dots and dots should be middle of the bars
Adjusting right side axis(i.e axis(4)) tick marks should align with left side axix(i.e axis(2))
Code:
Region=c("North","South","East","West")
Sales=sample(500:1000,4)
Change=sample(1:10,4)/10
names(Sales)=Region
names(Change)=Region
barplot(Sales,ylim=c(0,1000))
par(new=T)
plot(Change,type="b",axes=F,ylim=c(0,1))
axis(4)
box()
Regards,
Sivaji
First, save your barplot as some object. So you will get coordinates of the middle points. Then to add line you can use also function lines() and just multiply Change values with 1000.
Then for axis() function supply at= values and labels= the same as at=, just divided by 1000.
x<-barplot(Sales,ylim=c(0,1000))
lines(x,Change*1000,type="b")
axis(4,at=seq(0,800,200),labels=seq(0,800,200)/1000)
You need to play to set the same x-axis in the second plot, you get this info from par("usr").
The xaxs="i" is to set the xlim exactly, by default R increase the xlim a bit to make it better looking.
par(mar=c(5,5,2,5)) # change margins
x = barplot(Sales, ylim=c(0,1000)) # barplot, keep middle points of bars
mtext("Sales", 2, line=3) # first y-axis label
xlim = par("usr")[1:2] # get xlim from plot
par(new=TRUE)
plot.new() # new plot
plot.window(xlim=xlim, ylim=c(0,1), xaxs="i", yaxs="i") # new plot area, same xlim
lines(x,Change,type="b") # the lines in the middle points
axis(4) # secondary y-axis
mtext("Change", 4, line=3) # secondary y-axis label
box()

Legend box width not correct when using par

I have the problem , that my legend is too large, my code:
par(mfrow=c(1,2))
hist(alvsloss,breaks = 100, freq=F,main="Histogramm,
density curve (gaussian kernel) \n and fitted normal distribution of Allianz simple losses ",xlim=c(-0.15,0.15),xlab="loss",ylab="density",cex.axis=1.2,cex.lab=1.2)
lines(density(alvsloss), col="black", lwd=2)
curve(dnorm(x, mean = mean(alvsloss), sd = sd(alvsloss)), add=TRUE, col="black",lwd=2,lty="dotted")
legend(-0.155, 30, c("(Gaussian) Kernel density","fitted normal distribution"),lwd=2, cex=0.8,
col=c("black","black"), lty=1:2)
qqnorm(alvsloss,main="normal QQ Plot",cex.axis=1.2,cex.lab=1.2)
qqline(alvsloss)
This gives the following picture:
The problem is, that the legend on the left is too big, how can I control the width of the box? The box is way too large.
data can be found here: http://uploadeasy.net/upload/ocafq.rar
The white space on the right of you legend tells me that you manually widened your plot window. Legends do not scale well when it comes to manual re-sizing.
The solution is opening a plot of the exact size you need before plotting. In Windows, this is done with windows(width=10, height=8). Units are in inches. The surrounding box should now be tighter with the text.
If this is still not satisfactory, you should try:
Reducing the font size of the legend cex=0.7
Removing the box around the legend bty = "n" and using \n to
split your legend onto several lines
You can put your legend even more on the left using "topleft"
instead of coordinates
Here's how I would do it:
legend("topleft",
legend=c("(Gaussian)\nKernel\ndensity","Fitted\nnormal\ndistribution\n"),
bty = "n",lwd=2, cex=0.7, col=c("black","black"), lty=1:2)

Resources