如何在Rmarkdown文件和Rmarkdown html文档中设置绘图的缩放比例,适合度和分辨率?

时间:2020-09-28 10:36:50

标签: r ggplot2 r-markdown visualization

我从以下网站创建了一个地块作为参考:

Plot Web Reference

问题

当我在Rmarkdown源代码编辑器中绘制相同内容或从中创建html文档时,它的大小和分辨率在网站上都不会显示。

我如何原样复制它?

设置参数

我用过dpi = 300fig.height = 15,不管我用什么out.width都无济于事。

以下是网站与我的rmarkdown编辑器的快照比较。

Left来自原始网站; Right来自我的Rmarkdown编辑器

enter image description here

下面的临时解决方案-仅适用于此图表

原来的设置fig.width=7限制了地块的缩放,并且只能在网站中使用。

但是在其他类似的图表中,当x轴缩放到更高的值时,此问题就会出现在使用这种方法无法解决的问题上。

如果x轴值增加很多,如何重新缩放轴?

代码可在网站上找到,也可以在下面发布:

library(tidyverse)
library(glue)

gapminder <- read.csv("https://raw.githubusercontent.com/swcarpentry/r-novice-gapminder/gh-pages/_episodes_rmd/data/gapminder-FiveYearData.csv")

gapminder <- gapminder %>% mutate_if(is.character, as.factor)
gapminder_life_exp_diff  <- gapminder %>% 
                              filter(year %in% c(1952,2007)) %>% 
                              
                              # filter(country %in% c("India","Vietnam")) %>% 
                              
                              arrange(country, year) %>% 
                              
                              group_by(country) %>% 
                              
                              mutate(lifeExp_diff = lifeExp[2] - lifeExp[1],
                                     max_pop = max(pop)) %>% 
                              
                              ungroup() %>% 
                              
                              arrange(lifeExp_diff) %>% 
                              
                              filter(max_pop > 30000000) %>% 
                              
                              mutate(country = droplevels(country)) %>% 
                              select(country, year, continent, lifeExp, lifeExp_diff)

# Dumbell plot

gapminder_life_exp_diff %>% 
  mutate(country = fct_inorder(country)) %>% 
  
  group_by(country) %>% 
  
  mutate(max_lifeExp = max(lifeExp),
         min_lifeExp = min(lifeExp)) %>% 
  
  ungroup() %>% 
  
  # plotting begins
  ggplot() +
  geom_segment(aes(x = min_lifeExp, xend = max_lifeExp,
                   y = country, yend = country,
                   col = continent), alpha = 0.5, size = 7) +
  
  geom_point(aes(x = lifeExp, y = country, col = continent), size = 8) +
  
  geom_text(aes(x = min_lifeExp + 0.25, y = country,
                label = paste(country, round(min_lifeExp))),
            col = "grey50", hjust = "right") +
  
  geom_text(aes(x = max_lifeExp - 0.35, y = country,
                label = round(max_lifeExp)),
            col = "grey50", hjust = "left") +
  
  scale_x_continuous(limits = c(20,85)) +
  
  scale_color_brewer(palette = "Pastel2") +
  
  labs(title = "Change in Life Expectancy",
       subtitle = "Between years 1952 and 2007",
       col = "Continent") +
  
  # background & theme settings
  theme_classic() +
  
  theme(legend.position = "top", 
        axis.line = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank()
        )

0 个答案:

没有答案