Control size of subplots created with patchwork - code improvement - r

I want to make a multipanel figure containing multiple labeled plots. My plots are produced in ggplot2 & I would like to arrange them with patchwork.
I want to combine two subplots in the first row, followed by two other plots arranged one plot per row:
plot3 + plot4 - both in row #1
plot1 - in row #2
plot2 - in row #3
Here is a dummy example to illustrate the problem:
```{r, fig.width=10, fig.height=13}
library(ggplot2)
library(patchwork)
#Dummy plots
plot1 <- ggplot2::ggplot(data = mpg, aes(x = class, fill=drv)) +
geom_bar(aes(y = ..count..)) + ggplot2::ggtitle("Plot1")
plot2 <- ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, color=class)) +
geom_point()+ ggplot2::ggtitle("Plot2")
plot3 <- ggplot2::ggplot(data = mpg, aes(x = cty)) +
geom_density()+ ggplot2::ggtitle("Plot3")
plot4 <- ggplot2::ggplot(data = mpg, aes(x = cty, y=drv, fill = fl)) +
geom_col()+ ggplot2::ggtitle("Plot4")
# this works, but it is not the desired layout
final <- plot1 + plot2 + {plot3 + plot4 + patchwork::plot_layout(ncol=2)} +
patchwork::plot_layout(ncol=1,heights = unit(c(4, 7, 2),c('cm')))
plot(final)
#this does not work
final2 <- {plot3 + plot4 + patchwork::plot_layout(ncol=2)} + plot1 + plot2 +
patchwork::plot_layout(ncol=1, heights = unit(c(2, 4, 7),c('cm')))
print(final2)
```
This is the output I can produce, but this is not what I want:
And this is the picture I would like to obtain:
Some of my other attempts:
#this does not work either
up_patch <- plot3 + plot4 + patchwork::plot_layout(ncol=2)
final2 <- up_patch + plot1 + plot2 + patchwork::plot_layout(ncol=1, heights = unit(c(2, 4, 7),c('cm')))
print(final2)
#and this as well
up_patch <- plot3 + plot4 + patchwork::plot_layout(ncol=2, heights= unit(2,c('cm')))
bottom_patch <- plot1 + plot2 + patchwork::plot_layout(ncol=1, heights = unit(c(4, 7),c('cm')))
final2 <- up_patch + bottom_patch
print(final2)
# THIS WORKS but needs improvement
final_desired <- (plot3 | plot4) / plot1 + plot2
print(final_desired)
In the last attempt I was able to produce the desired layout, however I would like to be able to control the dimensions of the subplots as in my dummy example in the beginning of this post). It is important for me to adjust the image size to the size of the page.
I would also like to know how to use a namespace qualifier while calling patchwork in the working example, so I would not call a function from another package by an accident.
I followed the instructions from these sources:
https://ggplot2-book.org/arranging-plots.html
https://patchwork.data-imaginist.com/articles/guides/layout.html - (footnote: I do not understand the textual representation of layout)
Combine multiple patchworks

One option would be to use the design argument to specify the layout:
library(ggplot2)
library(patchwork)
design = "
CD
AA
BB
"
plot1 + plot2 + plot3 + plot4 +
plot_layout(
design = design,
heights = c(2, 4, 7))

Related

gganimation: animate bars one by one and retatin previously shown

I wonder how to animate geom_bar from ggplot2 with gganimate in such a way that previously shown bar stay and next come.
library(ggplot2)
library(gganimate)
a <- data.frame(group=c("A","B","C"), values=c(3,2,4), frame=rep('a',3))
b <- data.frame(group=c("A","B","C"), values=c(5,3,7), frame=rep('b',3))
df1 <- rbind(a,b)
Plot1 <-
ggplot(data= df1, aes(x = group, y = values, fill = group)) +
geom_bar(stat='identity') +
theme_bw() +
transition_states(
states = group
, transition_length = 2
, state_length = 1
)
Plot1
The above given code shows bar one by one but fade the previously shown. However, I want to retain already shown bars. Any thoughts.
Check out ?shadow_mark() from the gganimate package. I think this achieves what you are looking to accomplish:
library(ggplot2)
library(gganimate)
a <- data.frame(group=c("A","B","C"), values=c(3,2,4), frame=rep('a',3))
b <- data.frame(group=c("A","B","C"), values=c(5,3,7), frame=rep('b',3))
df1 <- rbind(a,b)
Plot1 <-
ggplot(data= df1, aes(x = group, y = values, fill = group)) +
geom_bar(stat='identity') +
theme_bw() +
transition_states(
states = group
, transition_length = 2
, state_length = 1
) +
shadow_mark() # keeps past data displayed
Plot1

How to add labels to facet_wrap(ed) geom_count plot?

