简体   繁体   中英

How to add plot to gap.barplot in plotrix package?

I'm the R novice and I need some help. I'm trying to add second plot to one created in gap.barplot(), but failing. I have two vectors:

y <- as.numeric(c(92, 8, 7,6,5))
z <- as.numeric(c(7.5, 5.9, 5.2, 4.5, 2.3))    

Plotting the first one is ok:

gap.barplot(y, 
                     gap = c(9, 80),
                     ytics=c(2,4,6,8,90,92),
                     col=rep("lightsalmon", 5), 
                     xaxt = "n",
                     ylab = "Frequencies, n",
                     xlab = "")    

Image of the plot

But after I'm trying to do the same with the second plot, smth goes wrong:

gap.barplot(z, 
        gap = c(9, 80),
        ytics=c(2,4,6,8,90,92),
        col=rep("green", 5), 
        xaxt = "n",
        ylab = "Frequencies, n",
        xlab = "",
        main = "Colors",
        add = T)    

I see strange inverted plot and the error message:

Inverted plot

Error in rect(xtics[bigones] - halfwidth, botgap, xtics[bigones] + halfwidth,  : 
  не могу смешивать координаты нулевой длины с ненулевыми (can not mix the coordinates of length zero with non-zero)
In addition: Warning messages:
1: In plot.window(...) : "add" -- не графический параметр (not graphics option)
2: In plot.xy(xy, type, ...) : "add" -- не графический параметр (not graphics option)
3: In title(...) : "add" -- не графический параметр (not graphics option)
4: In axis(1, at = xtics, labels = xaxlab, ...) :
  "add" -- не графический параметр (not graphics option)
5: In axis(2, at = c(ytics[littletics], ytics[bigtics] - gapsize),  :
  "add" -- не графический параметр (not graphics option)    

What am I doing wrong?

Thanks in advance!

I found this and this related posts but ran into the same trouble. Here is a solution based on the second post:

y <- as.numeric(c(92, 8, 7,6,5))
z <- as.numeric(c(7.5, 5.9, 5.2, 4.5, 2.3)) 
data=cbind(y,z);

library(plotrix)
barpos<-barplot(data,names.arg=colnames(data),
                ylim=c(0,100),beside=TRUE,col=c("darkblue","red"),axes=FALSE)
axis(2,at=c(2,4,6,8,90,92),
     labels=c(2,4,6,8,90,92))
box()
axis.break(9,80,style="gap")

I hope that helps.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM