如何使保存的图片有较高的分辨率
1.之前我都是保存为bmp格式,但是保存为bmp格式之后,发现保存的图片并不是我想要的,R语言画图产生的图片如下:
这个是R语言直接输出的图片,我还没对图片进行保存,如果保存成bmp格式,图片如下:
bmp(file ="甜甜圈.bmp",width =720*2,height = 720*2,units = "px",res =72*2)
我稍微放大了一点,可以看出,图片边缘有很多锯齿,图片的大小为1.97MB,分辨率为,1440*1440
接下来我们试试能否通过提高分辨率来消除边缘的锯齿
bmp(file ="甜甜圈02.bmp",width =720*5,height = 720*5,units = "px",res =72*5)
提高分辨率后,锯齿确实得到了消除,但是,此时图片的分辨率为3600*3600,内存为37MB,太大了
如果我们换成jpeg格式,看看效果如何?
jpeg(file ="甜甜圈03.jpeg",width =1000*15,height = 1000*15,units = "px",res =72*15)
可以看出锯齿消除效果非常好,分辨率为15000*15000,此时图片的内存大小只有3.71MB
## ----------------------------------------------------
library(tidyverse)
library(ggsci)
library(ggrepel)
library(cowplot)
## ----------------------------------------------------
browsers <- read.csv(file = 'data/ggplot/browers.csv') %>%
arrange(desc(version)) %>%
mutate(cumsum_share = cumsum(share))
browsers
## ----------------------------------------------------
browsers_sum <- group_by(browsers, browser) %>%
summarise(browser_share = sum(share)) %>%
arrange(desc(browser)) %>%
mutate(cumsum_browser_share = cumsum(browser_share))
browsers_sum
## ----------------------------------------------------
# ggplot(data = browsers_sum, aes(x = 1,
# y = browser_share)) +
# geom_col(color = "white", width = 1, aes(fill = browser)) +
# geom_text(data = filter(browsers_sum, browser_share >=5),
# size = 3,
# color = "white",
# aes(y = cumsum_browser_share - 0.5 * browser_share,
# label = str_c(browser, "\n", browser_share, "%"))) +
# geom_text_repel(data = filter(browsers_sum, browser_share < 5),
# size = 3,
# nudge_y = 7,
# segment.color = "white",
# min.segment.length = 0,
# color = "white",
# aes(y = cumsum_browser_share - 0.5 * browser_share,
# label = str_c(browser, "\n", browser_share, "%"))
# ) +
# scale_fill_aaas()
#
#
# ## ----------------------------------------------------
# ggplot(data = browsers_sum, aes(x = 1,
# y = browser_share)) +
# geom_col(color = "white", width = 1, aes(fill = browser)) +
# geom_text(data = filter(browsers_sum, browser_share >=5),
# size = 3,
# color = "white",
# aes(y = cumsum_browser_share - 0.5 * browser_share,
# label = str_c(browser, "\n", browser_share, "%"))) +
# geom_text_repel(data = filter(browsers_sum, browser_share < 5),
# size = 3,
# nudge_y = 7,
# segment.color = "white",
# min.segment.length = 0,
# color = "white",
# aes(y = cumsum_browser_share - 0.5 * browser_share,
# label = str_c(browser, "\n", browser_share, "%"))
# ) +
# scale_fill_aaas() +
# theme_nothing() +
# coord_polar(theta = "y")
#
#
# ## ----------------------------------------------------
# ggplot(data = browsers_sum, aes(x = 1,
# y = browser_share)) +
# geom_col(color = "white", width = 1, aes(fill = browser)) +
# geom_text(data = filter(browsers_sum, browser_share >=5),
# size = 3,
# color = "white",
# aes(y = cumsum_browser_share - 0.5 * browser_share,
# label = str_c(browser, "\n", browser_share, "%"))) +
# geom_text_repel(data = filter(browsers_sum, browser_share < 5),
# size = 3,
# nudge_y = 7,
# segment.color = "white",
# min.segment.length = 0,
# color = "white",
# aes(y = cumsum_browser_share - 0.5 * browser_share,
# label = str_c(browser, "\n", browser_share, "%"))
# ) +
# xlim(-0.5, 1.5) +
# scale_fill_aaas()
#
#
# ## ----------------------------------------------------
# ggplot(data = browsers_sum, aes(x = 1,
# y = browser_share)) +
# geom_col(color = "white", width = 1, aes(fill = browser)) +
# geom_text(data = filter(browsers_sum, browser_share >=5),
# size = 3,
# color = "white",
# aes(y = cumsum_browser_share - 0.5 * browser_share,
# label = str_c(browser, "\n", browser_share, "%"))) +
# geom_text_repel(data = filter(browsers_sum, browser_share < 5),
# size = 3,
# nudge_y = 7,
# segment.color = "white",
# min.segment.length = 0,
# color = "white",
# aes(y = cumsum_browser_share - 0.5 * browser_share,
# label = str_c(browser, "\n", browser_share, "%"))
# ) +
# xlim(-0.5, 1.5) +
# scale_fill_aaas() +
# theme_nothing() +
# coord_polar(theta = "y")
## ----------------------------------------------------
# bmp(file ="甜甜圈.bmp",width =720*2,height = 720*2,units = "px",res =72*2)
# bmp(file ="甜甜圈02.bmp",width =720*5,height = 720*5,units = "px",res =72*5)
jpeg(file ="甜甜圈03.jpeg",width =1000*15,height = 1000*15,units = "px",res =72*15)
# x=1.3代表左侧矩形的位置
ggplot(data = browsers_sum, aes(x = 1, y = browser_share)) +
geom_col(color = "white",
# width代表左侧矩形的宽度
width = 1,
aes(fill = browser),
linewidth=0.1) +
geom_text(data = filter(browsers_sum, browser_share >=5),
size = 2,
color = "black",
aes(y = cumsum_browser_share - 0.5 * browser_share,
label = str_c(browser, "\n", browser_share, "%"))) +
geom_text_repel(data = filter(browsers_sum, browser_share < 5),
size = 2.5,
nudge_y = 7,
segment.color = "white",
min.segment.length = 0,
color = "white",
aes(y = cumsum_browser_share - 0.5 * browser_share,
label = str_c(browser, "\n", browser_share, "%"))) +
geom_col(data = browsers,
# width代表右侧矩形的宽度
width = 0.1,
# x=2.3代表右侧矩形的位置
aes(x = 1.6, y = share, fill = version),
color="white",
linewidth=0.1) +
geom_text(data = filter(browsers, share > 4),
size = 2,
color = "white",
#aes中x的大小代表固定短线的长度,变换Firefox 3.5的位置
aes(x = 2, y = cumsum_share - 0.5 * share,
label = str_c(version, '\n', share, '%'))) +
geom_text_repel(data = filter(browsers, share <= 4),
size = 2.5,
color = "black",
nudge_x = 0.1,
xlim = c(2.7, Inf),
min.segment.length = 0,
#aes中x的大小代表Firefox 3.5 1.58%固定,变换短线的长度
aes(x =2.7, y = cumsum_share - 0.5 * share,
label = str_c(version, '\n', share, '%'))) +
scale_fill_igv() +
#xlim代表两个图形处于0.5到2.8的范围内
xlim(0.5, 2.8) +
theme_nothing()+
coord_polar(theta = "y")
dev.off()
browers.csv