Draw box around a grid of plots in R - r

I have arranged eight ggplots underneath each other in two columns with plot_grid. How can I draw a box around the entire grid?

If I understand your question correctly you can use the function panel_border as described in the following link:
https://www.rdocumentation.org/packages/cowplot/versions/1.0.0/topics/panel_border
Which adds an outer border to figures. I could not test that because you didn't added any piece of code that reproduce your result or a similar one.
EDIT:
as Matt said, the function panel_border is per plot, and not the entire plot image as desired. a solution with a dummy example is:
library(ggplot2)
library(cowplot)
p1 <- ggplot(mtcars, aes(disp, mpg)) +
geom_point()
p2 <- ggplot(mtcars, aes(qsec, mpg)) +
geom_point()
plot_grid(p1, p2, labels = c('A', 'B'))+
theme(panel.border = element_rect(colour = "black", fill=NA, size=5))
Is to use theme combined with its attribute panel.border the results is:
As can be seen the border is on the entire plot and not per graph.

Related

Change size (width) of plot in ggplot

I am a newbie attempting to change the width of a ggplot, so that I am can arrange different plots(heatmap and dotplot) in the same figure. However, after hours of trying to reduce the width of the dotplot, I am about to give up.
Code for heatmap (maybe not relevant):
heatmap_GO_NES_1<-ggplot(data=long_frame_GO_NES_1) +
geom_tile(mapping = aes(
x = factor(timepoint,levels = c("6h","12h","24h")),
y =bio_process,fill = NES)) +
ylab(label="Biological process") +
theme(axis.title.x=element_blank()) +
scale_fill_gradient(low="red",high="green")+
facet_grid( group ~. , scales="free",space="free")+
theme(axis.text.x = element_text(angle = 90))+
theme(strip.text.y = element_text(size = 8))
heatmap_GO_NES_1
Code for dotplot:
dot_GO_NES_1<- ggplot(data=long_frame_GO_NES_2)+
geom_count(mapping=aes(x=timepoint, y =bio_process, size=setsize))+
theme(axis.title.x=element_blank(), axis.text.x=element_blank(),
axis.ticks.x=element_blank(),axis.title.y=element_blank(),
axis.text.y=element_blank(),axis.ticks.y=element_blank())
dot_GO_NES_1
Code for figure:
plot_grid(heatmap_GO_NES_1,dot_GO_NES_1)
Obviously, the dotplot is stealing all the figure space, so that my heatmap does not show up in the figure.
TL;DR - you need to use the rel_widths= argument of plot_grid(). Let me illustrate with an example using mtcars:
# Plots to display
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
p2 <- ggplot(mtcars, aes(x='X', y=disp)) + geom_point(aes(size=cyl))
Here are the plots, where you see p2 is like your plot... should not be too wide or it looks ridiculous. This is the default behavior of plot_grid(), which makes both plots the same width/relative size:
plot_grid(p1,p2)
Adjust the relative width of the plots using rel_widths=:
plot_grid(p1,p2, rel_widths=c(1,0.3))

plot(p) and plot(ggplot_gtable(ggplot_build(p))) don't seem to give same output when plot background is transparent

I have the following R ggplot code:
require(ggplot2)
require(ggthemes)
df <- data.frame(x=1:10, y=5*(1:10))
p <- ggplot(df, aes(x,y)) +
geom_point() +
theme_few() +
theme(plot.background = element_rect(fill='transparent', colour=NA), legend.position='top')
pdf('test.pdf', width=5, height=2)
plot(p)
plot(ggplot_gtable(ggplot_build(p)))
But I do get two different figures:
I like the first figure (i.e without the background grid outside panel area). However, I also need to use ggplot_build() for some other processing. Could you please help?
You could copy what ggplot2::print.ggplot does more directly. This seems to work.
pdf('test.pdf', width=5, height=2)
plot(p)
grid::grid.newpage()
grid::grid.draw(ggplot_gtable(ggplot_build(p)))
dev.off()

