Multiple plots in R with different settings for each axis with less lines of code

。_饼干妹妹 提交于 2019-12-01 12:32:53

问题


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) 

回答1:


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')  



来源:https://stackoverflow.com/questions/17121390/multiple-plots-in-r-with-different-settings-for-each-axis-with-less-lines-of-cod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!