I have plotted an image using ggplot & then combined it with some pics/logos using cowplot + magick
.
Issue is on saving the plot using ggsave()
image doen't save as it looked in the editor and gets misaligned with respect to pics/logos combined with them .
chunk settings that I have used
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE, dpi = 300, out.width = "100%",attr.output='style="max-height: 300px;"', fig.width = 10)
Below is the plot result that I see in Rmarkdown source editor
:
Below is the image that gets saved by using ggsave()
How can I fix this so that it saves the same image as what i see in editor ?
Code used to combine plots & images/logos:
library(tidyverse)
library(scales)
library(ggeasy)
library(glue)
library(magick)
library(cowplot)
# creating df
isro_budget <- data.frame(Year = 1999:2020,
Initial_Budget = c(1766,2019,2030,2263,2368,2731,3148,3610,3859,4074,4959,
5778,6626,6715,6792,7238,7388,7509,9094,10783,12473,13479),
Revised_Budget = c(1726,1909,1909,2164,2274,2540,2675,2997,3290,3499,4167,
4880,4432,4880,5172,5826,6959,8045,9155,11200,12473,13479),
Party = c("BJP","BJP","BJP","BJP","BJP","Congress","Congress","Congress",
"Congress","Congress","Congress","Congress","Congress","Congress",
"Congress","BJP","BJP","BJP","BJP","BJP","BJP","BJP")
)
# Growth Values for labels
change_in_congress <- isro_budget %>%
filter(Party == "Congress") %>%
summarise(max(Initial_Budget) - min(Initial_Budget)) %>% pull()
change_in_bjp <- isro_budget %>%
filter(Party == "BJP", Year > 2010) %>%
summarise(max(Initial_Budget) - min(Initial_Budget)) %>% pull()
# creating plot
Initial_budget_plot <- isro_budget %>%
ggplot(aes(x=Year, y=Initial_Budget, col = Party) ) +
geom_path(aes(group = 1), arrow = arrow(type = "closed")) +
geom_vline(xintercept = c(2004,2014), col = c("red")) +
geom_label(
label=glue("Change in ISRO Budget in 10 years: \u20b9 {change_in_congress} Cr"), x=2009, y= 8500,
label.padding = unit(0.35, "lines"), size = 3, alpha = 0.4,
label.size = 0.25, color = "#00bfc7",
) +
geom_label(
label=glue("Change in\nISRO Budget\nin 5 years:\n \u20b9 {change_in_bjp} Cr"), x=2019, y= 8500, nudge_x = 0.5,
label.padding = unit(0.35, "lines"), size = 3, alpha = 0.4,
label.size = 0.25, color = "#f77667",
) +
theme_classic() +
labs(title = "ISRO Initial Budget allocation by govt. based on Political Party in their tenure",
subtitle = "Chart created by ViSa") +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5)) +
ggeasy::easy_plot_title_size(size = 12) +
scale_x_continuous(breaks = breaks_width(2)) +
scale_y_continuous(labels =label_dollar(prefix = "\u20b9", suffix = " Cr") )
# reading logos
congress_img <- magick::image_read("./logos/congress-logo.png")
bjp_img <- magick::image_read("./logos/Bharatiya-Janata-Party-Logo-PNG.png")
# drawing plot & logos
ggdraw() +
draw_plot(Initial_budget_plot) +
draw_image(congress_img,
scale = .2, x = .5,
hjust = 1, halign = 1, valign = .85) +
draw_image(bjp_img,
scale = .2, x = .26, hjust = 1, halign = 1, valign = .85) +
draw_image(bjp_img,
scale = .2, x = .8, hjust = 1, halign = 1, valign = .85)
# saving image
ggsave(filename = "ISRO_budget_comparison_3.jpg")