Make R Plot Axes the Same for Multi-Plot Figure - r

I have 3 multi-plot figures I'm making for a manuscript and R is shrinking the first plot in the 2 figures with 3 paneled plots. They have different axes and scales so I couldn't use a facet function, but ggarrange() does what I need. Is there a way to make the size of the actual graphs identical?enter image description here
Plot 1 axes smaller
Here's the script generating them:
library(ggplot2)
library(ggpubr)
library(ggpmisc)
library(ggrepel)
library(tidyverse)
summary_bp <- read.table("1500bp_amas_summary.txt", header=TRUE)
alignment_length <- summary_bp$Alignment_length
no_inf_sites <- summary_bp$Parsimony_informative_sites
Length_Site_Reg <- ggplot(summary_bp, aes(x=alignment_length, y=no_inf_sites)) +
geom_point(shape = 1, size = 1) +
geom_smooth(method = lm, size = 0.7) +
stat_regline_equation(label.x = 3000, label.y = 3500, geom = "text", aes(label= ..rr.label..), size = 2) +
stat_cor(method = "pearson", label.x = 3000, label.y = 3100,aes(label = ..p.label..), size = 2) +
coord_cartesian(xlim = c(1500, 15000), ylim = c(0,4000)) +
scale_x_continuous(breaks = seq(1500, 15000, by=1500)) +
scale_y_continuous(breaks = seq(0, 4000, by=500)) +
labs( x = "Alignment Lengths", y = "Number of Parsimony \n Informative Sites") +
theme(aspect.ratio = 1, axis.text.x = element_text(angle=45, hjust = 1),
axis.line = element_line(color = "black"), panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(size = 7),
axis.title.x = element_text(size = 9, face = "bold"), axis.title.y = element_text(size = 8, face = "bold"))
Length_Site_Reg
#Fig. 3b) GC hist
summary_all <- read.table("summary_trim.txt", header=TRUE, sep = ",")
GC_hist <- ggplot(data = summary_all, aes(x=GC_content)) +
geom_histogram(binwidth = 0.025, fill = "white", color = "black") +
xlim(0.3,0.725) +
coord_cartesian( ylim = c(0,1050)) +
labs(x = "GC Content", y = "Number of Orthologs") +
theme(aspect.ratio = 1, panel.background = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), legend.position = c(0.75, 0.65),
axis.line = element_line(color = "black"), axis.text = element_text(size = 8),
axis.title = element_text(size = 9, face = "bold"), legend.key.size = unit(0.4, 'cm'),
legend.key.height = unit(0.4, 'cm'),
legend.key.width = unit(0.4, 'cm'),
legend.title = element_text(size=6),
legend.text = element_text(size=6)) +
geom_vline(aes(xintercept=mean(GC_content), color="mean"),
linetype="solid", size=0.5, show.legend = FALSE)
GC_hist
# Fig. 3c) K2P Divergence Hist
#trimmed_k2 <- file.choose()
trimmed_k2_data <- read.table("trimmed_alignment_k2.csv", header=TRUE, sep = ",")
trimmed_k2_data
trimmed_k2_data_hist <- ggplot(data = trimmed_k2_data, aes(x=distance)) +
geom_histogram(binwidth = 0.025, fill = "white", color = "black") +
coord_cartesian( ylim = c(0,1200) ) + scale_y_continuous(breaks = seq(0,1200,200)) +
labs(x = "K2P Distance", y = "Number of Orthologs", size= 3) +
theme(aspect.ratio = 1, panel.background = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(color = "black"),
axis.text = element_text(size = 8),
axis.title = element_text(size = 9, face = "bold")) +
geom_vline(aes(xintercept=mean(distance)), linetype="solid", colour="red", size=0.5, show.legend = FALSE)
trimmed_k2_data_hist
Fig3_plots <-ggarrange(Length_Site_Reg, GC_hist, trimmed_k2_data_hist, ncol = 3, widths = 1, heights = 1)
Fig3_plots

Related

How to make a dual y-axis line plot with a polynomial function in ggplot2?

