R中的多个图(使用par(new = T/F)) [英] multiple plots in R (using par(new=T/F))

查看:91
本文介绍了R中的多个图(使用par(new = T/F))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用:

在R(Rstudio,mac)的同一图形中绘制多个图.

  plot(X,Y1pch = 0,ylim = c(min_v,max_v),col =红色")par(新= T)阴谋(X,Y2pch = 1ylim = c(min_v,max_v),col =蓝色")par(新= F)阴谋(X,Y3pch = 2ylim = c(min_v,max_v),col =绿色")par(新= F) 

但是,它仅绘制第三张图.
我想念什么?

解决方案

我认为最好在最后两个地方使用 points 而不是绘图.

  min_v<-min(Y1,Y2,Y3)max_v<-max(Y1,Y2,Y3)xr <-范围(X)图(X,Y1,pch = 21,ylim = c(min_v,max_v),xlim = xr,bg ="red",,ylab = expression(paste(Y [i],',i = {1,2,3}')),xlab ="X")点(X,Y2,pch = 22,bg =蓝色")点(X,Y3,pch = 23,bg =绿色") 

情节

如果OP确实想使用 plot 函数,则以下内容可能会有用.(OP产生的主要错误是使用第二个 new = F ,但由于y轴标签彼此重叠等,还会出现其他问题.)

  plot(X,Y1,pch = 21,ylim = c(min_v,max_v),xlim = xr,bg =红色",ylab =",xlab =",)par(新= T)阴谋(X,Y2,pch = 22,ylim = c(min_v,max_v),xlim = xr,bg =蓝色",ylab =",xlab =",)par(新= T)阴谋(X,Y3,pch = 23,ylim = c(min_v,max_v),xlim = xr,bg =绿色",ylab = expression(paste(Y [i],',i = {1,2,3}'))),xlab ="X",)par(新= F) 

ggplot2

当我在这里的时候,这里是使用的数据:

  set.seed(1984)X<-rnorm(10)Y1<-rnorm(10)Y2<-rnorm(10)Y3<-rnorm(10) 

I am trying to draw multiple plots in the same graph in R (Rstudio, mac), using:

plot(
  X,
  Y1,
  pch = 0,
  ylim = c(min_v, max_v),
  col = "red"
)
par(new = T)
plot(
  X,
  Y2,
  pch = 1,
  ylim = c(min_v, max_v),
  col = "blue"
)
par(new = F)

plot(
  X,
  Y3,
  pch = 2,
  ylim = c(min_v, max_v),
  col = "green"
)
par(new = F)

However, it draws only the third plot.
what am I missing?

解决方案

points

I think it is best to use points for the last two instead of plot.

min_v <- min(Y1, Y2, Y3)
max_v <- max(Y1, Y2, Y3)
xr <- range(X)

plot(X, Y1, pch = 21, ylim = c(min_v, max_v),
  xlim = xr, bg = "red", , 
  ylab = expression(paste(Y[i],', i = {1, 2, 3}')), xlab ="X")
points(X, Y2, pch = 22, bg = "blue")
points(X, Y3, pch = 23, bg = "green")

plot

If the OP really wants to use the plot functions, then the following could be useful. (The main error OP made is using the second new=F, but there will be other problems as well since the y-axis labels being on top of each other etc.)

plot(
  X, Y1, pch = 21, ylim = c(min_v, max_v),
  xlim = xr,
  bg = "red",
  ylab = "", xlab ="",
)
par(new = T)
plot(
  X, Y2, pch = 22, ylim = c(min_v, max_v),
  xlim = xr,
  bg = "blue",
  ylab = "", xlab ="",
)

par(new = T)
plot(
  X, Y3, pch = 23, ylim = c(min_v, max_v),
  xlim = xr,
  bg = "green",
  ylab = expression(paste(Y[i],', i = {1, 2, 3}')),
  xlab ="X",
)
par(new = F)

ggplot2

While I'm at it, here's version of it too.

library(ggplot2)
df <- data.frame(X=X, Y1=Y1, Y2=Y2, Y3=Y3)
p1 <- ggplot(df, aes(x = X, y=Y1)) + geom_point(color = "red")
p1 <- p1 + geom_point(color = "blue", aes(y=Y2))
p1 <- p1 + geom_point(color = "black", aes(y=Y3)) 
p1 + xlab("X") + ylab("Y")
p1

Data used:

set.seed(1984)
X <- rnorm(10)
Y1 <- rnorm(10)
Y2 <- rnorm(10)
Y3 <- rnorm(10)

这篇关于R中的多个图(使用par(new = T/F))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