Plot area reduces when x title includes superscript - r

I'm drawing a figure with two plots:
library(ggpubr)
library(ggplot2)
set.seed(123)
df <- data.frame(y= 1:20, x= sample(1:5, 20, replace= T))
A <- ggplot(df, aes(x, y)) + geom_point() + theme_bw() + labs(x= 'A (kg/mm^3)', y= 'B (%)')
B <- ggplot(df, aes(x, y)) + geom_point() + theme_bw() + labs(y= 'A (mm^2)', x= 'B (%)')
ggarrange(A, B)
When I include in one of the plots a superscript (in either the x or the y axis) the area of the plot reduces automatically a bit.
C <- ggplot(df, aes(x, y)) + geom_point() + theme_bw() + labs(x= expression(paste(paste("A (kg/", mm^3), ')')), y= expression('B (%)', c^2))
D <- ggplot(df, aes(x, y)) + geom_point() + theme_bw() + labs(y= expression(paste('A', mm^2)), x= expression('B (%)', c^2))
ggarrange(C, D)
I tried to reduce the area of the plot without a superscript by adding an expression that includes an invisible superscript (c^2) but the undesired area-reduction does not disappear.

For plot composition, package 'patchwork' is the best and easiest to use. It automatically aligns and resizes the plotting areas.
library(patchwork)
C | D

Related

overlaying plots from different dataframes in ggplot without messing with legend

I want to overlay two plots: one is a simple point plot where a variable is used to control the dot size; and another is a simple curve.
Here is a dummy example for the first plot;
library(ggplot2)
x <- seq(from = 1, to = 10, by = 1)
df = data.frame(x=x, y=x^2, v=2*x)
ggplot(df, aes(x, y, size = v)) + geom_point() + theme_classic() + scale_size("blabla")
Now lets overlay a curve to this plot with data from another dataframe:
df2 = data.frame(x=x, y=x^2-x+2)
ggplot(df, aes(x, y, size = v)) + geom_point() + theme_classic() + scale_size("blabla") + geom_line(data=df2, aes(x, y), color = "blue") + scale_color_discrete(name = "other", labels = c("nanana"))
It produces the error:
Error in FUN(X[[i]], ...) : object 'v' not found
The value in v is not used to draw the intended curse, but anyway, I added a dummy v to df2.
df2 = data.frame(x=x, y=x^2-x+2, v=replicate(length(x),0)) # add a dummy v
ggplot(df, aes(x, y, size = v)) + geom_point() + theme_classic() + scale_size("blabla") + geom_line(data=df2, aes(x, y), color = "blue") + scale_color_discrete(name = "other", labels = c("nanana"))
An the result has a messed legend:
What is the right way to achieve the desired plot?
You can put the size aes in the geom_point() call to make it so that you don't need the dummy v in df2.
Not sure exactly what you want regarding the legend. If you replace the above, then the blue portion goes away. If you want to have a legend for the line color, then you have to place color inside the geom_line aes call.
x <- seq(from = 1, to = 10, by = 1)
df = data.frame(x=x, y=x^2, v=2*x)
df2 = data.frame(x=x, y=x^2-x+2)
ggplot(df, aes(x, y)) +
geom_point(aes(size = v)) +
theme_classic() +
scale_size("blabla") +
geom_line(data=df2, aes(x, y, color = "blue")) +
scale_color_manual(values = "blue", labels = "nanana", name = "other")

ggplot2 & gridExtra: Reduce space between plots, but keep ylab of one plot

I would like to plot 3 graphics beside each other via the ggplot2 and gridExtra packages. The graphic on the left side has a ylab, the other 2 graphics do not. All three graphics should have the same size and the space between the graphics should be reduced as much as possible. However, due to the ylab of the graphic on the left side, I am either not able to reduce the space as much as I want; or I am cutting off the ylab.
Consider the following example in R:
library("ggplot2")
library("gridExtra")
# Example data
df <- data.frame(x = 1:10,
y = 1:10)
# Plots
ggp1 <- ggplot(df, aes(x, y)) +
geom_line() + theme_bw() +
ylab("Here is the ylab")
ggp2 <- ggplot(df, aes(x, y)) +
geom_line() + theme_bw() +
ylab("")
ggp3 <- ggplot(df, aes(x, y)) +
geom_line() + theme_bw() +
ylab("")
# Arrange grids
grid.arrange(ggp1, ggp2, ggp3, ncol = 3)
The space between the graphics should be reduced as much as possible.
All graphics should have the same size.
The ylab of the graphic on the left side should be kept.
I was trying to fix the problem with plot.margin, but unfortunately that didn't work.
I would suggest to cbind() the gtables, with the axis removed. null units automatically ensure equal panel widths.
lg <- lapply(list(ggp1,ggp2,ggp3),ggplotGrob)
rm_axis <- function(g){
lay <- g[["layout"]]
cp <- lay[lay$name == "panel",]
g[,-c(1:(cp$l-1))]
}
lg[-1] <- lapply(lg[-1], rm_axis)
grid::grid.draw(do.call(gtable_cbind, lg))
Adding theme (axis.title.y = element_blank()) to ggp2 and ggp3 will reduce the space between them.
library("ggplot2")
library("gridExtra")
# Example data
df <- data.frame(x = 1:10,
y = 1:10)
# Plots
ggp1 <- ggplot(df, aes(x, y)) +
geom_line() + theme_bw() +
ylab("Here is the ylab")
ggp2 <- ggplot(df, aes(x, y)) +
geom_line() + theme_bw() +
ylab("") + theme (axis.title.y = element_blank())
ggp3 <- ggplot(df, aes(x, y)) +
geom_line() + theme_bw() +
ylab("") + theme (axis.title.y = element_blank())
# Arrange grids
grid.arrange(ggp1, ggp2, ggp3, ncol = 3)

plotting: color based on the combination of two column levels