I am creating a facetted plot using facet_wrap. I want text labels to be included inside the bubble. Instead it seems the total is included as label - i.e. all graphs has the same numbers but different bubble size (which is correct).
(Edits)
My code:
Category1 <- c('A','B','C','A','B','C','A','B','C','A','B','C','A','B','C','A','B','C','A','B')
Category2 <- c('W','V','W','V','W','V','W','V','W','V','W','V','W','V','W','V','W','V','W','V')
Class <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
df <- data.frame(Category1, Category2, Class)
g <- ggplot(df, aes(Category1, Category2))
g <- g + facet_wrap(Class ~ ., nrow = 3) + geom_count(col="tomato3", show.legend=F) + scale_size_continuous(range = c(5, 10))
labs(subtitle="Count Plot", y="Category2", x="Category1", title="Cat1 vs Cat2")
g
g2 <- g + geom_text(data=ggplot_build(g)$data[[1]], aes(x, y, label=n), size=2) #+ scale_size(range = c(5, 15))
g2
I expect that the size of the bubble will be indicated by the text inside the bubble. But the actual result is all graphs have the same number. I want the small bubble to have small number proportional to its size.
The problem is that your code using ggplot_build data does not have the same categories as the original. You need to create a count data before hand and use it for plotting.
Create count data
library(tidyverse)
df_count <- df %>%
count(Class, Category1, Category2)
Plot
There are two ways to incorporate this new data.
Method 1
The first example I show is to use both df and df_count. This method will modify your code minimally:
g <- ggplot(df, aes(Category1, Category2))
g <- g + facet_wrap(Class ~ ., nrow = 3) + geom_count(col="tomato3", show.legend=F) +
geom_text(data = df_count, aes(Category1, Category2, label=n), size=2) +
scale_size_continuous(range = c(5, 10)) +
labs(subtitle="Count Plot", y="Category2", x="Category1", title="Cat1 vs Cat2")
g
The line geom_text(data = df_count, aes(Category1, Category2, label=n), size=2) + is added.
Method 2
This method uses only the count data. It uses geom_point() instead of geom_count() and alter the size using the variable n. This method is probably better in terms of code readability.
g_alternative <- ggplot(df_count, aes(Category1, Category2, label = n)) +
facet_wrap(Class ~ ., nrow = 3) +
geom_point(col="tomato3", aes(size = n), show.legend=F) +
geom_text() +
scale_size_continuous(range = c(5, 10)) +
labs(subtitle="Count Plot", y="Category2", x="Category1", title="Cat1 vs Cat2")
g_alternative
The output looks like this:

Modifying Aesthetics - ggplot2

I am trying to learn ggplot2 and have made below plots:
Using this code:
library(ggplot2); library(gridExtra)
gg <- ggplot(mydata,aes(x=Level))
plot1 <- gg + geom_line(aes(y=Experience,colour="xp"),size=1) +
labs(title="xp")
g <- ggplot(mydata,aes(x=Level))
plot2 <- g + geom_line(aes(y=Experience,colour="xp"),size=1) + geom_line(aes(y=Accu,colour="accu"),size=1) +
labs(title="xp vs Accumulated")
grid.arrange(plot1,plot2,ncol=2)
Where mydata is a data frame containing 3 columns (Level, xp and accu) and 30 rows.
What I am wondering is:
How to get the y-axis on the left-hand plot to have the same form as the
right-hand plot.
How to make the color of "xp" the same in both plots
without removing the descriptions of what the lines represent.
How about this (with some random data)?
library(ggplot2)
library(gridExtra)
library(scales)
gg <- ggplot(mydata,aes(x=Level))
plot1 <- gg + geom_line(aes(y=Experience,colour="xp"),size=1) +
labs(title="xp") + scale_y_continuous(labels = comma) +
scale_colour_manual(values = c("red"))
g <- ggplot(mydata,aes(x=Level))
plot2 <- g + geom_line(aes(y=Experience,colour="xp"),size=1) +
geom_line(aes(y=Accu,colour="accu"),size=1) +
labs(title="xp vs Accumulated") + scale_y_continuous(labels = comma) +
scale_colour_manual(values = c("blue", "red"))
grid.arrange(plot1,plot2,ncol=2)

Multiplot using ggplot2

