How to manually edit a grid.arrange, ggplot_gtable and facet? - r

I'm ploting a Hydrograph but I additionally use facet_grid in R because I have objects with common features.
But when I use facet_grid the plot gets distorted, as shown in the figure below. How can I randerize this?
Note that it is not aligned properly, the scale of the y axis is scrambled, etc.
Among the adjustments I tried, I realized that it is possible to greatly improve this plot. I've created an image based on the above plot, some other attempts on how I'm trying and making some adjustments to paint to demonstrate what I'm trying to do.
Here's my code:
library(ggplot2)
library(grid)
library(gridExtra)
g1 <- ggplot(data_cet,
aes(x = Periodo,
y = Ind_plu)) +
geom_bar(stat = 'identity',
fill = "blue",
position = position_dodge()) +
ylab("Precip.") +
scale_y_reverse(labels = scales::comma) +
theme_bw() +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())
g2 <- ggplot(data_cet,
aes(x = Periodo,
y = Nivel,
colour = Bomba)) +
geom_line(aes(group = 1)) +
scale_color_manual(values = c("#0B775E", "#35274A", "#F2300F")) +
labs(colour = "Status CMB") +
facet_grid(data_cet$arranjo + data_cet$Bacia ~.) +
scale_x_date(breaks = datebreaks_m,
labels = date_format("%b/%y")) +
xlab('Período') + ylab('% Nível') +
theme_bw() +
theme(axis.text.x = element_text(face = "plain",
color = "black",
angle = 90),
axis.text.y = element_text(face = "plain",
color = "black"),
legend.title = element_blank(),
strip.background = element_blank(),
legend.position = "bottom")
g1 <- ggplot_gtable(ggplot_build(g1))
g2 <- ggplot_gtable(ggplot_build(g2))
maxWidth = unit.pmax(g1$widths[2:3], g2$widths[2:3])
g1$widths[2:3] <- maxWidth
g2$widths[2:3] <- maxWidth
plot_hyd <- grid.arrange(g1, g2, ncol = 1, heights = c(1, 3))
ggsave(file = "plot_hyd4.pdf", plot_hyd)
My dataset is too large, my apologize for not showing the dataset and dput().

You could add a widths = c(0.9, 1) to grid.arrange (fiddle with the first number some) to get your graphs to line up along the right side.
Otherwise, ggsave your file to a larger pdf. Your element_text objects, such as the legend, are absolute sizes, so if you scale up the pdf dimensions your graphs will look larger by comparison.
The exact values of widths and ggsave(width, height) are going to depend on you data, and unfortunately will take some trial and error. If you're using something like RStudio, I suggest fiddling with the grid.arrange call and finding the widths argument you like before calling ggsave. When you are ready to experiment with different ggsave width and height arguments, run it at a lower dpi the first few times so it processes more quickly.
Note that since you haven't included your data, I haven't tried to recreate this problem - this is just how I've solved this kind of issue in the past. If these suggestions don't work for you, let me know and I can use some built-in datasets to find another solution

Following the logic of the #Pintintended tip for the code. I adopted the layout_matrix argument.
>
plot_hyd <- grid.arrange(g1, g2,
layout_matrix = rbind(c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,NA),
c(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2),
c(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2),
c(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2),
c(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2),
c(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2),
c(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)))
#ggsave(file="plot_hyd4.jpeg",plot_hyd,width=13,height=16,dpi=200)

Related

adding theme_survminer removes grid lines from ggsurvplot object [duplicate]

Say I have the following data:
require(ggplot2)
set.seed(123)
data <- data.frame(x = sample(1:20, 100, replace = TRUE))
I want to create a dot plot of data$x, so this is what I do:
ggplot(data, aes(x)) +
geom_dotplot(binwidth = 1) +
scale_x_continuous(breaks = seq(1, 20, 1)) +
scale_y_continuous(breaks = NULL)
Which gives me this:
I would like to get rid of those vertical grid lines, so I add theme(line = element_blank()) to my ggplot statement. The problem is that command also eliminates the tick marks, which I would like to keep. How can I hide the grid lines whilst keeping their respective ticks?
I would also like to know how I can change the grid lines so they'll be drawn every 1:20, not at every 0.5 mark.
I've looked for those answers in ?title() and ?geom_dotplot, tried a couple of things, to no avail. Appreciate the help!
Use panel.grid
theme(panel.grid = element_blank())
If you want the grid lines to be drawn every 1:20, not at every 0.5 mark.
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_line(colour = "white",size=0.75))
you might find answers here
opts has been changed to theme. but the thing about grid.major.x, grid.minor.y should set on the track
To get rid of the major grid line, for example
ggplot(data, aes(x)) +
geom_dotplot(binwidth = 1) +
scale_x_continuous(breaks = seq(1, 20, 1)) +
scale_y_continuous(breaks = NULL) + theme(panel.grid.major = element_blank())

ggplot how to control fonts on boxplot with stat

How do I control the font-family and size for text elements added to my boxplot:
Following the approach in this question, I have implemented the following code to show the number of observations:
library("ggplot2")
v_min <- -1
v_max <- 3.5
increm <- 0.5
y_limits <- c(v_min, v_max)
increms <- seq(v_min, v_max, increm)
counts <- function(x){
# you can experiment with 'adjust' and 'max-value' to find the perfect position
adjust <- 0.95
return(c(y = adjust * v_max, label = length(x)))
}
ggplot(d1, aes(x = ONRC_Hierarchy, y=lwpMeanRut_inc)) +
geom_boxplot(outlier.alpha = 0.2, outlier.size = 0.5) +
geom_hline(aes(yintercept=0), color="blue", linetype="dotted", size=1)+
stat_summary(fun.data = counts, geom = "text") +
scale_y_continuous(limits = y_limits, breaks = increms) +
xlab("") +
ylab("Rut Increment (mm/year)\n") +
theme_minimal() +
theme(
text = element_text(size = 10, family = "mono"),
axis.text.x=element_text(angle = -35, hjust = 0),
panel.grid.major.y = element_line(color = "lightgray",
size = 0.15,linetype = 2),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_blank())
This solution works, as shown in the plot below, except that the font is different from the other graph elements. As you can see, I have tried to control this with the theme() statement, but it does not seem to work. Note, I deliberately used a small mono font to show the difference in the number of observations labels and the other graph elements.
Geom/stat fonts are set in the geom/stat layer, not in theme(). In this case, you can add family = "mono" as an argument to your stat_summary().
The size of fonts in geom_text/geom_label etc. is not on the same scale as element_text theme options, which are in points. You can read more about that here and here.

Stacking multiple figures together in ggplot

I am attempting to make publication ready figures where the bottom axis (with tick marks) of one figure is cleanly combined with the top axis of the figure below it. Here is an example of what it might look like, although this one doesn't have tick marks on each panel:
Here is my attempt to do so, by simply using grid.arrange:
#Libraries:
library(ggplot2)
library(dplyr)
library(gridExtra)
#Filter to create two separate data sets:
dna1 <- DNase %>% filter(Run == 1)
dna2 <- DNase %>% filter(Run == 2)
#Figure 1:
dna1_plot <- ggplot(dna1, aes(x = conc, y = density)) + geom_point() + theme_classic() +
theme(axis.title.x = element_blank())
#Figure 2:
dna2_plot <- ggplot(dna2, aes(x = conc, y = density)) + geom_point() + theme_classic()
#Using grid.arrange to combine:
dna <- grid.arrange(dna1_plot, dna2_plot, nrow = 2)
And an attempt with some adjustments to the plot margins, although this didn't seem to work:
dna1_plot_round2 <- ggplot(dna1, aes(x = conc, y = density)) + geom_point() + theme_classic() +
theme(axis.title.x = element_blank(),
plot.margin = (0,0,0,0), "cm")
dna2_plot_round2 <- ggplot(dna2, aes(x = conc, y = density)) + geom_point() + theme_classic() +
theme(plot.margin = unit(c(-0.5,-1,0,0), "cm"))
dna_round2 <- grid.arrange(dna1_plot_round2, dna2_plot_round2, nrow = 2)
Does anyone know the best way to stack figures like this in ggplot? Is there a better way than using grid.arrange? If possible it would be great to see how to do it with/without tick marks on each x axis as well.
Thank you!
You don't need any non-native ggplot stuff. Keep your data in one data frame and use facet_grid.
dna <- DNase %>% filter(Run %in% 1:2)
ggplot(dna, aes(x = conc, y = density)) +
geom_point() +
theme_bw() +
facet_grid(rows = vars(Run)) +
theme(panel.spacing = unit(0, "mm"))
The R package deeptime has a function called ggarrange2 that can achieve this. Instead of just pasting the plots together like grid.arrange (and ggarrange), it lines up all of the axes and axis labels from all of the plots.
# remove bottom axis elements, reduce bottom margin, add panel border
dna1_plot_round2 <- ggplot(dna1, aes(x = conc, y = density)) + geom_point() + theme_classic() +
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.title.x = element_blank(),
plot.margin = margin(0,0,-.05,0, "cm"), panel.border = element_rect(fill = NA))
# reduce top margin (split the difference so the plots are the same height), add panel border
dna2_plot_round2 <- ggplot(dna2, aes(x = conc, y = density)) + geom_point() + theme_classic() +
theme(plot.margin = margin(-.05,0,0,0, "cm"), panel.border = element_rect(fill = NA))
dna_round2 <- ggarrange2(dna1_plot_round2, dna2_plot_round2, nrow = 2)
You might also try the fairly recent patchwork package, although I don't have much experience with it.
Note that while Gregor's answer may be fine for this specific example, this answer might be more appropriate for other folks that come across this question (and see the example at the top of the question).
For your purposes, I believe Gregor Thomas' answer is best. But if you are in a situation where facets aren't the best option for combining two plots, the newish package {{patchwork}} handles this more elegantly than any alternatives I've seen.
Patchwork also provides lots of options for adding annotations surrounding the combined plot. The readME and vignettes will get you started.
library(patchwork)
(dna1_plot / dna2_plot) +
plot_annotation(title = "Main title for combined plots")
Edit to better address #Cameron's question.
According to the package creator, {{patchwork}} does not add any space between the plots. The white space in the example above is due to the margins around each individual ggplot. These margins can be adjusted using the plot.margin argument in theme(), which takes a numeric vector of the top, right, bottom, and left margins.
In the example below, I set the bottom margin of dna1_plot to 0 and strip out all the bottom x-axis ticks and text. I also set the top margin of dna2_plot to 0. Doing this nearly makes the y-axis lines touch in the two plots.
dna1_plot <- ggplot(dna1, aes(x = conc, y = density)) + geom_point() + theme_classic() +
theme(axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
plot.margin = unit(c(1,1,0,1), "mm"))
#Figure 2:
dna2_plot <- ggplot(dna2, aes(x = conc, y = density)) + geom_point() + theme_classic() +
theme(plot.margin = unit(c(0,1,1,1), "mm"))
(dna1_plot / dna2_plot)

Arrange multiple ggplots, but avoid squishing due to axis label

I've been using cowplot::plot_grid to arrange multiple ggplot figures, which has been working great in general. However, I've now run into a problem where I want to include the x axis labels on just the bottom plot, but it ends up squishing all the plots (with align = "vh") or the lowest plot (with align = "v") in the column.
Is there any way to perhaps extract the x-axis label grob, and add that as a another plot in plot_grid (perhaps similar to https://github.com/wilkelab/cowplot/blob/master/vignettes/shared_legends.Rmd for legends)? Or another approach?
Thanks for any thoughts!
library(ggplot2)
library(cowplot)
a = data.frame(a = rnorm(100), b = rep(c("laskjdflsakjd", "cknmlokfjoweijojf"), 50))
b = data.frame(a = rnorm(100), b = rep(c("laskjdflsakjd", "cknmlokfjoweijojf"), 50))
c = data.frame(a = rnorm(100), b = rep(c("laskjdflsakjd", "cknmlokfjoweijojf"), 50))
aplot = ggplot(a, aes(x=b, y=a)) + geom_bar(stat = "identity")
bplot = ggplot(b, aes(x=b, y=a)) + geom_bar(stat = "identity")
cplot = ggplot(c, aes(x=b, y=a)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle=90, vjust = 0.5))
plot_grid(aplot + theme(axis.text.x = element_blank(), axis.title.x = element_blank()),
bplot + theme(axis.text.x = element_blank(), axis.title.x = element_blank()),
cplot, ncol = 1, align = "vh")
You can play with the relative heights directly, though it may take a fair bit of tinkering (and will change anytime your plot size changes). In addition, for a single column, you should just be able to use align = "v" -- the "h" option is trying to line up the bottoms of the plots as well (hence why it is re-adding the space for the axis labels).
This worked reasonably well for me:
plot_grid(aplot + theme(axis.text.x = element_blank(), axis.title.x = element_blank())
, bplot + theme(axis.text.x = element_blank(), axis.title.x = element_blank())
, cplot
, ncol = 1
, align = "v"
, rel_heights = c(0.5,0.5,1)
)
In addition, here is the obligatory suggestion to consider facet_wrap though your actual use case presumably does not allow it.
Finally, I have in the past tried to extract the axes to do what you suggested: it did not go well. I had immense problems getting them to line up correctly and eventually abandoned the attempt to return to setting rel_heights
For completeness, here is an approach using facet_wrap (using bind_rows from dplyr):
df <-
bind_rows(a = a, b = b, c = c
, .id = "df")
ggplot(df, aes(x=b, y=a)) +
geom_bar(stat = "identity") +
facet_wrap(~df, ncol = 1) +
theme(axis.text.x = element_text(angle=90, vjust = 0.5))

Change grid line behavior in ggplot2

Say I have the following data:
require(ggplot2)
set.seed(123)
data <- data.frame(x = sample(1:20, 100, replace = TRUE))
I want to create a dot plot of data$x, so this is what I do:
ggplot(data, aes(x)) +
geom_dotplot(binwidth = 1) +
scale_x_continuous(breaks = seq(1, 20, 1)) +
scale_y_continuous(breaks = NULL)
Which gives me this:
I would like to get rid of those vertical grid lines, so I add theme(line = element_blank()) to my ggplot statement. The problem is that command also eliminates the tick marks, which I would like to keep. How can I hide the grid lines whilst keeping their respective ticks?
I would also like to know how I can change the grid lines so they'll be drawn every 1:20, not at every 0.5 mark.
I've looked for those answers in ?title() and ?geom_dotplot, tried a couple of things, to no avail. Appreciate the help!
Use panel.grid
theme(panel.grid = element_blank())
If you want the grid lines to be drawn every 1:20, not at every 0.5 mark.
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_line(colour = "white",size=0.75))
you might find answers here
opts has been changed to theme. but the thing about grid.major.x, grid.minor.y should set on the track
To get rid of the major grid line, for example
ggplot(data, aes(x)) +
geom_dotplot(binwidth = 1) +
scale_x_continuous(breaks = seq(1, 20, 1)) +
scale_y_continuous(breaks = NULL) + theme(panel.grid.major = element_blank())

Resources