Forcing the size of a barplot in R - 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)

Related

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

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.

Create scatter plot with third dimension and multiple colors

Purpose
Create scatter plot with third dimension and multiple colors.
First:
- 3rd dimension with another scale in contrast to y-axis
- create two colors (this is done using col, see code)
Sketch simulating the purpose:
Code
Two "containers" of points plotted in this way:
plot(1:3, c(3,3,3))
points(1:3, c(2,2,2), col="blue")
Another nice plotting is done by:
#install.packages("hexbin")
library(hexbin)
x <- 1:1000#rnorm(1000)
y <- 1500:501#rnorm(1000)
bin<-hexbin(x, y, xbins=50)
plot(bin, main="Hexagonal Binning")
But I do not know how to use hexbin (I do not understand the functionality). There are needed two colors which I do not know how to generate.
Questions
How to create the 3rd axis with other scaling than the y-axis?
Can I use ´hexbin´ to get the result?
For some reason, using points() does not work, but using plot() does work:
#Set margin on right side to be a bit larger
par(mar = c(5,4.5,4,5))
#Plot first set of data
plot(1:3, rep(3,3), ylim=c(-5,5), xlab="X-Axis", ylab="Y-Axis 1")
#Plot second set of data on different axis.
par(new=T)
plot(1:3, rep(5,3), ylim=c(-10,10), col="blue", xlab="", ylab="", axes=FALSE)
#Add numbers and labels to the second y-axis
mtext("Y-Axis 2",side=4,line=3)
axis(4, ylim=c(-10,10))

Adding arrows to plotrix gap.plot() with y-axis breaks

I'm trying to add confidence intervals to a gap.plot made with the plotrix package when there is a y-axis break. Here is a MWE:
twogrp=c(rnorm(5)+4,rnorm(5)+20,rnorm(5)+5,rnorm(5)+22)
gap.plot(twogrp,gap=c(8,16),
xlab="X values",ylab="Y values",xlim=c(1,30),ylim=c(0,25),
main="Test two gap plot with the lot",xtics=seq(0,25,by=5),
ytics=c(4,6,18,20,22,24),
lty=c(rep(1,10),rep(2,10)),
pch=c(rep(2,10),rep(3,10)),
col=c(rep(2,10),rep(3,10)),
type="b")
arrows(2,18,2,24,length=0.05,angle=90,code=3)
This doesn't add the arrow, presumably because the upper plotting region has been closed.
This works though:
gap.plot(twogrp,gap=c(8,16),
xlab="X values",ylab="Y values",xlim=c(1,30),ylim=c(0,25),
main="Test two gap plot with the lot",xtics=seq(0,25,by=5),
ytics=c(4,6,18,20,22,24),
lty=c(rep(1,10),rep(2,10)),
pch=c(rep(2,10),rep(3,10)),
col=c(rep(2,10),rep(3,10)),
type="b")
arrows(2,4,2,6,length=0.05,angle=90,code=3)
Anybody know of a work around that doesn't involve using another package. I know how to do this in ggplot2, I just prefer to use plotrix.
You need to subtract the gap value from y value when you put arrows in the upper plot area.
arrows(2,18,2,24,length=0.05,angle=90,code=3)
should be as follows.
arrows(2,18 - 8,2,24 - 8,length=0.05,angle=90,code=3)

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="|")

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