R中有多个图,每个轴的设置不同,代码行少 [英] Multiple plots in R with different settings for each axis with less lines of code

查看:163
本文介绍了R中有多个图,每个轴的设置不同,代码行少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的图表中,


  • 是否可以使用较少的代码行创建相同的图形?我的意思是,因为每个图。 AD有不同的标签设置,因此我必须为每个图设置一个设置,这会使其更长。



下面的图形是用数据在pdf设备中。

对这些帮助问题是高度赞赏(新手到R!)。由于所有的代码都过长,所以我在这里发布了一个与图C相关的部分。

.photobucket.com / albums / r730 / max4m4 / stackexchangecom / stackfig1_2_zps538a2371.jpgalt =来自R的图表

 # FigC 
label1 = c(0,100,200,300)
plot(data $ TimeVariable2C,data $ Variable2C,axes = FALSE,ylab =,xlab =,xlim = c(0,24),
ylim = c(0,2.4),xaxs =i,yaxs =i,pch = 19)
lines(data $ TimeVariable3C,data $ Variable3C)
axis(2,tick = T,at = seq(0.0,2.4,by = 0.6),label = seq(0.0,2.4,by = 0.6))
axis(1,tick = T,at = seq(0,24,by = 6),label = seq(0,24,by = 6))
mtext((C),side = 1,outer = F,line = -10,adj = 0.8)
minor.tick(nx = 5,ny = 5)

par(new = TRUE)
plot(data $ TimeVariable1C,data $ Variable1C,axes = FALSE,xlab =,ylab =,type =l,
ylim = c(800,0),xaxs =i,yaxs =i)
轴(3,xlim = c(0,24 ),tick = TRUE,at = seq(0,24,by = 6),label = seq(0,24,by = 6),col.axis =violetred4,col =violetred4)
(4,tick = TRUE,at = label1, label = label1,col.axis =violetred4,col =violetred4)
polygon(data $ TimeVariable1C,data $ Variable1C,col ='violetred4',border = NA)


解决方案

您在同一个OP中问很多问题。我会试着回答一个问题:如何简化你的代码,或者如何为每个字母调用一次。我认为最好将数据放在长格式中。例如,这将创建一个4元素的列表

  ll < -  lapply(LETTERS [1:4],function( let){
dat.let< - dat [,grepl(let,colnames(dat))]
dd< - reshape(dat.let,direction ='long',
v.names = c('TimeVariable','Variable'),
varying = 1:6)
dd $ time < - factor(dd $ time)
dd $ Type< - 让
dd
}

ll是一个列表4个data.frame,其中每个看起来像:

  head(ll [[1]])
time TimeVariable变量id类型
1.1 1 0 0 1 A
2.1 1 0 5 2 A
3.1 1 8 110 3 A
4.1 1 16 0 4 A
5.1 1不适用5 A
6.1 1不适用6 A

然后你可以像使用它一样例如:

library(Hmisc)

  l ayout(矩阵(1:4,2,2,byrow = TRUE))
lapply(ll,函数(data){
label1 = c(0,100,200,300)
类型< - unique数据$类型)
dat < - 子集(数据,时间== 2)
x.mm < - max(dat $ Variable,na.rm = TRUE)
plot(dat $ TimeVariable,dat $ Variable,axes = FALSE,ylab =,xlab =,xlim = c(0,x.mm),
ylim = c(0,2.4),xaxs =i ,
dat( - 数据,时间== 2)
lines(dat $ TimeVariable,dat $ Variable)
轴(2,yaxs =i,pch = 19) tick = T,at = seq(0.0,2.4,by = 0.6),label = seq(0.0,2.4,by = 0.6))
axis(1,tick = T,at = seq(0,x。 mm,by = 6),label = seq(0,x.mm,by = 6))
mtext(Type,side = 1,outer = F,line = -10,adj = 0.8)$ b $ (数据,时间== 1)b $ minor.tick(nx = 5,ny = 5)
par(new = TRUE)
dat < - subset(data,time == 1)
plot(dat $ TimeVariable, dat $ variable,axes = FALSE,xlab =,ylab =,type =l,
ylim = c(800,0),xaxs =i,yaxs =i)$ b = b轴(3,xlim = c(0,24),tick = TRUE,at = seq(0,24,by = 6),label = seq(0,24,by = 6),col.axis = violetred4,col =violetred4)
轴(4,tick = TRUE,at = label1,label = la bel1,col.axis =violetred4,col =violetred4)
多边形(dat $ TimeVariable,dat $ Variable,col ='violetred4',border = NA)
})

使用长数据格式的另一个优点是使用``ggplot2 facet_wrap`。

  ##将您的数据转换为data.frame 
dat.l< ; - do.call(rbind,ll)
library(ggplot2)
ggplot(subset(dat.l,time!= 1))+
geom_line(aes(x = TimeVariable,y =变量,group = time,color = time))+
geom_polygon(data = subset(dat.l,time == 1),
aes(x = TimeVariable,y = 60-Variable / 10 ,fill = Type))+
geom_line(data = subset(dat.l,time == 1),
aes(x = TimeVariable,y = Variable,fill = Type))+
facet_wrap(〜Type,scales ='free')


In the graph below,

  • Is it possible to create same graph with less lines of codes? I mean, since each Figs. A-D has different label settings, I have to write settings for each Fig. which makes it longer.

The graph below is produced with the data in pdf device.
Any help with these issues is highly appreciated.(Newbie to R!). Since all the code is too long to post here, I have posted a part relevant to the problem here for Fig.C

#FigC
label1=c(0,100,200,300)
plot(data$TimeVariable2C,data$Variable2C,axes=FALSE,ylab="",xlab="",xlim=c(0,24),
     ylim=c(0,2.4),xaxs="i",yaxs="i",pch=19)
lines(data$TimeVariable3C,data$Variable3C)
axis(2,tick=T,at=seq(0.0,2.4,by=0.6),label= seq(0.0,2.4,by=0.6))
axis(1,tick=T,at=seq(0,24,by=6),label=seq(0,24,by=6))
mtext("(C)",side=1,outer=F,line=-10,adj=0.8)
minor.tick(nx=5,ny=5)

par(new=TRUE)
plot(data$TimeVariable1C,data$Variable1C,axes=FALSE,xlab="",ylab="",type="l",
     ylim=c(800,0),xaxs="i",yaxs="i")
axis(3,xlim=c(0,24),tick=TRUE,at= seq(0,24,by=6),label=seq(0,24,by=6),col.axis="violetred4",col="violetred4")
axis(4,tick=TRUE,at= label1,label=label1,col.axis="violetred4",col="violetred4")
polygon(data$TimeVariable1C,data$Variable1C,col='violetred4',border=NA)

解决方案

You ask many questions in the same OP. I will try to answer to just one : How to simplify your code or rather how to call it once for each letter. I think it is better to put your data in the long format. For example, This will create a list of 4 elements

ll <- lapply(LETTERS[1:4],function(let){
  dat.let <- dat[,grepl(let,colnames(dat))]
  dd <- reshape(dat.let,direction ='long',
                v.names=c('TimeVariable','Variable'),
                varying=1:6)
  dd$time <- factor(dd$time)
  dd$Type <- let
  dd
}
)

ll is a list of 4 data.frame, where each one that looks like :

head(ll[[1]])
 time TimeVariable Variable id Type
1.1    1            0        0  1    A
2.1    1            0        5  2    A
3.1    1            8      110  3    A
4.1    1           16        0  4    A
5.1    1           NA       NA  5    A
6.1    1           NA       NA  6    A

Then you can use it like this for example :

library(Hmisc)

layout(matrix(1:4, 2, 2, byrow = TRUE))
lapply(ll,function(data){
  label1=c(0,100,200,300)
  Type <- unique(dat$Type)
  dat <- subset(data,time==2)
  x.mm <- max(dat$Variable,na.rm=TRUE)
  plot(dat$TimeVariable,dat$Variable,axes=FALSE,ylab="",xlab="",xlim=c(0,x.mm),
       ylim=c(0,2.4),xaxs="i",yaxs="i",pch=19)
  dat <- subset(data,time==2)
  lines(dat$TimeVariable,dat$Variable)
  axis(2,tick=T,at=seq(0.0,2.4,by=0.6),label= seq(0.0,2.4,by=0.6))
  axis(1,tick=T,at=seq(0,x.mm,by=6),label=seq(0,x.mm,by=6))
  mtext(Type,side=1,outer=F,line=-10,adj=0.8)
  minor.tick(nx=5,ny=5)
  par(new=TRUE)
  dat <- subset(data,time==1)
  plot(dat$TimeVariable,dat$Variable,axes=FALSE,xlab="",ylab="",type="l",
       ylim=c(800,0),xaxs="i",yaxs="i")
  axis(3,xlim=c(0,24),tick=TRUE,at= seq(0,24,by=6),label=seq(0,24,by=6),col.axis="violetred4",col="violetred4")
  axis(4,tick=TRUE,at= label1,label=label1,col.axis="violetred4",col="violetred4")
  polygon(dat$TimeVariable,dat$Variable,col='violetred4',border=NA)
})

Another advantage of using the long data format is to use ``ggplot2andfacet_wrap` for example .

 ## transform your data to a data.frame
 dat.l <- do.call(rbind,ll)
 library(ggplot2)
 ggplot(subset(dat.l,time !=1)) +
  geom_line(aes(x=TimeVariable,y=Variable,group=time,color=time))+
  geom_polygon(data=subset(dat.l,time ==1),
              aes(x=TimeVariable,y=60-Variable/10,fill=Type))+
  geom_line(data=subset(dat.l,time ==1),
               aes(x=TimeVariable,y=Variable,fill=Type))+
  facet_wrap(~Type,scales='free') 

这篇关于R中有多个图,每个轴的设置不同,代码行少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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