Markdown输出和RStudio输出在绘图输出方面的差异

时间:2014-02-16 09:04:38

标签: r plot statistics dataframe markdown

对于马力高于或等于中等的汽车,如何制作带有mpg地毯的密度图并放入底行(行= 3)?问题是以下代码在RStudio中听起来有效(不确定我是否正确!!)但是当我在.rmd文件中写入它而knit将它写入html时它会显示逐个绘制与预期不相似的情节。知道编织会发生什么吗?

mat <- matrix(1:3, ncol=1, nrow=3)
mat <- rbind(cbind(0, mat), 0)
plot(density(mtcars$mpg),ylim=c(0,0.15), xlim=c(0,40))
rug(mtcars$mpg, col=2, lwd=3.5,ylim=c(0,0.15), xlim=c(0,40))
lower_mpg=mtcars$mpg[mtcars$mpg < 19.20]
plot(density(lower_mpg),ylim=c(0,0.15), xlim=c(0,40))
rug(lower_mpg, col=2, lwd=3.5,ylim=c(0,0.15), xlim=c(0,40))
(summary(mtcars$mpg))
greater_mpg=mtcars$mpg[mtcars$mpg >= 19.20]
plot(density(greater_mpg),ylim=c(0,0.15), xlim=c(0,40))
rug(greater_mpg, col=2, lwd=3.5,ylim=c(0,0.15), xlim=c(0,40))

P.S。:如果我不告诉行号,它只是将它们堆叠在一起,当我告诉它时,它会给我警告。但是我不确定我是否正确选择了mtcars $ mpg? P.S。:我可以在开头只定义一次xlim和ylim吗?喜欢怎么样?

> summary(mtcars$mpg)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  10.40   15.42   19.20   20.09   22.80   33.90 
greater_mpg=mtcars$mpg[mtcars$mpg >= 19.20]

RStudio中显示的内容: enter image description here 我在knit html中看到的内容: enter image description here

这里出现了问题定义,我相信我这样做的正确率为80%: enter image description here

1 个答案:

答案 0 :(得分:2)

感谢我的评论,我使用了以下代码,它也适用于markdown

par(mfrow=c(3, 1))
mat <- rbind(cbind(0, mat), 0)
plot(density(mtcars$mpg),ylim=c(0,0.15), xlim=c(0,40))
rug(mtcars$mpg, col=2, lwd=3.5,ylim=c(0,0.15), xlim=c(0,40))
lower_mpg=mtcars$mpg[mtcars$mpg < 19.20]
plot(density(lower_mpg),ylim=c(0,0.15), xlim=c(0,40))
rug(lower_mpg, col=2, lwd=3.5,ylim=c(0,0.15), xlim=c(0,40))
(summary(mtcars$mpg))
greater_mpg=mtcars$mpg[mtcars$mpg >= 19.20]
plot(density(greater_mpg),ylim=c(0,0.15), xlim=c(0,40))
rug(greater_mpg, col=2, lwd=3.5,ylim=c(0,0.15), xlim=c(0,40))

enter image description here