Cars包中的多面板散点图

时间:2016-11-23 18:29:53

标签: r plot r-car

我正试图在RStudio中制作一个2面板的人物。通常很简单:

par(mfrow=c(1,2)) #1*2 plotting window

但是,当我使用汽车套件中的scatterplot()函数制作绘图时,它似乎会覆盖绘图窗口的这个分区。

可重复的例子: 我正在寻找的格式可以用:

par(mfrow=c(1,2))
plot(iris$Sepal.Length,iris$Sepal.Width)
plot(iris$Petal.Width,iris$Sepal.Width)

但出于某些原因,我想使用scatterplot()。但是当我再次尝试相同的格式化技巧时,它不起作用。试试这个块:

library(car)
par(mfrow=c(1,2))
scatterplot(Sepal.Length~Sepal.Width|Species,data=iris,grid="FALSE", boxplots="", reg.line="FALSE",pch=c(0,1,2))
scatterplot(Petal.Width~Sepal.Width|Species,data=iris,grid="FALSE", boxplots="", reg.line="FALSE",pch=c(0,1,2))

有人知道解决方法吗?

我还尝试了一种替代结构:

m<-rbind(c(1,2))
layout(m)

没有运气。

更新这个问题在很大程度上是this one的重复,除了我正在寻找它的解决方法,而不是在该页面上留下的响应,基本上只是承认scatterplot()实际上会覆盖par(mfrow)

1 个答案:

答案 0 :(得分:1)

您可以使用chunk参数rmarkdownfig.show='hold'文档中输出并排数字。另一种方法是使用ggplot2创建绘图,而不是cars scatterplot函数。我在下面展示了两种方法。

cars::scatterplot文档

中的并排rmarkdown

以下是PDF输出的示例。 out.width='3in'设置输出文档中每个绘图的实际大小,无论fig.heightfig.width如何。但您仍然可以调整fig.heightfig.width来调整宽高比和相对于绘图区域的文字大小。

---
title: "Untitled"
author: "eipi10"
date: "November 23, 2016"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r fig.show='hold', out.width='3in', fig.height=5, fig.width=5}
library(car)

scatterplot(Sepal.Length~Sepal.Width|Species,data=iris,grid="FALSE",
            boxplots="", reg.line="FALSE", pch=c(0,1,2))
scatterplot(Petal.Width~Sepal.Width|Species,data=iris,grid="FALSE", 
            boxplots="", reg.line="FALSE", pch=c(0,1,2))
```

enter image description here

使用ggplot2

的并排散点图

如果您愿意使用ggplot2,那么您可以相对轻松地获得双图布局:

library(ggplot2)
library(gridExtra)
theme_set(theme_bw())

grid.arrange(
  ggplot(iris, aes(Sepal.Width, Sepal.Length, colour=Species)) +
    geom_point() +
    geom_smooth(alpha=0.2) +
    theme(legend.position="top"),
  ggplot(iris, aes(Sepal.Width, Petal.Width, colour=Species)) +
    geom_point() +
    geom_smooth(alpha=0.2) +
    theme(legend.position="top"),
  ncol=2)

enter image description here

自定义ggplot2图,看起来更像cars::scatterplot输出

您可以通过其他方式自定义上述代码。如果您不想使用置信区间,请将se=FALSE添加到geom_smooth。如果您想为每个物种设置不同的形状,请将aes(shape=Species)添加到geom_point。如果您想要基本图形中使用的特定形状,请添加+ scale_shape_manual(values=0:2)等。您还可以通过一些额外的工作获得单个图例。

在下面的代码中,我添加了这些和其他自定义,以重现更接近原始基本图形图的内容。

# Components we'll reuse for both plots
my_theme = list(geom_point(aes(shape=Species)),
                geom_smooth(se=FALSE, show.legend=FALSE, lwd=0.8),
                scale_shape_manual(values=0:2),
                scale_colour_manual(values=c("black", "red","green")),
                theme_bw(),
                theme(panel.grid.major=element_blank(),
                      panel.grid.minor=element_blank(),
                      legend.position="top"))

p1 = ggplot(iris, aes(Sepal.Width, Sepal.Length, colour=Species)) +
  my_theme +
  labs(x="Sepal Width", y="Sepal Length") +
  scale_y_continuous(limits=c(3,8)) +
  scale_x_continuous(limits=c(1,5)) 

p2 = ggplot(iris, aes(Sepal.Width, Petal.Width, colour=Species)) +
  my_theme +
  labs(x="Sepal Width", y="Petal Width")

# Function to extract legend
# https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

leg = g_legend(p1)

grid.arrange(leg,
  arrangeGrob(grobs=lapply(list(p1,p2), function(p) p + guides(colour=FALSE, shape=FALSE)), ncol=2), 
  ncol=1, heights=c(1,10))

enter image description here