I am using ggplot2 for a multiplot. Althoug after a lot of tweaking, I still face
problems as:
Some free space gets plotted on each side (left/right) of each plot. I have marked this on the right side of each plot.
Plots are not aligned by the left side. This problem is clearly observed in the bottom plot
Y axis label is much far away from the plots. Can I reduce this separation?
Multiplot is:
I used the following R code for the same:
p1 <- ggplot(data = dplots[[1]],aes(timestamp,power/1000))+ geom_line()+
ylab("")+theme(axis.text.x=element_blank(),axis.title.x=element_blank(),axis.ticks.x=element_blank(),plot.margin = unit(c(-0.3,1,-0.3,1), "cm"))+labs(title="room1")
p2 <- ggplot(data = dplots[[2]],aes(timestamp,power/1000))+ geom_line()+
ylab("")+theme(axis.text.x=element_blank(),axis.title.x=element_blank(),axis.ticks.x=element_blank(),plot.margin = unit(c(-0.3,1,-0.3,1), "cm"))+ labs(title="room2")
p3 <- ggplot(data = dplots[[6]],aes(timestamp,power/1000))+ geom_line()+
ylab("")+theme(axis.text.x=element_blank(),axis.title.x=element_blank(),axis.ticks.x=element_blank(),plot.margin = unit(c(-0.3,1,-0.3,1), "cm"))+ labs(title="room3")
p4 <- ggplot(data = dplots[[4]],aes(timestamp,power/1000))+ geom_line()+
ylab("")+theme(axis.text.x=element_blank(),axis.title.x=element_blank(),axis.ticks.x=element_blank(),plot.margin = unit(c(-0.3,1,-0.3,1), "cm"))+ labs(title="room4")
p5 <- ggplot(data = dplots[[5]],aes(timestamp,power/1000))+ geom_line()+
ylab("")+theme(axis.text.x=element_blank(),axis.title.x=element_blank(),axis.ticks.x=element_blank(),plot.margin = unit(c(-0.3,1,-0.3,1), "cm"))+ labs(title="room5")
p6 <- ggplot(data = dplots[[3]],aes(timestamp,power/1000))+ geom_line()+
ylab("")+theme(axis.title.x=element_blank(),axis.ticks.x=element_blank(),plot.margin = unit(c(-0.3,1,-0.3,1), "cm"))+ labs(title="Chiller") +
scale_x_datetime(labels= date_format("%d-%m-%y",tz ="UTC"),breaks = pretty_breaks(8))
grid.arrange(p1,p2,p3,p4,p5,p6,nrow=6,ncol=1,heights=c(0.15,0.15,0.15,0.15,0.15,0.15),left="Power (KW)")
The dataset (dplots) is stored at the link.
Probably the easiest solution is to combine the dataframes in the list in one dataset. With rbindlist from the data.table package you can also include id's for each dataframe:
library(data.table)
# bind the dataframes together into one datatable (which is an enhanced dataframe)
DT <- rbindlist(dplots, idcol = "id")
# give names to the id's
DT$id <- factor(DT$id, labels = c("room 1","room 2","room 3", "room 4","room 5","Chiller"))
library(ggplot2)
ggplot(DT, aes(x = timestamp, y = power)) +
geom_line() +
scale_x_datetime(expand = c(0,0)) +
facet_grid(id ~ ., scales="free_y") +
theme_bw()
this results in the following plot:
With your existing code, use cowplot package:
library(cowplot)
plot_grid(p1,p2,p3,p4,p5,p6,ncol=1,align = "v")

How to add different lines for facets

I have data where I look at the difference in growth between a monoculture and a mixed culture for two different species. Additionally, I made a graph to make my data clear.
I want a barplot with error bars, the whole dataset is of course bigger, but for this graph this is the data.frame with the means for the barplot.
plant species means
Mixed culture Elytrigia 0.886625
Monoculture Elytrigia 1.022667
Monoculture Festuca 0.314375
Mixed culture Festuca 0.078125
With this data I made a graph in ggplot2, where plant is on the x-axis and means on the y-axis, and I used a facet to divide the species.
This is my code:
limits <- aes(ymax = meansS$means + eS$se, ymin=meansS$means - eS$se)
dodge <- position_dodge(width=0.9)
myplot <- ggplot(data=meansS, aes(x=plant, y=means, fill=plant)) + facet_grid(. ~ species)
myplot <- myplot + geom_bar(position=dodge) + geom_errorbar(limits, position=dodge, width=0.25)
myplot <- myplot + scale_fill_manual(values=c("#6495ED","#FF7F50"))
myplot <- myplot + labs(x = "Plant treatment", y = "Shoot biomass (gr)")
myplot <- myplot + opts(title="Plant competition")
myplot <- myplot + opts(legend.position = "none")
myplot <- myplot + opts(panel.grid.minor=theme_blank(), panel.grid.major=theme_blank())
So far it is fine. However, I want to add two different horizontal lines in the two facets. For that, I used this code:
hline.data <- data.frame(z = c(0.511,0.157), species = c("Elytrigia","Festuca"))
myplot <- myplot + geom_hline(aes(yintercept = z), hline.data)
However if I do that, I get a plot were there are two extra facets, where the two horizontal lines are plotted. Instead, I want the horizontal lines to be plotted in the facets with the bars, not to make two new facets. Anyone a idea how to solve this.
I think it makes it clearer if I put the graph I create now:
Make sure that the variable species is identical in both datasets. If it a factor in one on them, then it must be a factor in the other too
library(ggplot2)
dummy1 <- expand.grid(X = factor(c("A", "B")), Y = rnorm(10))
dummy1$D <- rnorm(nrow(dummy1))
dummy2 <- data.frame(X = c("A", "B"), Z = c(1, 0))
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) +
geom_hline(data = dummy2, aes(yintercept = Z))
dummy2$X <- factor(dummy2$X)
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) +
geom_hline(data = dummy2, aes(yintercept = Z))

Resources