Exact Positioning of multiple plots in ggplot2 with grid.arrange - r

I'm trying to create a multiple plot with the same x-axis but different y-axes, because I have values for two groups with different ranges. As I want to control the values of the axes (respectively the y-axes shall reach from 2.000.000 to 4.000.000 and from 250.000 to 500.000), I don't get along with facet_grid with scales = "free".
So what I've tried is to create two plots (named "plots.treat" and "plot.control") and combine them with grid.arrange and arrangeGrob. My problem is, that I don't know how to control the exact position of the two plots, so that both y-axes are positioned on one vertical line. So in the example below the second plot's y-axis needs to be positioned a bit more to the right.
Here is the code:
# Load Packages
library(ggplot2)
library(grid)
library(gridExtra)
# Create Data
data.treat <- data.frame(seq(2005.5, 2015.5, 1), rep("SIFI", 11),
c(2230773, 2287162, 2326435, 2553602, 2829325, 3372657, 3512437,
3533884, 3519026, 3566553, 3527153))
colnames(data.treat) <- c("Jahr", "treatment",
"Aggregierte Depositen (in Tausend US$)")
data.control <- data.frame(seq(2005.5, 2015.5, 1), rep("Nicht-SIFI", 11),
c(324582, 345245, 364592, 360006, 363677, 384674, 369007,
343893, 333370, 318409, 313853))
colnames(data.control) <- c("Jahr", "treatment",
"Aggregierte Depositen (in Tausend US$)")
# Create Plot for data.treat
plot.treat <- ggplot() +
geom_line(data = data.treat,
aes(x = `Jahr`,
y = `Aggregierte Depositen (in Tausend US$)`),
size = 1,
linetype = "dashed") +
geom_point(data = data.treat,
aes(x = `Jahr`,
y = `Aggregierte Depositen (in Tausend US$)`),
fill = "white",
size = 2,
shape = 24) +
scale_x_continuous(breaks = seq(2005, 2015.5, 1),
minor_breaks = seq(2005, 2015.5, 0.5),
limits = c(2005, 2015.8),
expand = c(0.01, 0.01)) +
scale_y_continuous(breaks = seq(2000000, 4000000, 500000),
minor_breaks = seq(2000000, 4000000, 250000),
labels = c("2.000.000", "2.500.000", "3.000.000",
"3.500.000", "4.000.000"),
limits = c(2000000, 4000000),
expand = c(0, 0.01)) +
theme(text = element_text(family = "Times"),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.line.x = element_line(color="black", size = 0.6),
axis.line.y = element_line(color="black", size = 0.6),
legend.position = "none") +
geom_segment(aes(x = c(2008.7068),
y = c(2000000),
xend = c(2008.7068),
yend = c(3750000)),
linetype = "dotted") +
annotate(geom = "text", x = 2008.7068, y = 3875000, label = "Lehman\nBrothers + TARP",
colour = "black", size = 3, family = "Times") +
geom_segment(aes(x = c(2010.5507),
y = c(2000000),
xend = c(2010.5507),
yend = c(3750000)),
linetype = "dotted") +
annotate(geom = "text", x = 2010.5507, y = 3875000, label = "Dodd-Frank-\nAct",
colour = "black", size = 3, family = "Times") +
geom_rect(aes(xmin = 2007.6027, xmax = 2009.5, ymin = -Inf, ymax = Inf),
fill="dark grey", alpha = 0.2)
# Create Plot for data.control
plot.control <- ggplot() +
geom_line(data = data.control,
aes(x = `Jahr`,
y = `Aggregierte Depositen (in Tausend US$)`),
size = 1,
linetype = "solid") +
geom_point(data = data.control,
aes(x = `Jahr`,
y = `Aggregierte Depositen (in Tausend US$)`),
fill = "white",
size = 2,
shape = 21) +
scale_x_continuous(breaks = seq(2005, 2015.5, 1), # x-Achse
minor_breaks = seq(2005, 2015.5, 0.5),
limits = c(2005, 2015.8),
expand = c(0.01, 0.01)) +
scale_y_continuous(breaks = seq(250000, 500000, 50000),
minor_breaks = seq(250000, 500000, 25000),
labels = c("250.000", "300.000", "350.000", "400.000",
"450.000", "500.000"),
limits = c(250000, 500000),
expand = c(0, 0.01)) +
theme(text = element_text(family = "Times"),
axis.title.x = element_blank(), # Achse
axis.title.y = element_blank(), # Achse
axis.line.x = element_line(color="black", size = 0.6),
axis.line.y = element_line(color="black", size = 0.6),
legend.position = "none") +
geom_segment(aes(x = c(2008.7068),
y = c(250000),
xend = c(2008.7068),
yend = c(468750)),
linetype = "dotted") +
annotate(geom = "text", x = 2008.7068, y = 484375, label = "Lehman\nBrothers + TARP",
colour = "black", size = 3, family = "Times") +
geom_segment(aes(x = c(2010.5507),
y = c(250000),
xend = c(2010.5507),
yend = c(468750)),
linetype = "dotted") +
annotate(geom = "text", x = 2010.5507, y = 484375, label = "Dodd-Frank-\nAct",
colour = "black", size = 3, family = "Times") +
geom_rect(aes(xmin = 2007.6027, xmax = 2009.5, ymin = -Inf, ymax = Inf),
fill="dark grey", alpha = 0.2)
# Combine both Plots with grid.arrange
grid.arrange(arrangeGrob(plot.treat, plot.control,
ncol = 1,
left = textGrob("Aggregierte Depositen (in Tausend US$)",
rot = 90,
vjust = 1,
gp = gpar(fontfamily = "Times",
size = 12,
colout = "black",
fontface = "bold")),
bottom = textGrob("Jahr",
vjust = 0.1,
hjust = 0.2,
gp = gpar(fontfamily = "Times",
size = 12,
colout = "black",
fontface = "bold"))))

