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.
Related
I'm using r-markdown to make one pagers. I have all my plots set in place but they do not line up nicely. The plots area is lined up but where the bars start is different for each plot. How can i set it so that it's always lined up? I want to line up the green lines.The start of the plots (maroon line) is always in line.
NB! The two plots are made separately from one another as they use different data.
In the below script, outliers on the boxplot are shown as individual scatter points. Instead, I would like the creation of the boxplot to include these and to not treat these points as outliers. Consequently, the box would be extended to include them.
ggplot(imp,aes(Group,LWG,fill=Group))+geom_boxplot()
As per the below picture, the bottom of the left boxplot would extend downwards further.
That would be inappropriate to extend the boxplot. The main thing about them is to show the quantiles, therefor an extension would make the boxplot statically wrong in its interpretation.
But you can remove the outliers with:
geom_boxplot(outlier.shape = NA)
I was wondering if you could help me with something.
I'm currently writing some papers with regression analyses (and scatter plots with fitted lines). I'd like to merge all my plots into a single panel as shown.
I've already stacked some plots using grid.arrange from gridExtra (which go in box A), but I have no clue how to:
Create boxes A, B, C, (...), each with its own letter on the top-left corner. Each box contains stacked plots (2 or 3 rows depending on the # of models, with all plots for a single model on the same row) linear regression plots.
Place color labels on top of the boxes (e.g Social Cognition and Moral Judgement)
Place vertical text (which would serve as the Y label) such as Risk Perception and Impact Estimation
Thank you very much
This is the referenced layout
You can plot multiple charts within the same chart area. You can do this with the standard graphics functions by setting the mfcol parameter for a device. For example, to plot six figures within the plot area in three rows of two columns, you would set mfcol as follows:
par(mfcol=c(3, 2))
Each time a new figure is plotted, it will be plotted in a different row or column within the device, starting with the top-left corner.
So you can use plot or ggplot or another plotting package and the plot will be added to the layer.
I have a bar graph which looks like the following:
Problem: If I facet it by the same variable as the color, the x-axis has space for plotting all the bars even though I don't need them.
My Solution: I used multiplot function from the Rmisc to separately make bar graphs for each partner but then a lot of individual customization is needed to make the graphs go cohesively together.
Question: Is there another way that I can use to get closer to plot 2 without the extra spaces for the variables that don't apply.
I'm using ggplot to plot.
use:
+ facet_wrap(~variable_to_facet_by,
scales = 'free')
as part of your ggplot code and that should get you what you want.
I just came into a problem while making several maps in R, the problem I came to is that I want to plot several maps and some geom_points in those maps, each map will have some points with different values and so the legend with the scales (size and color) will change between maps. All I want is to have exactly the same legend, representing the same values (for both color and size). I've tried with breaks etc but my data is continuous, so I didn't find any way to fix it.
EDIT:Simple example
Will try to explain with simple example by myself. Imagine I have these two arrays to be plotted into different coordinates for 2 different days:
c<-(1,2,3,2,1)
c<-(1,9,2,1,2)
What I want is to set the legend of the plot to be always representing the range 1-9 as values of the geom_points, no matter the specific values of the given day, in a way that no matter the values, the legend will be always the same and if I try to set some slides, the scale will not change
Any ideas?