Knitr只产生一半的情节

时间:2016-04-03 12:42:22

标签: r plot knitr lattice

我在最终制作.md.html文件时遇到问题:

该地块应包含2个方面,.md仅显示一个。

相同的R脚本生成正确的图。我尝试了latticeggplot2系统-result是相同的:在Rstudio(Mac)中情节是正确的,在.md.html中丢失了1个方面。

library(lattice)
xyplot(steps ~ interval | day_type, data = mean_tsd_final, layout = c(1, 2),
       type="l", xlab = "Interval", ylab = "Number of steps")

> str(mean_tsd_final)
'data.frame':   576 obs. of  3 variables:
 $ interval: int  0 5 10 15 20 25 30 35 40 45 ...
 $ day_type: Factor w/ 2 levels "weekday","weekend": 1 1 1 1 1 1 1 1 1 1 ...
 $ steps   : num  2.251 0.445 0.173 0.198 0.099 ...

由R-script制作

Produced by R-script

由knitr用相同的代码制作

Produced by the same code by knitr

1 个答案:

答案 0 :(得分:0)

所以答案是: 用于设置本地首选项的代码:

## To set it in english
Sys.setlocale("LC_TIME","en_US.UTF-8")

这很重要,因为我们使用字符串比较来生成因子变量。 以下是making mean_td_final数据框的代码:

##Adding column with type of the day
Activity_data_filled$day_type <- weekdays(as.Date(Activity_data_filled$date))
Activity_data_filled$day_type <- ifelse(Activity_data_filled$day_type %in% c("Saturday", "Sunday"),"weekend", "weekday")
Activity_data_filled$day_type <-as.factor(Activity_data_filled$day_type)

##Aggegating by type of the day and time intervals
mean_tsd_final <- aggregate(Activity_data_filled$steps,
                       by=list(Activity_data_filled$interval,
                               Activity_data_filled$day_type),mean)

如果没有覆盖本地设置,ifelse函数只生成1个“工作日”级别,因为它将字符串“星期六”和“星期日”与weekdays函数生成的日期的本地名称进行比较。 (俄语,就我而言)。