Do:
install.packages("cowplot")
but do not library(cowplot) as it'll mess up your theme work.
Then, do:
grid.arrange(
arrangeGrob(cowplot::plot_grid(plot.treat, plot.control, align = "v", ncol=1),
ncol = 1,
left = textGrob("Aggregierte Depositen (in Tausend US$)",
rot = 90,
vjust = 1,
gp = gpar(fontfamily = "Times",
size = 12,
colout = "black",
fontface = "bold")),
bottom = textGrob("Jahr",
vjust = 0.1,
hjust = 0.2,
gp = gpar(fontfamily = "Times",
size = 12,
colout = "black",
fontface = "bold"))))

Related

Add id labels to points above the limit line in ggplot

I have a dataframe like this
id <- c(5738180,51845,167774,517814,1344920)
amount <- c(3.76765976,0.85195407,1.96821355,0.01464609,0.57378284)
outlier <- c("TRUE","FALSE","FALSE","FALSE","FALSE")
df.sample <- data.frame(id,amount,outlier)
I am trying to plot the points and add an id label to any point that is above the limit. In this case (id=5738180)
I am plotting like this
library(tidyverse)
library(ggplot2)
library(ggrepel) # help avoid overlapping text labels
library(gridExtra) # adds custom table inside ggplot
library(scales) # breaks and labels for axes and legends
library(ggthemes) # Adding desired ggplot themes
df.sample %>%
ggplot(aes(x = as.numeric(row.names(df.sample)),
y = amount, label = as.character(id))) +
geom_point(alpha = 0.6, position = position_jitter(w = 0.05, h = 0.0),
aes(colour = (amount < 3)), size = 2) +
geom_hline(aes(yintercept = 3, linetype = "Limit"),
color = 'black', size = 1) +
geom_text(aes(y = 3,x = amount[4],
label = paste("Limit = ", round(3, 3)),
hjust = 0, vjust = 1.5)) +
geom_text_repel(data = subset(df.sample, outlier == 'TRUE'),
nudge_y = 0.75,
size = 4,
box.padding = 1.5,
point.padding = 0.5,
force = 100,
segment.size = 0.2,
segment.color = "grey50",
direction = "x") +
geom_label_repel(data = subset(df.sample, outlier == 'TRUE'),
nudge_y = 0.75,
size = 4,
box.padding = 0.5,
point.padding = 0.5,
force = 100,
segment.size = 0.2,
segment.color = "grey50",
direction = "x")
labs(title = "Outlier Detection",
y = "amount",
x = "") +
theme_few() +
theme(legend.position = "none",
axis.text = element_text(size = 10, face = "bold"),
axis.title = element_text(size = 10, face = "bold"),
plot.title = element_text(colour = "blue", hjust = 0.5,
size = 15, face = "bold"),
strip.text = element_text(size = 10, face = "bold")) +
scale_colour_manual(values = c("TRUE" = "green","FALSE" = "red"))
I am running into an error "Error: Aesthetics must be either length 1 or the same as the data (1): x"
Can someone point me in the right direction?
The issue is with geom_text_repel() and geom_label_repel(). You subset the data, which now only includes 1 row, but the aes() are inheriting from the original data which have 5 rows, hence the error. To fix this, subset the data outside of the ggplot() call, and change the aesthetics for it. You are also missing a + after geom_label_repel() and the result below modifies the nudge_y to nudge_x and removes the geom_text_repel().
outliers <- subset(df.sample, outlier == TRUE)
ggplot(data = df.sample,
aes(x = as.numeric(row.names(df.sample)),
y = amount,
label = as.character(id))) +
geom_point(alpha = 0.6,
position = position_jitter(w = 0.05, h = 0.0),
aes(colour = (amount < 3)),
size = 2) +
geom_hline(aes(yintercept = 3,
linetype = "Limit"),
color = 'black',
size = 1) +
geom_text(aes(y = 3,x = amount[4],
label = paste("Limit = ",
round(3, 3)),
hjust = 0,
vjust = 1.5)) +
geom_label_repel(data = outliers,
aes(x = as.numeric(rownames(outliers)),
y = amount,
label = amount),
nudge_x = 0.75,
size = 4,
box.padding = 0.5,
point.padding = 0.5,
force = 100,
segment.size = 0.2,
segment.color = "grey50",
direction = "x",
inherit.aes = F) +
labs(title = "Outlier Detection",
y = "amount",
x = "") +
theme_few() +
theme(legend.position = "none",
axis.text = element_text(size = 10, face = "bold"),
axis.title = element_text(size = 10, face = "bold"),
plot.title = element_text(colour = "blue", hjust = 0.5,
size = 15, face = "bold"),
strip.text = element_text(size = 10, face = "bold")) +
scale_colour_manual(values = c("TRUE" = "green","FALSE" = "red"))

Changing the shape of one point or few points in a scatter plot in R

I have a set of points in a scatter plot as below. I want to change the shape of one point or few points. I searched for this but could not find a way to do it.
I want to achieve like this
And like this
Code:
df <- data.frame(x = c(1,2,2,3,3.5,4,4.5,5,5.5,6,1.5,2,2,2,2,1.5,2.5,3,3,3,3,5.5,5,6,5.5,7)
,y = c(2,1,2,2,2,2,2,2,1.5,2,2.5,3,3.5,4,4.5,3.5,3.5,2,3,3.5,4,2.5,3,3,4,3.5))
library(ggplot2)
library(extrafont)
# helper dataframe for axis
df_arrow <- data.frame(x = c(0, 0),
y = c(0, 0),
xend = c(0, 8),
yend = c(8, 0))
ggplot(df,aes(x, y)) +
geom_point(colour = "blue", size = 5, shape = 3)+
scale_x_continuous(breaks = 1:7, expand = expansion(add = c(0, 1)))+
scale_y_continuous(breaks = 1:7, expand = expansion(add = c(0, 1)))+
coord_fixed(xlim = c(0, 7), ylim = c(0, 7), clip = "off")+
geom_segment(data = df_arrow, aes(x = x, xend = xend, y = y, yend = yend), size = 0.75, colour = "black",
arrow = arrow(angle = 20, length = unit(3, "mm"), ends = "last", type = "closed"), linejoin = "mitre") +
annotate("text", x = c(7.8, 0.3), y = c(0.3, 7.8), label = c("italic(x)", "italic(y)"), parse = TRUE, size = 6, family = "Times New Roman")+
labs(x = NULL,
y = NULL)+
theme_bw()+
theme(panel.grid.major = element_line(colour = "gray80"),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.ticks.length = unit(1, "mm"),
text = element_text(size = 18, family = "Times New Roman"))
How is this?
Create a new column using dplyr::mutate, which is conditional upon the x-coordinates (for example, but it could be anything). Then, use this column within aes to control the shape size.
Also, you can use scale_shape_manual and scale_colour_manual to manually control the shape and colours. It's not clear to me what shape you want but you would just need to change the arguments in scale_shape_manual.
EDIT:
Since you specifically need a different symbol, then you need to use geom_text instead.
df %>%
dplyr::mutate(z = ifelse(x >= 5, "-", "+")) %>%
ggplot(aes(x, y)) +
geom_text(size = 12, aes(colour=z, label=z)) +
scale_x_continuous(breaks = 1:7, expand = expansion(add = c(0, 1)))+
scale_y_continuous(breaks = 1:7, expand = expansion(add = c(0, 1)))+
coord_fixed(xlim = c(0, 7), ylim = c(0, 7), clip = "off")+
geom_segment(data = df_arrow, aes(x = x, xend = xend, y = y, yend = yend), size = 0.75, colour = "black",
arrow = arrow(angle = 20, length = unit(3, "mm"), ends = "last", type = "closed"), linejoin = "mitre") +
annotate("text", x = c(7.8, 0.3), y = c(0.3, 7.8), label = c("italic(x)", "italic(y)"), parse = TRUE, size = 6, family = "Times New Roman")+
labs(x = NULL,
y = NULL)+
theme_bw()+
theme(panel.grid.major = element_line(colour = "gray80"),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.ticks.length = unit(1, "mm"),
text = element_text(size = 18, family = "Times New Roman")) +
scale_shape_manual(values=c(8, 9)) +
scale_colour_manual(values = c('red', 'blue'))

annotate font is different from the other texts in the figure with ggplot2

I am trying to add some annotations to the graph that I create with ggplot2 2. The font size and type seem to be different for the annotations compared to everything else. I have a font size of 20 for the labels and the legend, but even with a size of 12 and specifying the font type (family) as Arial, the annotations look very different than the rest of the graph.
fontsize <- 20 / .pt
plot <- ggplot(material) + theme_void() + scale_color_hue() +
aes(x = strain, y = stress, colour = type, group = type) +
geom_xspline(size = 1, spline_shape = 0.5) + labs(x = "Strain (in/in)",
y = "Stress (ksi)", color = element_blank()) + theme(legend.position = "bottom",
legend.box.background = element_rect(colour = "black", size = 0.5),
axis.title.y = element_text(size=20, colour="black", angle = 90, vjust = -22, hjust = 0.55),
axis.title.x = element_text(size=20, colour="black", vjust = 7, hjust = 0.6),
axis.text.y = element_blank(), axis.text.x = element_blank(),
legend.text=element_text(size=20), legend.box.margin = margin(10,10,10,10),
legend.spacing.x = unit(0, 'cm')) +
scale_y_continuous(trans = "reverse") + scale_x_continuous(trans = "reverse") +
geom_vline(aes(xintercept = 0), size = 0.3) + geom_hline(aes(yintercept = 0), size = 0.3) +
geom_segment(aes(x = 0, y = 0, xend = -0.0009226, yend = 0), size = 1) +
geom_segment(aes(x = -0.0009226, y = 0, xend = -0.0011707, yend = -0.94), size = 1) +
geom_segment(aes(x = 0, y = -4.958, xend = -0.00333, yend = -4.958),
size = 0.6, linetype = "dotted", colour = "black") +
geom_segment(aes(x = -0.00333, y = 0, xend = -0.00333, yend = -4.958),
size = 0.6, linetype = "dotted", colour = "black") +
geom_segment(aes(x = -0.0024, y = 0, xend = -0.0024, yend = -4.958),
size = 0.6, linetype = "dotted", colour = "black") +
geom_segment(aes(x = -3.75E-03, y = 0, xend = -3.75E-03, yend = -4.6011),
size = 0.6, linetype = "dotted", colour = "black") +
geom_segment(aes(x = -0.0046736, y = 0, xend = -0.0046736, yend = -4.6011),
size = 0.6, linetype = "dotted", colour = "black") +
annotate("text", x = 0, y= -0.94, label = "f'", size = fontsize, family = "sans")
The resulted graph is
How can I send all the text in the figure to the same font and size?
EDIT
The issue is that the font size in the theme is usually specified in pt (points) while when specifying size inside of the geoms or annotate it is measured in mm. This means that the size of you axis.title is 20pt, while the size of your annotation is 12mm or approx. 34pt. Therefore, if you want the same font sizes you have to do a conversion for which ggplot2 offers a helper constant .pt.
If you want the annotation to have the same size as e.g. the axis.title you have to set the fontsize in annotate equal to 20 / .pt.
library(ggplot2)
family <- "sans"
fontsize <- 20 / .pt
ggplot(mtcars, aes(hp, mpg)) + theme_void() +
geom_point(aes(color = factor(cyl))) +
annotate("text", x = 0, y= -0.94, label = "f'", size = fontsize, family = "sans") +
geom_text(data = data.frame(hp = 100, mpg = -.94), aes(label = "f'"), size = fontsize) +
theme(legend.position = "bottom",
legend.box.background = element_rect(colour = "black", size = 0.5),
axis.title.y = element_text(size=20, colour="black", angle = 90, vjust = -22, hjust = 0.55),
axis.title.x = element_text(size=20, colour="black", vjust = 7, hjust = 0.6),
axis.text.y = element_blank(), axis.text.x = element_blank(),
legend.text=element_text(size=20), legend.box.margin = margin(10,10,10,10),
legend.spacing.x = unit(0, 'cm'))

ggsave() not working when label of annotate() layer has subscripts

I am trying to add labels to my plot that contain subscripts. The code runs fine in R studio, but when exporting the figure using ggsave() I get the following error:
Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
Metric information not available for this family/device
Here is my data:
dput(LLTemp_wide)
structure(list(Temp = c(-15, -20, -22.5, -25, -30), Alive = c(5L,
4L, 2L, 1L, 0L), Dead = c(0L, 2L, 4L, 5L, 5L), Prop_Survival = c(1,
0.6666, 0.3333, 0.1666, 0)), class = "data.frame", row.names = c(NA,
-5L))
The packages in using:
library(plyr)
library(MASS)
library(ggPredict)
library(dplyr)
library(ggplot2)
And my code:
# theme adjustments
Alex_Theme = theme_bw() +
theme(plot.title = element_text(hjust = 0.5, face='plain', size = 12)) +
theme(plot.title = element_text(vjust=-1.8)) +
theme(plot.subtitle=element_text(size=10, hjust=0.5, face="italic", color="black")) +
theme(legend.position="none") +
theme(panel.border = element_rect(fill=NA, colour = "black", size=0.5)) +
theme(axis.text = element_text(face = "plain", size = 10)) +
theme(axis.title.x = element_text(margin = margin(t = 8, r = 20, b = 0, l = 0))) +
theme(axis.title.y = element_text(margin = margin(t = 8, r = 6, b = 0, l = 0))) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(axis.title = element_text(face="plain", size = 10))
ggplot(LLTemp_wide, aes(x = Temp, y = Prop_Survival)) +
Alex_Theme +
#ggtitle("UIC: Lower lethal temperature") +
#theme(plot.title = element_text(size = 10, vjust = 0.5)) +
xlab("Temperature (°C)") +
ylab("Survival Probibility") +
geom_segment(aes(x = -21.4917, y = -Inf, xend = -21.4917, yend = 0.5), linetype = 2, color = 'grey50') + # LT50 vertical line
geom_segment(aes(x = -Inf, y = 0.5, xend = -21.4917, yend = 0.5), linetype = 2, color = 'grey50') + # LT50 horizontal line
geom_segment(aes(x = -25.35566, y = -Inf, xend = -25.35566, yend = 0.1), linetype = 2, color = 'grey50') + # LT10 vertical line
geom_segment(aes(x = -Inf, y = 0.1, xend = -25.35566, yend = 0.1), linetype = 2, color = 'grey50') + # LT10 horizontal line
geom_segment(aes(x = -17.62774, y = -Inf, xend = -17.62774, yend = 0.9), linetype = 2, color = 'grey50') + # LT90 vertical line
geom_segment(aes(x = -Inf, y = 0.9, xend = -17.62774, yend = 0.9), linetype = 2, color = 'grey50') + # LT90 horizontal line
stat_smooth(method = "glm",
method.args = list(family = quasibinomial(link = 'logit')),
se = FALSE,
colour = "red") +
geom_point() +
annotate(geom = "text", x = -29, y = 0.55, size = 3, parse = TRUE, label = as.character(expression(paste(LT[50], "= -21.5 °C")))) +
annotate(geom = "text", x = -33, y = 0.15, size = 3, parse = TRUE, label = as.character(expression(paste(LT[90], "= -25.4 °C")))) +
annotate(geom = "text", x = -25, y = 0.95, size = 3, parse = TRUE, label = as.character(expression(paste(LT[10], "= -17.6 °C")))
)
## Save Plot
ggsave("ALB_Lethal_Temp.png", width = 3.5, height = 4, type = "cairo-png")
Here is a screenshot of the plot in the console (seems to be working fine...):
And what I end up with after ggsave() export:
If I remove the expression from the label and just print text, the ggsave() export works fine. Is this a ggsave issue or some issue with my code?

