How can I create a panel layout with grouped plots? - r

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.

Related

Resizing plots in plotly (R)

Sorry if this question has already been answered but I can't find a solution.
I have a data frame which is imported from a .csv file. It has four columns. I want to plot a 3d scatter plot of the first two columns against the last column using Plotly.
The problem is that for some reason, the scatter plot does not automatically resize its axes to fit the plot space. So in this case I end up with a tall thin plot. The weird thing is that if I do an example with some random numbers, I don't get this problem.
Can anyone help me to get my scatter plot to fit to the window?
My data can be found at https://www.dropbox.com/s/ojwrezjker33x2b/Data.csv?dl=0
Basically I want to do:
library(plotly)
X<-read.csv("Data.csv")
plot_ly(x=X[,1],y=X[,2],z=X[,4],type='scatter3d',mode='markers')
And this results in a tall and thin scatter plot (sorry can't post an image cos I am new to the site).
I would like this to be a well-proportioned scatter plot that fits the window.
Thanks for any help.
The default setting for aspectmode is 'auto' and Plotly will try to keep the relative axis lenghts equal.
If you set aspectmode='cube' you should get a plot with identical absolute axes lengths.
plot_ly(x=X[,1],y=X[,2],z=X[,4],type='scatter3d',mode='markers') %>%
layout(scene=list(aspectmode='cube'))

R stack multiple boxplot on top of each other

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.

Plot same plot again (or choose which plot to draw)

I have a complex layout of plots, say for example
layout(matrix(1:2,nrow=1), widths=c(1,7))
I have drawn earlier plots, and am now working on a particular plot (e.g. working on the code on the plot). I redraw the plot often to see impacts of changing e.g. margins.
How can I avoid that each time I redraw the plot it moves on to the next plot in the layout?
e.g. each time I have to start from the beginning, call dev.off, call layout, plot the earlier plots, and then start again with my plot.
Can I just select which plot in the layout I want to plot? Can I move the "counter" that selects which plot is being plotted next?

multiple plots in r, a, b, c, d with lines and curves

I have some pretty nice plots that I want to present in a report, but I would like to have multiple plots with one common legend, title and caption. The problem is the lines in the plots. So far I have made a plot and added lines or curves from one or several binomial models. These curves/lines exist in different data.frames, but they can fit in the same plot because they have the same parameters and lengths.
I have tried to make a plot where I add all the lines at the same time as the plot is being created but then only the lines show up and i got a lot of errors.
So when i create a plot with lines i do like this:
plot(Spruce,Leaves,xlab="Spruce",ylab="Leaves>0")
g=glm(Leaves>0~Spruce,family=binomial,data=1)
g2=glm(Leaves>0~Spruce,family=binomial,data=2)
curve(predict(g,data.frame("Spruce"=x),type="resp"),add=TRUE,col="blue")
curve(predict(g2,data.frame("Spruce"=x),type="resp"),add=TRUE,col="red")
I cannot upload a picture of the plot, but it's nice.
The problem is that I need to present it with several other plots to explain my point and then I would like to do it as one of several a,b,c,d plots.
I have used
par(mfrow=c(2,2))
To have all plots visual at the same time but I would like to name them a,b,c,d and have a common legend, title and mtext.
Any suggestions how to do that?

R: How to overlay pie charts on 'dots' in a scatterplot in R

Using R I would like to replace the points in a 2d scatter plot by a pie chart displaying additional values.
The rational behind is that I have time series data for hundreds of elements (proteins) derived from a biological experiment monitored for 4 conditions. I would like to plot the elements (categorial data) on the y axis and occurrence of a event in time on the x axis. To visualize the relative occurrence between the 4 conditions I would like to visualize this in form of a pie chart or doughnut chart overplayed onto the respective point in the scatter plot.
The overall data density is low so overlapping won't be an issue.
Is this possible in R?
I was thinking of using a manual scale in ggplot2 but could not figure out how to define a pie chart as a scale.
Also of interest would be how to best cluster this data and sort it accordingly.
Yes. pieGlyph() is one ready-to-go function from the Rgraphviz package.
Also, I would check out this Q/A for how to do things like this more generally:
How to fill a single 'pch' point on the plot with two-colours?
Especially check out ?my.symbols from the TeachingDemos package.
Lastly, in regards to ggplot2, you should check out this blog post about possible upcoming features:
http://blog.revolutionanalytics.com/2011/10/ggplot2-for-big-data.html
See also Paul Murrell. Integrating grid graphics output with base graphics output. R News, 3(2):7-12, October 2003. http://www.r-project.org/doc/Rnews/Rnews_2003-2.pdf
The code on pp 10-11 sets up the main plot axes, labels and legend, and then opens a series of smaller windows centered at each individual point on the plot and plots a small graph in each window. I've tried pie charts, mosaics and barplots, but the method is not limited to these types.

Resources