Good evening everybody,
I'm getting about a problem with a multiple boxplot in R.
I would like to remove the space between the numbers on the y-axes
and the axes itself. I don't want to move the label, with mtext(),
and it's not a problem with the margins par(mar()).
Thank you very much here there is an example: I would get the numbers (-1,1) closer to the axes.
Related
I am creating two plots in R, arranged with one over another. The issue is that the plot is very small within the boundary box, and I don't know how to "zoom in" essentially. Changing the margins only affects the size of the boundary box, not the little plot on the inside. Any help would be appreciated as I'm very new to this!
par(mfrow=c(2,1), mar=c(1,1,1,1))
plot(G_nwr,axes=FALSE,main="Non-weighted")
plot(boundary,add=TRUE)
points(G$utm_x,G$utm_y,pch=16,cex=0.2)
plot(roads_clip,add=TRUE,col="red",lwd=0.5)
plot(buildings_clip,add=TRUE,col="gray80")
plot(creeks_clip,add=TRUE,col="lightskyblue",lwd=2)
plot(railways_clip,add=TRUE,col="purple1",lwd=2)
plot(water_clip,add=TRUE,col="steelblue1")
legend("topright",inset=0.05, cex=0.8,
c("Canadian Goose","Road","Building","Creek","Railway","Water Body"),pch=c(16,NA,15,NA,15,NA),lty=c(NA,1,NA,1,NA,1),col=c("black","red","gray80","lightskyblue","purple1","steelblue1"))
I am trying to stack plots with common x- and y-axes in ggplot. What I want to do is have only the bottom plot show the x-axis labels and titles. But I've never been able to figure out how to do this cleanly in ggplot2 without having the bottom plot be squished by carrying the virtue of the x-axis labels/title. There must be an easy way to do this- everyone wants to stack graphs, right?!
I'm currently trying with ggarrange. Example code below. Note that the bottom plot gets compressed vertically because it has the tick and axis labels. I could just have the top two have white font labels/title, but then there is an unseemly amount of margin space between the three if you use that hack.
I'm definitely open to packages other than gpubr, but I am hoping for something not too elaborate that I can use in subsequent situations, as I'm sure I'll encounter this again...
Help, please!! -Ryan
#
require(ggplot2); require(ggpubr)
X=data.frame(seq(as.Date("2001-01-01"),as.Date("2001-12-31"),by='days')); colnames(X)='date'
X$Y1=sample(80:100,size=nrow(X),replace=T)
X$Y2=sample(100:120,size=nrow(X),replace=T)
X$Y3=sample(50:70,size=nrow(X),replace=T)
plot.Y1= ggplot(X, aes(x=date,y=Y1))+
geom_line()+lims(y=c(50,150))+
theme(axis.title.x = element_blank(),axis.text.x=element_blank())
plot.Y2= ggplot(X, aes(x=date,y=Y2))+
geom_line()+lims(y=c(50,150))+
theme(axis.title.x = element_blank(),axis.text.x=element_blank())
plot.Y3= ggplot(X, aes(x=date,y=Y3))+
geom_line()+lims(y=c(50,150))
x11(10,8)
ggarrange(plot.Y1,plot.Y2,plot.Y3,nrow=3,ncol=1)
Bottom plot is squished!
try this,
egg::ggarrange(plot.Y1,plot.Y2,plot.Y3,ncol=1)
I am trying to make some boxplots. Here is a sample data
set.seed(1)
a<-rnorm(100)
a1<-rnorm(100);a2<-rnorm(100);a3<-rnorm(100);a4<-rnorm(100)
b1<-rnorm(100);b2<-rnorm(100);b3<-rnorm(100);b4<-rnorm(100)
c1<-rnorm(100);c2<-rnorm(100);c3<-rnorm(100);c4<-rnorm(100)
d1<-rnorm(100);d2<-rnorm(100);d3<-rnorm(100);d4<-rnorm(100)
e1<-rnorm(100);e2<-rnorm(100);e3<-rnorm(100);e4<-rnorm(100)
f1<-rnorm(100);f2<-rnorm(100);f3<-rnorm(100);f4<-rnorm(100)
dat<-data.frame(a,a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4,e1,e2,e3,e4,f1,f2,f3,f4)
par(mfrow=c(4,1))
boxplot(dat$a,dat$a1,dat$b1,dat$c1,dat$d1,dat$e1,dat$f1)
boxplot(dat$a,dat$a2,dat$b2,dat$c2,dat$d2,dat$e2,dat$f2)
boxplot(dat$a,dat$a3,dat$b3,dat$c3,dat$d3,dat$e3,dat$f3)
boxplot(dat$a,dat$a4,dat$b4,dat$c4,dat$d4,dat$e4,dat$f4)
And this is the resultant plot
As you can see, the four boxplots lie on top of each other. Is there any way I can combine these plots on top of each other so that there is no spaces between them as well as make the size of boxplot small (i.e. the boxes inside the plots)
I thought doing a par(mfrow=c(4,1)) should do the trick but it is leaving a lot of spaces between the plots. Ideally, I would want a single x-axis and single y-axis (further split into four axis to show the values of each of the plots)
Thanks
You can use par(mar=c(0,0,0,0)) to get rid of the entire figure margin. Adjusting the four mar values will change the margins (see ?par).
As for changing the size of the boxplots, you can adjust the boxwex argument in the boxplot function (see ?boxplot). Here is code that changes both mar and boxwex.
par(mfrow=c(4,1), mar=c(2,3,0,1))
boxplot(dat$a,dat$a1,dat$b1,dat$c1,dat$d1,dat$e1,dat$f1, boxwex=0.25)
boxplot(dat$a,dat$a2,dat$b2,dat$c2,dat$d2,dat$e2,dat$f2, boxwex=0.5)
boxplot(dat$a,dat$a3,dat$b3,dat$c3,dat$d3,dat$e3,dat$f3, boxwex=0.75)
boxplot(dat$a,dat$a4,dat$b4,dat$c4,dat$d4,dat$e4,dat$f4, boxwex=1,
names=1:7)
You can set the first element of mar to 0 if you want to completely get rid of the space between the plots, but that doesn't seem like it would look particularly nice, and that makes it trickier to get the x-axis in the bottom figure without changing its size relative to the first three plots.
Another alternative you could try is to put all the boxplots into one plot, but have side-by-side boxplots for each category (1-7). You can use the at argument in the boxplot function to specify the position of each boxplot along the x-axis.
I've produced a pareto.chart using the QCC package in R. In the default plot, the Y axis is scaled too large & for that reason the bars are too small. Most of the plot is wasted to empty white space. I assume this is a result of the long tail (right-skew) in the data ?
How is it possible to re-scale the Y-Axis so that the bars of the chart will be taller and more prominent (and differences between the bars more visible) ?
This is my first question and I can't post images yet. Please follow the link to an illustration of the problem:
https://www.dropbox.com/s/t8bwhmoxmwl1aic/pareto-axis.png
Thanks!
Keith
If you type help(pareto.chart) you will see there is an option ylim to specify the limits if the y-axis. If you provide some sample data I could try it out.
Otherwise, try including in your function call
pareto.chart(..., ylim=c(0,10000))
and see if that rescales your y-axis.
I'm working on visualizing a matrix in R (almost exactly like http://reference.wolfram.com/mathematica/ref/MatrixPlot.html), and I've been using
image(<matrix>,axes=FALSE)
to draw the picture. However, I noticed that with the y-axis turned off, the plot isn't centered--the space is still there for the axis ticks + label. I can finagle some centering with
par(oma=c(0,0,0,2.5))
but this seems inefficient and error-prone (if my matrix were to change dimensions/become non-square). Is there a better way to force the graphic to center?
Reference image http://img694.imageshack.us/img694/9891/metropolis.png
The right hand margin is significantly smaller than the left.
Does
par(mar=c(5,2,4,2))+0.1
help?