Adding labels to this ggridge plot

I can't see, to figure out how to add labels to this plot:
ggplot(input_cleaned, aes(x =DAYS_TO_FA, y = fct_rev(DATE_TEXT), group = fct_rev(DATE_TEXT))) +
geom_density_ridges2(stat="binline", bins = 75, scale = 0.95, draw_baseline = FALSE) +
labs(title = 'Monthly Plots of Time to First Nose Pickin', y='Month Tracked', x = 'Days to First Pickin Action') +
theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) +
scale_x_continuous(breaks=seq(0,130,5)) +
geom_segment(aes(x=50, xend = 50, y=1,yend=5),
linetype = "dashed", size = 1.5,
color = "black") +
geom_segment(aes(x=75, xend = 75, y=5,yend=30),
linetype = "dashed", size = 1.5,
color = "black")
which produces:
I have tried this:
ggplot(input_cleaned, aes(x =DAYS_TO_FA, y = fct_rev(DATE_TEXT), group = fct_rev(DATE_TEXT))) +
geom_density_ridges2(stat="binline", bins = 75, scale = 0.95, draw_baseline = FALSE) +
geom_text(stat = "bin",
aes(y = fct_rev(input_cleaned$DATE_TEXT) + 0.95*(..count../max(..count..)), label = ifelse(..count..>0, ..count.., "")),
vjust = 1.4, size = 3, color = "white", binwidth = 1) +
labs(title = 'Monthly Plots of Time to First Nose Pickin', y='Month Tracked', x = 'Days to First Pickin Action') +
theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) +
scale_x_continuous(breaks=seq(0,130,5)) +
geom_segment(aes(x=50, xend = 50, y=1,yend=5),
linetype = "dashed", size = 1.5,
color = "black") +
geom_segment(aes(x=75, xend = 75, y=5,yend=30),
linetype = "dashed", size = 1.5,
color = "black")
based on the example found here:
Visualization of Groups of Poisson random samples using ggridges
but I can't get it to work. Nothing changes.
I know it may not be a good idea for this graph, but I am interested in seeing how it looks and more or less learning how to apply it.

Resources