I need help with the modification of a plot with y-axis. In Excel I don't have trouble, but the hard part is in R. Besides, I uploaded an example of the final plot in excel.
It is not relevant the data you used.
I will appreciate your help.
Required double y-axis with polynomial function plot:
#Make lm model
Model<- lm(V~ T,data=data1)
summary(Model)
anova(Model)
HSD.test(Model, 'T', group = T, MSerror, alpha = 0.05, console = T)
#Make eq
eq <- function(data1){
m<- lm(V~T);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(unname(coef(m)[1]), digits = 2),
b = format(unname(coef(m)[2]), digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
}
#Make plot
ggplot(data, aes(x=Time, y=mean))+
geom_line()+
geom_smooth(method=loess)+
stat_regline_equation(label.y = 4.0e-06, aes(label=..eq.label..))+
stat_regline_equation(label.y = 3.8e-06, aes(label=..rr.label..))+
geom_pointrange(aes(ymin=mean-se, ymax=mean+se))+
theme(
line = element_line(colour = "black", size = 1, linetype = 1, lineend = "butt"),
rect = element_rect(fill = "white", colour = "black", size = 1, linetype = 1),
aspect.ratio = 1,
plot.background = element_rect(fill = "white"),
plot.margin = margin(1, 1, 1, 1, "cm"),
axis.text = element_text(size = rel(2.5), colour = "#000000", margin = 1),
strip.text = element_text(size = rel(0.8)),
axis.line = element_blank(),
axis.text.x = element_text(vjust = 0.2, angle=0),
axis.text.y = element_text(hjust = 1),
axis.ticks = element_line(colour = "#000000", size = 1.2),
axis.title.x = element_text(size = 30, vjust=0.5),
axis.title.y = element_text(size = 30, angle = 90),
axis.ticks.length = unit(0.1, "cm"),
legend.background = element_rect(colour = NA),
legend.margin = unit(0.15, "cm"),
legend.key = element_rect(fill = "grey95", colour = "white"),
legend.key.size = unit(1.2, "lines"),
legend.key.height = NULL,
legend.key.width = NULL,
legend.text = element_text(size = rel(2.0)),
legend.text.align = NULL,
legend.title = element_text(size = rel(2.0), face = "bold", hjust = 0),
legend.title.align = NULL,
legend.position = "right",
legend.direction = NULL,
legend.justification = "right",
legend.box = NULL,
panel.background = element_rect(fill = "#ffffff", colour = "#000000",
size = 2, linetype = "solid"),
panel.border = element_rect(colour = "black", fill=NA, size=2),
)+
ylab(expression(Volume~(m^3))) + xlab(expression(Time~(min)))

How to add basemaps to spatraster plot with ggplot 2?

I want to add this basemap using to raster using ggplot2 and terra packages and I am stuck.
Can I have some help figuring this out?
library(basemaps)
library(terra)
Library(rasterVis)
set_defaults(Yiel_total , map_service = "esri" , map_type = "world_hillshade")
x <- basemap_magic(Yiel_total , map_service = "esri" , map_type = "world_hillshade")
#Yiel_total was used for the extent
SpatRaster Data for Yiel_total
The ggplot code:
Yield <-rast("Yield_total")
Yield_Tot_6 <- gplot(Yield ) + geom_tile(aes(fill = value)) + ggtitle("")+
theme(panel.spacing.x=unit(0,"lines"), axis.title.x = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(),
axis.title.y = element_blank(),plot.title = element_text(hjust = 0.5, size = 18, face = "bold"),strip.background = element_blank(),
strip.text = element_text(size = 14, color = "black", face = "bold"),legend.text = element_text(size = 14, face = "bold"),
legend.title = element_text(hjust = 0.1, size = 16, face = "bold"), panel.background = element_blank()) +
guides(fill = guide_colourbar(title = "Relative yield",title.hjust = 0.5, barheight = 15, barwidth = 2))+
#basemap_magic(Yield_0.5_6, map_service = "esri" , map_type = "world_hillshade")+
facet_wrap(~ variable, ncol = 3 ) +
scale_fill_gradient(limits = c(0.01,0.88),low = 'red', high = 'dark green', na.value = "grey93", breaks = c(0.02,0.20, 0.40, 0.60,0.75, 0.87))
Yield_Tot_6
You can use tidyterra to work easily with ggplot2 and SpatRasters. I modified the call to basemap_raster and converted it also to SpatRaster:
library(basemaps)
library(terra)
Yiel_total <- rast("Yiel_total.tif")
set_defaults(Yiel_total, map_service = "esri", map_type = "world_hillshade")
x <- basemap_raster(Yiel_total, map_service = "esri", map_type = "world_hillshade")
x_terr <- rast(x)
library(ggplot2)
library(tidyterra)
ggplot() +
geom_spatraster_rgb(data = x_terr) +
geom_spatraster(data = Yiel_total) +
# Faceting with tidyterra
facet_wrap(~lyr, ncol = 3) +
scale_fill_gradient(
limits = c(0.01, 0.88), low = "red", high = "dark green",
na.value = NA, breaks = c(0.02, 0.20, 0.40, 0.60, 0.75, 0.87)
) +
ggtitle("") +
theme(
panel.spacing.x = unit(0, "lines"), axis.title.x = element_blank(),
axis.text = element_blank(), axis.ticks = element_blank(),
axis.title.y = element_blank(), plot.title = element_text(
hjust = 0.5,
size = 18, face = "bold"
), strip.background = element_blank(),
strip.text = element_text(size = 14, color = "black", face = "bold"),
legend.text = element_text(size = 14, face = "bold"),
legend.title = element_text(hjust = 0.1, size = 16, face = "bold"),
panel.background = element_blank()
) +
guides(fill = guide_colourbar(
title = "Relative yield", title.hjust = 0.5,
barheight = 15, barwidth = 2
))

geom_point not showing up on ggplot with geom_sf of rivers

I am trying to build a map that contains a few points overlayed onto rivers. I am using code adapted from https://milospopovic.net/map-rivers-with-sf-and-ggplot2-in-r/, but I downloaded the North America map and substituted
st_polygon(list(cbind(
c(-84.9, -84.9, -84.0, -84.0, -84.9),
c(34.4, 35.05, 35.05, 34.4, 34.4)
))),
crs = crsLONGLAT)
to create my bounding box.
test.point <- data.frame("x"= -84.73405, "y"= 35.00988)
p <-
ggplot()+
geom_point(data= test.point, aes(x=x, y=y), color= "black", fill= "black") +
geom_sf(data=na_riv, aes(color=factor(ORD_FLOW), size=width)) +
coord_sf(crs = 4087,
xlim = c(bbox["xmin"], bbox["xmax"]),
ylim = c(bbox["ymin"], bbox["ymax"])) +
labs(x= "", subtitle="",
title="Rivers of the Southeast",
caption="©2022\nSource: ©World Wildlife Fund, Inc. (2006-2013)\n HydroSHEDS database http://www.hydrosheds.org") +
scale_color_manual(
name = "",
values = c('#081f6b', '#08306b', '#08519c', '#2171b5', '#4292c6', '#6baed6', '#9ecae1', '#c6dbef', '#deebf7')) +
scale_size(range=c(0, .3)) +
scale_alpha_manual(values=c("2"= 1, "3" = 1, "4" = 1, "5" = .7, "6" = .6, "7" = .4, "8" = .3, "9" = .2, "10" = .1)) +
theme_bw() +
theme(text = element_text(family = "georg"),
panel.background = element_blank(),
legend.background = element_blank(),
legend.position = "none",
panel.border = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
plot.title = element_text(size=40, color="#2171b5", hjust=0.5, vjust=0),
plot.subtitle = element_text(size=14, color="#ac63a0", hjust=0.5, vjust=0),
plot.caption = element_text(size=10, color="grey60", hjust=0.5, vjust=10),
legend.text = element_text(size=9, color="grey20"),
legend.title = element_text(size=10, color="grey20"),
strip.text = element_text(size=12),
plot.margin = unit(c(t=1, r=0, b=-1, l=-1),"lines"))
p
The above code yields a pretty map of all of the rivers but the point doesn't show up. :(
Here is one way to get around this. Basically you can convert the points you want to highlight a a simple feature geometry:
test.point <- data.frame(x = c(-84.73405, -84.6), y = c(35.00988, 34.6))
new_test.point <- st_sfc(st_multipoint(as.matrix(test.point)), crs = crsLONGLAT)
Then, I just filtered the data to have only the rivers present in the area of interested:
nariv = nariv %>% mutate(inter = as.integer(st_intersects(geometry, bbox)))
nariv_filt = nariv %>% filter(!is.na(inter))
And create the plot:
ggplot() +
geom_sf(data = nariv_filt, aes(color = factor(ORD_FLOW), size = width, alpha = width)) +
geom_sf(data = new_test.point, color="red", size=5)+
coord_sf(crs = prj, xlim = c(bb["xmin"], bb["xmax"]),ylim = c(bb["ymin"], bb["ymax"])) +
geom_point(data = test.point, aes(x=x, y=y), color="red", size=5)+
labs(y = "", subtitle = "", x = "", title = "Rivers of North and Central America") +
scale_color_manual(name = "",
values = c(
"#08306b", "#1c4680", "#305d94", "#4574a7",
"#5d8cb9", "#77a4cb", "#deebf7", "#deebf7", "#deebf7"
), drop = F) +
scale_alpha(range = c(0.3, 0.8)) +
scale_size(range = c(0, 3)) +
theme_minimal() +
theme(
text = element_text(family = "Montserrat"),
panel.background = element_blank(),
legend.background = element_blank(),
legend.position = "none",
panel.border = element_blank(),
#panel.grid.minor = element_blank(),
#panel.grid.major = element_blank(),
#plot.title = element_text(size = 100, color = "#2171b5", hjust = 0.5, vjust = -22),
#plot.subtitle = element_text(size = 14, color = "#ac63a0", hjust = 0.5, vjust = 0),
#plot.caption = element_text(size = 40, color = "grey60", hjust = 0.5, vjust = 120),
#axis.title.x = element_text(size = 10, color = "grey20", hjust = 0.5, vjust = -6),
legend.text = element_text(size = 9, color = "grey20"),
legend.title = element_text(size = 10, color = "grey20"),
strip.text = element_text(size = 12),
#plot.margin = unit(c(t = -2.5, r = -10, b = -10, l = -10), "lines"),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
#axis.text.x = element_blank(),
#axis.text.y = element_blank()
)