How to plot based on the combination of two column levels(here: treatment, replicate)?
set.seed(0)
x <- rep(1:10, 4)
y <- sample(c(rep(1:10, 2)+rnorm(20)/5, rep(6:15, 2) + rnorm(20)/5))
treatment <- sample(gl(8, 5, 40, labels=letters[1:8]))
replicate <- sample(gl(8, 5, 40))
d <- data.frame(x=x, y=y, treatment=treatment, replicate=replicate)
plots: color based on single column levels
ggplot(d, aes(x=x, y=y, colour=treatment)) + geom_point()
ggplot(d, aes(x=x, y=y, colour=replicate)) + geom_point()
The combination of two column levels would be a-1, a-2, a-3, ... h-6, h-7, h-8.
64 colours will be uninterpretable. How about point labels instead:
ggplot(d, aes(x=x, y=y, colour=treatment)) +
geom_text(aes(label=paste0(treatment, replicate)), size=3, show.legend=FALSE) +
theme_classic()
Or, if you're trying to spot differences in patterns for different treatments, maybe faceting would help:
ggplot(d, aes(x=x, y=y, colour=treatment)) +
geom_text(aes(label=paste0(treatment, replicate)), size=3, show.legend=FALSE) +
facet_wrap(~ treatment, ncol=4) +
scale_x_continuous(expand=c(0,0.7)) +
theme_bw() + theme(panel.grid=element_blank())
But, if you really want a whole bunch of colours...
ggplot(d, aes(x=x, y=y, colour=interaction(treatment,replicate,sep="-",lex.order=TRUE))) +
geom_point() +
labs(colour="Treatment-Replicate") +
theme_classic()
(If you want all potential treatment-replicate combinations to be listed in the legend, regardless of whether they're present in the data, then add + scale_colour_discrete(drop=FALSE) to the plot code.)

In ggplot2, how to add text by group in facet_wrap?

Here is an example:
library(ggplot2)
set.seed(112)
df<-data.frame(g=sample(c("A", "B"), 100, T),
x=rnorm(100),
y=rnorm(100,2,3),
f=sample(c("i","ii"), 100, T))
ggplot(df, aes(x=x,y=y, colour=factor(g)))+
geom_point()+geom_smooth(method="lm", fill="NA")+facet_wrap(~f)
My question is how to add text like the second plot by group into the plot.
You can manually create another data.frame for your text and add the layer on the original plot.
df_text <- data.frame(g=rep(c("A", "B")), x=-2, y=c(9, 8, 9, 8),
f=rep(c("i", "ii"), each=2),
text=c("R=0.2", "R=-0.3", "R=-0.05", "R=0.2"))
ggplot(df, aes(x=x,y=y, colour=factor(g))) +
geom_point() + geom_smooth(method="lm", fill="NA") +
geom_text(data=df_text, aes(x=x, y=y, color=factor(g), label=text),
fontface="bold", hjust=0, size=5, show.legend=FALSE) +
facet_wrap(~f)
Another option is to calculate the correlations on the fly and use the underlying numeric values of the factor variable g to place the text so that the red and blue labels don't overlap. This reduces the amount of code needed and makes label placement a bit easier.
library(dplyr)
ggplot(df, aes(x=x, y=y, colour=g)) +
geom_point() +
geom_smooth(method="lm", fill=NA) + # Guessing you meant fill=NA here
#geom_smooth(method="lm", se=FALSE) # Better way to remove confidence bands
facet_wrap(~f) +
geom_text(data=df %>% group_by(g, f) %>% summarise(corr = cor(x,y)),
aes(label=paste0("R = ", round(corr,2)), y = 10 - as.numeric(g)),
x=-2, hjust=0, fontface="bold", size=5, show.legend=FALSE)

ggplot2: legends for different aesthetics

I first plot histogram for a group of simulated data and fill the bars with one colour. Then I add the line of the density function from which the data was simulated from and make the line with a different colour. Now I want use legends to show one colour (the fill colour of the histogram) is for samples whereas the other (the colour of the line) is for theoretical density. How can I achieve this?
The code is as follows
require(ggplot2)
df <- data.frame(x=rnorm(10^4))
p <- ggplot(df, aes(x=x)) + geom_histogram(aes(y=..density..), fill='steelblue', colour='black', alpha=0.8, width=0.2)
x <- seq(-4, 4, 0.01)
df <- data.frame(x=x, y=dnorm(x))
p <- p + geom_line(data=df, aes(x=x, y=y), colour='red', size=1.5)
p
You can do this by adding a new column to each of your data frames to create fill and colour aesthetics to go into the legend. In each case, there's only one category, but putting them inside the aes() gives you the legends you want:
require(ggplot2)
df <- data.frame(x=rnorm(10^4), fill=rep("Sample", 10^4))
p <- ggplot(df, aes(x=x)) + geom_histogram(aes(y=..density.., fill=fill),
colour='black', alpha=0.8, width=0.2) +
scale_fill_manual(values="steelblue") + labs(fill="")
x <- seq(-4, 4, 0.01)
df <- data.frame(x=x, y=dnorm(x), colour=rep("Theoretical Density",length(x)))
p <- p + geom_line(data=df, aes(x=x, y=y, colour=line), size=1.5) +
scale_colour_manual(values="red") + labs(colour="")
Without changing your data at all, you can specify literal aes() values that you can define later via manual scales.
df <- data.frame(x=rnorm(10^4))
p <- ggplot(df, aes(x=x)) + geom_histogram(aes(y=..density.., fill="samples"),
alpha=0.8, colour="black", width=0.2)
p <- p+scale_fill_manual("",breaks="samples", values="steelblue")
x <- seq(-4, 4, 0.01)
df <- data.frame(x=x, y=dnorm(x))
p <- p + geom_line(data=df, aes(x=x, y=y, colour="theory"), size=1.5)
p <- p+scale_color_manual("",breaks="theory", values="red")

Resources