在一个图中组合多个图

时间:2016-02-27 09:23:04

标签: r pie-chart boxplot

我想在一张图上组合2个饼图和2个箱形图。结合这个的简单方法是什么?我的代码如下所示。

x <-  c(20, 12, 4, 16, 8)
y <- c(30, 12,45, 20, 9)
names <- c("germany","france","australia","vienna","hungary")
par(fig=c(0,0.5,0,1))
p1 <- pie(x, names, col = c("purple", "cyan", "blue","red","yellow"))
title("pie1", line=-9)
par(fig=c(0.5,1,0,1),new=TRUE)
p2 <-pie(y, names, col = c("purple", "cyan", "blue", "red", "yellow"))
title("pie2", line=-9)

x = read.csv ("data1.csv")
y = read.csv ("data2.csv")
p3 <-boxplot(x,col=c("green","blue","red"), main = "boxplot1", ylim=c(0,10))
p4 <-boxplot(y,col=c("green","blue","red"), main = "boxplot2", ylim=c(0,10))

data1.csv

c1 c2 c3
5   7  8
4   5  7
5   5  5
6   5
10
3

data2.csv

   c1  c2  c3
    5   7  8
    7   4  7
    5   3  5
    6   5
    9
    3

1 个答案:

答案 0 :(得分:0)

您可以使用par

更改绘图布局,使其中包含多个绘图
par(mfrow=c(2,2))
p1
p2
p3
p4

请务必在事后更改:

par(mfrow=c(1,1))

有时会出现绘图区域太小的错误。在这种情况下,您将不得不更改单个图的外边距以减少空格:

par(oma=c(a,b,c,d))

其中a = left,b = top,c = right,d = bottom。