簡體   English   中英

R中有多個圖,每個軸的設置不同,代碼行數較少

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

在下圖中,

  • 是否可以用更少的代碼行創建相同的圖形? 我的意思是,因為每個無花果。 AD有不同的標簽設置,我必須為每個圖編寫設置,這使得它更長。

下圖是使用pdf設備中的數據生成的。
對這些問題的任何幫助都非常感謝。(新手R!)。 由於所有代碼都太長而無法在此發布,因此我已在此處發布了與此問題相關的部分

來自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")
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)

你在同一個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 <- let
  dd
}
)

ll是4個data.frame的列表,其中每個看起來像:

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

然后您可以像這樣使用它,例如:

庫(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)
})

使用長數據格式的另一個好處是使用``ggplot2 and facet_wrap`。

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

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM