I am trying to get a plot to occupy the whole of a landscape page. I am using Rmarkdown and knitting a pdf document. I have also tried to change the chunk option and no luck, please help before I pull my hair out. I tried to modify the size in ggsave, it is not changing the size the whole plot. I have tried to modify aspect/ratio, I have tried to modify the chunk, no luck either.
image_path <- "C:/PATH TO IMAGES"
image_files <- c(
"2021 Summer.jpg",
"2021 Autum.jpg",
"2021 Winter.jpg",
"2021 Spring.jpg",
"2022 Summer.jpg",
"2022 Autum.jpg",
"2022 Winter.jpg",
"2022 Spring.jpg",
"2023 Summer.jpg",
"2023 Autum.jpg",
"2023 Winter.jpg",
"2023 Spring.jpg"
)
# Loop through each image file
#for (filename in image_files) {
# Read the image
#image_path_full <- file.path(image_path, filename)
#image <- image_read(image_path_full)
# Rotate the image 90 degrees clockwise
#rotated_image <- image_rotate(image, degrees = -90)
# Show the rotated image (optional)
#plot(as.raster(rotated_image))
#}
#print("Images rotated successfully.")
# Function to create a plot for each image file with year label
create_image_plot <- function(file, year_label) {
image <- readJPEG(file.path(image_path, file))
aspect_ratio <- dim(image)[2] / dim(image)[1]
ggplot() +
annotation_custom(grid::rasterGrob(image, width = unit(0.8, "npc"), height = unit(0.8 * aspect_ratio, "npc")),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) +
labs(subtitle = year_label) +
theme_void() +
theme(plot.margin = margin(0),
plot.subtitle = element_text(hjust = 0.5)) # Center the year label
}
# Create a list to hold the image grobs
image_grobs <- mapply(create_image_plot, file = image_files, year_label = substr(image_files, 1, 4), SIMPLIFY = FALSE)
# Create a list of legend plots
legends <- lapply(c("Summer", "Autumn", "Winter", "Spring"), function(season) {
ggplot() +
geom_blank() +
labs(title = season) +
theme_void() +
theme(legend.position = "none", plot.title = element_text(hjust = 0.5))
})
# Combine legends into one plot
legend_plot <- grid.arrange(
grobs = legends,
nrow = 1,
widths = rep(1, 4)
)
newpage
blandscape
thispagestyle{empty}
{r echo=FALSE, warning=FALSE, message=FALSE, fig.align='center',fig.width=10, fig.height=8, fig.cap = paste("Soil Adjusted Vegetation Index (SAVI) imagery at the ", Carbon_Project, " between 2021 and 2023 for each season."), dpi=800}
# Arrange the plots and legend in a grid
combined_plot <- grid.arrange(
legend_plot,
arrangeGrob(
do.call(arrangeGrob, c(image_grobs[c(1, 5, 9)], ncol = 1)),
do.call(arrangeGrob, c(image_grobs[c(2, 6, 10)], ncol = 1)),
do.call(arrangeGrob, c(image_grobs[c(3, 7, 11)], ncol = 1)),
do.call(arrangeGrob, c(image_grobs[c(4, 8, 12)], ncol = 1)),
ncol = 4
),
nrow = 3,
heights = unit(c(0.15, rep(1, 2)), "null")
)
# Save the combined plot as an image
ggsave("combined_plot.png", combined_plot)
elandscape
newpage