问题描述
我已经使用ggplot绘制了图像,然后使用cowplot + magick
将其与一些图片/徽标结合在一起。
问题正在使用ggsave()
图像保存地块,因为在编辑器中查看时图像并没有保存,并且对与它们组合的图片/徽标不对齐。
我使用的大块设置
knitr::opts_chunk$set(echo = TRUE,message = FALSE,warning = FALSE,dpi = 300,out.width = "100%",attr.output='style="max-height: 300px;"',fig.width = 10)
以下是我在Rmarkdown source editor
中看到的绘图结果:
以下是使用ggsave()
如何解决此问题,以便保存与编辑器中看到的图像相同的图像?
用于组合绘图和图像/徽标的代码:
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,2164,2274,2540,2675,2997,3290,3499,4167,4880,4432,5172,5826,6959,8045,9155,11200,Party = c("BJP","BJP","Congress","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,nudge_x = 0.5,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,x = .26,x = .8,valign = .85)
# saving image
ggsave(filename = "ISRO_budget_comparison_3.jpg")
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)