Conditionally circling around data plots using ggplot2

I have couple of questions regarding plotting using ggplot2.
I have already used below commands to colour data points using R.
library(ggplot2)
df <- read.csv(file="c:\\query2.csv")
ggplot( df,aes( x = Time,y ,y = users,colour = users>40) ) + geom_point()
My question is: how should I draw a continuous line connecting data points and how do I circle around data points for users >40?
To connect the points, use geom_line (if that doesn't give you what you need, please explain what you're trying to accomplish).
I haven't used geom_encircle, but another option is to use a filled marker with the fill deleted to create the circles. Here's an example, using the built-in mtcars data frame for illustration:
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_point(data=mtcars[mtcars$mpg>30,],
pch=21, fill=NA, size=4, colour="red", stroke=1) +
theme_bw()
pch=21 is one of the filled markers (see ?pch for more info on other available point markers). We set fill=NA to remove the fill. stroke sets the thickness of the circle border.
UPDATE: To add a line to this chart, using the example above:
ggplot(mtcars, aes(wt, mpg)) +
geom_line() +
geom_point() +
geom_point(data=mtcars[mtcars$mpg>30,],
pch=21, fill=NA, size=4, colour="red", stroke=1) +
theme_bw()
However, if (as in my original code for this graph) you put the aes statement inside the geom, rather than in the initial call to ggplot, then you need to include an aes statement inside geom_line as well.

How to fill boxes in geom_point legend with color of points, not just increasing their size?

I'm having a similar problem as described in here under "2- After having the two legends...", but instead of increasing the point size (which eventually also enlarges the legend itself), I would like fill each box in the legend with the corresponding color. Like in a bar plot's legend. Data & code examples here.
Looking through several other questions here, the ggplot docu, etc., I tried variations of code-snippets I found, but couldn't figure out a solution. The legend always retained the point symbols.
Therefore: If possible, how to tweak or replace the legend of a point/scatter/bubble plot so that it looks like the legend of a bar plot? Or, more generally, how to replace the legend of a given geom in ggplot2 with that of a different one? Thank you for any hints!
Edit: Example with mtcars data
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(colour = factor(cyl), size = qsec))
p
Adding what I gathered from other SO-answers...
p <- p + guides(colour = guide_legend(override.aes = list(fill = unique(mtcars$cyl))))
p
...keeps the points, instead of expanding the color to fill the legend box, no matter arguments and datasources I try for guides() and list().
On the other hand:
ggplot(mtcars, aes(wt, mpg)) + geom_bar(aes(fill = factor(cyl)), stat="identity")
...draws nicely color-filled boxes to the legend. That's what I'm trying to do for a bubble plot.
You won't be able to get a fill-type legend per se, but you can easily emulate it:
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(colour = factor(cyl), size = qsec)) +
guides(col = guide_legend(override.aes = list(shape = 15, size = 10)))

Legend title position in ggplot2

Anone know how to change the position of the legend title in ggplot?
I have used the following code to move the legend to the bottom and make it horizontal
p <- p + opts(legend.position = 'bottom', legend.direction = 'horizontal')
But now I want the title to be to the left of the legend instead of above. I've looked in the follwing places but cant find it or figure it out:
https://github.com/hadley/ggplot2/wiki/Legend-Attributes
http://had.co.nz/ggplot2/book/toolbox.r
Any assistance would be greatly appreciated
Using the transition guide to version 0.9 as a reference, you might try the following (assuming you want to change the title position for the colour legend):
library(scales)
+ guides(colour = guide_legend(title.position = "left"))
For a continuous scale you'd use guide_colorbar instead of guide_legend.
Just to provide a concrete example to prove I'm not just making this up,
library(ggplot2)
library(scales)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(aes(colour = qsec)) +
guides(colour = guide_legend(title.position = "right"))

Resources