如何在rmarkdown编辑器和针织html文档中正确拟合图形?

时间:2020-09-30 18:36:26

标签: r ggplot2 r-markdown visualization

我正在创建一些在rmarkdown编辑器和html文档中存在对齐问题的图。

即使使用fig.align='center',图形也不会居中对齐,并且其边缘也被切掉(所附图像中的PS:国家/地区的名称已在左侧切出)。

如何使图表在x轴上滚动,而不是缩小或缩小,以致于无法阅读。

例如,此网页上的图表可滚动而不是按比例缩小:https://cran.r-project.org/web/packages/gapminder/README.html

块设置的使用如下图所示: enter image description here

我还使用了以下块设置

knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE, dpi = 300, cache = FALSE, attr.output='style="max-height: 300px;"')

下图显示了国名被删减的问题 enter image description here

引用代码

library(tidyverse)

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_gdpPercap_diff  <- gapminder %>% 
                              filter(year %in% c(1952,2007)) %>% 
                              
                              # filter(country %in% c("India","Vietnam")) %>% 
                              
                              arrange(country, year) %>% 
                              
                              group_by(country) %>% 
                              
                              mutate(gdpPercap_diff = gdpPercap[2] - gdpPercap[1],
                                     max_pop = max(pop)) %>% 
                              
                              ungroup() %>% 
                              
                              arrange(gdpPercap_diff) %>% 
                              
                              filter(max_pop > 30000000) %>% 
                              
                              mutate(country = droplevels(country)) %>% 
                              select(country, year, continent, gdpPercap, gdpPercap_diff)

gapminder_gdpPercap_diff %>% 
  mutate(country = fct_inorder(country)) %>% 
  
  group_by(country) %>% 
  
  mutate(max_gdpPercap = max(gdpPercap),
         min_gdpPercap = min(gdpPercap)) %>% 
  
  ungroup() %>% 

 # plotting begins
  ggplot() +
  geom_segment(aes(x = min_gdpPercap, xend = max_gdpPercap,
                   y = country, yend = country,
                   col = continent), alpha = 0.5, size = 7) +
  
  geom_point(aes(x = gdpPercap, y = country, col = continent), size = 8, alpha = .8) +
  
  geom_text(aes(x = min_gdpPercap + 10, y = country,
                label = paste(country, round(min_gdpPercap))),
            col = "grey50", hjust = "right") +
  
  geom_text(aes(x = max_gdpPercap - 8.0, y = country,
                label = round(max_gdpPercap)),
            col = "grey50", hjust = "left") +
  
  # scale_x_continuous(limits = c(20,85)) +
  
  scale_color_brewer(palette = "Pastel2") +
  
  labs(title = "Change in GDP Per Capita",
       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()
        )

如果我按照答案中的建议添加scale_x_continuous(limits = c(-1000,35000)),则图表将变得太小而无法阅读国家/地区名称和数字(请参见下图)。我希望图表可读,即使我对此有滚动显示。

enter image description here

2 个答案:

答案 0 :(得分:0)

您非常接近想要的东西。可以手动设置scale_x_continuous来处理可见性。您已经在代码中对其进行了注释。

尝试scale_x_continuous(limits = c(-5000,40000)),您可以根据自己的满意程度进行调整。

答案 1 :(得分:0)

您执行新的scale_x_continuous,但是使用扩展而不是限制,scale_x_continuous(expand = expansion(mult = c(.25)))为我工作。