Ajust variable order and color in forest plot (ggplot2)

I have adapted the solution to align forest plot and a table from this post:
how to align table with forest plot (ggplot2)
Here is my code:
library(dplyr, warn = FALSE)
library(ggplot2)
library(patchwork)
tester <- data.frame(
treatmentgroup = c("Education Continuous", "0", "1-4",
"5-8", ">8"),
or = c(0.914, 0.961, 0.709, 0.523, 0.457),
low_ci = c(0.894, 0.793, 0.577, 0.389, 0.339),
up_ci = c(0.935, 1.166, 0.871, 0.708, 0.616),
OR_ci = c(
"0.914 (0.894; 0.935)", "0.961 (0.793; 1.166)", "0.709 (0.577; 0.871)",
"0.523 (0.389; 0.708)", "0.457 (0.339; 0.616)"),
ci = c(
"0.894; 0.935",
"0.793; 1.166",
"0.577; 0.871",
"0.389; 0.708",
"0.339; 0.616"),
no = c(1, 2, 3, 4, 5)
)
forest <- ggplot(
data = tester,
aes(x = treatmentgroup, y = or, ymin = low_ci, ymax = up_ci)) +
geom_pointrange(aes(col = treatmentgroup)) +
geom_hline(yintercept = 1, colour = "black") +
xlab("") +
ylab("OR (95% CI)") +
geom_errorbar(aes(ymin = low_ci, ymax = up_ci, col = treatmentgroup), width = 0, cex = 1) +
theme_classic() +
theme(
panel.background = element_blank(), strip.background = element_rect(colour = NA, fill = NA),
strip.text.y = element_text(face = "bold", size = 12),
panel.grid.major.y = element_line(colour = col_grid, size = 0.5),
strip.text = element_text(face = "bold"),
panel.border = element_rect(fill = NA, color = "black"),
legend.position = "none",
axis.text = element_text(face = "bold"),
axis.title = element_text(face = "bold"),
plot.title = element_text(face = "bold", hjust = 0.5, size = 13)
) +
coord_flip()
dat_table <- tester %>%
select(treatmentgroup, OR_ci) %>%
tidyr::pivot_longer(c(OR_ci), names_to = "stat") %>%
mutate(stat = factor(stat, levels = "OR_ci"))
table_base <- ggplot(dat_table, aes(stat, treatmentgroup, label = value)) +
geom_text(size = 3) +
scale_x_discrete(position = "top", labels = "OR (95% CI)") +
labs(y = NULL, x = NULL) +
theme_classic() +
theme(
strip.background = element_blank(),
panel.grid.major = element_blank(),
panel.border = element_blank(),
axis.line = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(size = 12),
axis.ticks = element_blank(),
axis.title = element_text(face = "bold"),
)
forest + table_base + plot_layout(widths = c(10, 4))
However, my graph ends up with the categories out of order. How can I adjust the order to this one: Education Continuous, 0, 1-4, 5-8, and >8?
I tried factor(tester$treatmentgroup) but it did not work.
Also, how can I make all the categories the same color (black, for example) instead of one each color? I tried eliminating the line geom_pointrange(aes(col = treatmentgroup)) + but it does not work.
You're right that you can convert treatmentgroup to a factor, you just need to specify the levels. Try running this code before you generate your plots with ggplot().
tester <- tester %>%
mutate(treatmentgroup = factor(treatmentgroup,
levels = c(">8", "5-8", "1-4", "0", "Education Continuous")))

