如何保存与Rstudio中显示完全相同的绘图?
我使用ggplot绘制了一幅图像,然后使用如何保存与Rstudio中显示完全相同的绘图?,r,ggplot2,visualization,cowplot,R,Ggplot2,Visualization,Cowplot,我使用ggplot绘制了一幅图像,然后使用cowplot+magick将其与一些图片/徽标组合 问题是关于使用ggsave()保存绘图的问题。图像未按编辑器中显示的方式保存,并且与它们组合的图片/徽标不对齐 我使用过的块设置 knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE, dpi = 300, out.width = "100%",attr.output='style="max
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)
下面是我在r标记源代码编辑器中看到的绘图结果:
下面是使用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,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")
库(tidyverse)
图书馆(比例尺)
图书馆(ggeasy)
图书馆(胶水)
图书馆(magick)
图书馆(cowplot)
#创建df
印度空间研究组织预算%
总结(最大值(初始预算)-最小值(初始预算))%>%pull()
bjp%中的变化
过滤器(第三方==“BJP”,年份>2010年)%>%
总结(最大值(初始预算)-最小值(初始预算))%>%pull()
#创建情节
初始预算图%
ggplot(aes(x=年度,y=初始预算,col=缔约方))+
几何路径(aes(组=1),arrow=arrow(type=“closed”)+
几何线(xintercept=c(20042014),col=c(“红色”))+
geom_标签(
label=glue(“10年内ISRO预算的变化:\u20b9{国会的变化}Cr”),x=2009,y=8500,
label.padding=单位(0.35,“线”),尺寸=3,α=0.4,
label.size=0.25,color=“#00bfc7”,
) +
geom_标签(
label=glue(“5年内预算变化:\n\u20b9{Change_in_bjp}Cr”),x=2019,y=8500,微移x=0.5,
label.padding=单位(0.35,“线”),尺寸=3,α=0.4,
label.size=0.25,color=“#f77667”,
) +
主题(经典)+
实验室(title=“ISRO政府基于其任期内政党的初始预算分配”,
副标题=“ViSa创建的图表”)+
主题(plot.title=element\u text(hjust=0.5),
plot.subtitle=元素\文本(hjust=0.5))+
ggeasy::简易绘图标题大小(大小=12)+
比例x连续(打断=打断宽度(2))+
连续缩放(标签=标签美元(前缀=“\u20b9”,后缀=“Cr”))
#阅读标志
这纯粹是一个设备大小的问题。您只需在ggsave
中增加输出文件的尺寸,以匹配查看器窗口的尺寸谢谢@AllanCameron,正如您正确地说的,它在ggsave
中设置宽度时创建了预期的图像。这就是我使用的ggsave(filename=“ISRO\u budget\u comparison\u 4.jpg”,width=10,dpi=300)