knitr :: spin并不总是为ggplot添加换行符

时间:2016-08-23 21:56:47

标签: r rstudio knitr r-markdown

我尝试使用虚拟数据重现这一点,但无法重现,但我会尽力将无法共享的生产数据中的详细信息合并。

我正在尝试使用knitr :: spin()将rmd文件编织到md(具体来说,使用ezknitr包装器)。

有时多个ggplots有一个换行符,但有时候却没有换行符,导致页面上出现大块的情节。

我不确定为什么有些图表在不同的行上,而其他图表不在最终的.md输出中。

这是一个示例.md输出:

some text................................

other text............................................

![plot of chunk unnamed-chunk-3](reports/figs/unnamed-chunk-3-1.png)![plot of chunk unnamed-chunk-3](reports/figs/unnamed-chunk-3-2.png)![plot of chunk unnamed-chunk-3](reports/figs/unnamed-chunk-3-3.png)![plot of chunk unnamed-chunk-3](reports/figs/unnamed-chunk-3-4.png)


![plot of chunk unnamed-chunk-4](reports/figs/unnamed-chunk-4-1.png)

和用于生成此md的代码:

some text................................

other text............................................

```{r}
ca <- filter(sv.cal, role %in% c("CA", "CAEX", "CABS")) %>%
  select(emp_id, region, total_sales = ann.sales)

ca2 <- filter(sv.cal, role %in% c("CA", "CAEX", "CABS"), total_sales > 0) %>%
  select(emp_id, region, total_sales = ann.sales)


g <- ggplot(data=ca, aes(x=total_sales / 1000 )) +
  geom_freqpoly(bins=20, aes(color = region)) + 
  scale_x_continuous(limits = c(0, 1e03)) + 
  scale_y_continuous(limits = c(0, 150)) + 
  labs(x = "Total Sales by CAs $M", y = "Count of CAs", title = "No significant differentiation in sales by CAs along Region/MGO")
g  

ggplot(data=ca2, aes(region, total_sales)) +
  geom_boxplot() + 
  scale_y_continuous(labels = dollar) + 
  labs(x = "Region", y = "$M") + 
  coord_cartesian(ylim = c(0,2000000))


bai <- filter(sv.total, role %in% c("BAI")) %>%
  mutate(ann.sales = annualize(total_sales, nmonth))

ggplot(data=bai, aes(region, ann.sales / 1e06)) +
  geom_boxplot() + 
  scale_y_continuous(labels = comma) + 
  labs(x = "Region", y = "$MM") +
  coord_cartesian(ylim = c(0,20))

ggsave(file = "img/bai_performance.png", scale = 2)
ca_percentile <- ca %>%
  group_by(region, emp_id) %>%
  summarise(total_sales = sum(total_sales)) %>%
  group_by(region) %>%
  crossing(p = seq(.1, .9, .1)) %>%
  mutate(q = quantile(total_sales, p)) %>%
  select(-total_sales) %>%
  select(-emp_id) %>%
  distinct(region, p, .keep_all = TRUE)

ggplot(data=ca_percentile, aes(p, q, colour = region)) +
  geom_line() + 
  labs( x = "Percentile", y = "Sales Volume $", title = "Distribution of Sales by Region for CAs shows no material change between CA performance across regions") + 
  scale_x_continuous(labels = percent) + 
  scale_y_continuous(labels = dollar)
ggsave(file = "img/ca_performance_quantile.png", scale = 2)

```

0 个答案:

没有答案
相关问题