r adding line (new coordinates) to ggplot2 scaterplot

How can I add a line with new x coordinates to my scatterplot? I get an error saying the line x-values must match those from my scatterplot.. I tried using geom_line() as you can see
library(ggplot2)
x <- c(1,2,3)
y <- c(4,5,6)
a <- seq(0.5,5, by = 0.5)
b <- seq(1,10)
ggplot(as.data.frame(cbind(x,y)), aes(x, y)) +
geom_point(shape = 1) +
geom_point(aes(2.5,2.5, colour = "My Portfolio"),
shape = 18,
size = 3) +
geom_line(aes(a, b)) +
ggtitle("Efficient Frontier") +
xlab("Volatility (Weekly)") +
ylab("Expected Returns (Weekly)") +
theme(plot.title = element_text(size=14, face="bold.italic", hjust = 0.5, margin=margin(0,0,15,0)),
axis.title.x = element_text(size = 10, margin=margin(15,0,0,0)),
axis.title.y = element_text(size = 10, margin=margin(0,15,0,0)),
panel.border = element_rect(colour = "black", fill=NA, size=1),
legend.position = c(0.93,0.06),
legend.title = element_blank(),
legend.text = element_text(size=8),
legend.background = element_rect(color = "black"),
legend.key=element_blank())
thanks!
You have to provide a new data.frame to geom_line() and set the aestetics accordingly.
library(ggplot2)
x <- c(1,2,3); y <- c(4,5,6)
a <- seq(0.5,5, by = 0.5); b <- seq(1,10)
ggplot(as.data.frame(cbind(x,y)), aes(x, y)) +
geom_point(shape = 1) +
geom_point(aes(2.5,2.5, colour = "My Portfolio"),
shape = 18,
size = 3) +
geom_line(data=data.frame(a,b), mapping=aes(x=a, y=b)) +
ggtitle("Efficient Frontier") +
xlab("Volatility (Weekly)") +
ylab("Expected Returns (Weekly)") +
theme(plot.title = element_text(size=14, face="bold.italic", hjust = 0.5, margin=margin(0,0,15,0)),
axis.title.x = element_text(size = 10, margin=margin(15,0,0,0)),
axis.title.y = element_text(size = 10, margin=margin(0,15,0,0)),
panel.border = element_rect(colour = "black", fill=NA, size=1),
legend.position = c(0.93,0.06),
legend.title = element_blank(),
legend.text = element_text(size=8),
legend.background = element_rect(color = "black"),
legend.key=element_blank())

Resources