多个并排的箱形图:改变轴的大小

时间:2016-08-14 15:14:46

标签: r plot ggplot2 boxplot

我为提出一个简单的问题道歉,但我试图找到一个解决方案,而且我对制作复杂的情节我是相当新的。我有一个数据框(下面)包含四个变量:(1)Sampling.Station; (2)蝙蝠。 (3)Light.Intensity; (4)Bat.Passes。

我的目标是产生两个由变量Bat.Species分组的图,其中包含三个水平因子:(1)Pipestrellus pygmaeus; (2)Pipestrellus pipestrellus; (3)nyctalus noctula:

剧情1:

x轴 - Sampling.Station(编号1:8); y轴 - Light.Intensity

剧情2:

x轴 - Sampling.Station(编号1:8); y轴 - Bat.Passes

我已成功从名为Format的数据框生成了第1个图(下图)。下面的数据框是使用函数 cbind 生成的,以便添加名为Bat.Passes的列,我试图从中生成绘图2;但是,我继续收到此警告消息,代码生成盒子和样子图,但不生成轴,轴标签或图例。

Warning message:
In bxp(list(stats = c(2, 2, 3.5, 5, 5, 2, 3, 4, 6, 6, 1, 6, 10.5,  :
some notches went outside hinges ('box'): maybe set notch=FALSE 

有任何建议如何做到这一点?我做了一些搜索,但我无法解决问题,我无法使用Mac上R studio中的函数windows()打开一个窗口,以增加绘图参数。任何帮助将不胜感激!

  windows(width=10, height=9)
  Error: could not find function "windows"

地块1的示例

enter image description here

剧情1的代码:

          Sampling.Station.labels=c("1","2","3","4","5","6","7","8")

         bat.labels <- rep(c("Pipistrellus pygmaeus", "Pipestrellus pipestrellus", "Nyctalus noctula"), 8)

         data_long <- gather(bats1, x, Mean.Value, Saparano.Pipestrelle:Noctule)
         head(data_long) 

         stacked.data.1<-melt(data_long, id=c('Sampling.Station', 'x'))
         head(stacked.data.1)
         str(stacked.data.1)


         stacked.data.1=stacked.data.1[, -3]
         head(stacked.data.1)
         colnames(stacked.data.1)<-c("Sampling.Station", "Bat.Species", "Light.Intensity") 
         head(stacked.data.1)

         par(mfrow = c(1,1))
         boxplots.double.1=boxplot(Light.Intensity~Sampling.Station + Bat.Species, 
                                   data = stacked.data.1, 
                                   at = c(1:24), 
                                   ylim = c(min(0, min(0)), 
                                            max(30, na.rm = T)),
                                   xaxt = "n",
                                   notch=TRUE,
                                   col = c("red", "blue", "green"),
                                   cex.axis=0.7,
                                   cex.labels=0.7,
                                   ylab="Light Intensity (Lux)", 
                                   xlab="Sampling Stations",
                                   space=1)

         axis(side = 1, at = seq(3, 24, by = 1), labels = FALSE)
         text(seq(3, 24, by=3), par("usr")[3] - 0.2, labels=unique(Sampling.Station.labels), srt = 45, pos = 1, xpd = TRUE, cex=0.8)
         par(oma = c(4, 1, 1, 1))
         par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
         plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
         legend("top", 
                legend=c("Pipistrellus pygmaeus","Pipestrellus pipestrellus","Nyctalus noctula"),
                fill=c("Blue", "Red", "Green"),
                xpd = TRUE, horiz = TRUE, 
                inset = c(0,0), 
                bty = "n", 
                col = 1:4, 
                cex = 0.8,
                title = "Bat Species",
                lty = c(1,1))

图2的代码

 par(mfrow = c(1,1))
 Sampling.Station.labels=c("1","2","3","4","5","6","7","8")

     bat.labels <- rep(c("Pipistrellus pygmaeus", "Pipestrellus pipestrellus", "Nyctalus noctula"), 8)

     data_long <- gather(bats1, x, Mean.Value, Saparano.Pipestrelle:Noctule)
         head(data_long) 
         head(bats1)

         stacked.data.2<-melt(data_long, id=c('Number.of.bat.passes', 'x'))
         head(stacked.data.2)
         str(stacked.data.2)
         stacked.data.2$x<- as.factor(stacked.data.2$x)
         stacked.data.2$Number.of.bat.passes <-as.numeric(stacked.data.2$Number.of.bat.passes)

         stacked.data.2=stacked.data.2[,-3]
         head(stacked.data.2)
         colnames(stacked.data.2)<-c("Bat.Passes", "Bat.Species", "Sampling.Station") 
         head(stacked.data.2)
         str(stacked.data.2)

     par(mfrow = c(1,1))
     boxplots.double.2=boxplot(Bat.Passes~Sampling.Station + Bat.Species, 
                               data = stacked.data.1, 
                               at = c(1:24), 
                               ylim = c(min(0, min(0)), 
                                        max(30, na.rm = T)),
                               xaxt = "n",
                               notch=TRUE,
                               col = c("red", "blue", "green"),
                               cex.axis=0.7,
                               cex.labels=0.7,
                               ylab="Light Intensity (Lux)", 
                               xlab="Sampling Stations",
                               space=1)

     axis(side = 1, at = seq(3, 24, by = 1), labels = FALSE)
     text(seq(3, 24, by=3), par("usr")[3] - 0.2, labels=unique(Sampling.Station.labels), srt = 45, pos = 1, xpd = TRUE, cex=0.8)
     par(oma = c(4, 1, 1, 1))
     par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
     plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
     legend("top", 
            legend=c("Pipistrellus pygmaeus","Pipestrellus pipestrellus","Nyctalus noctula"),
            fill=c("Blue", "Red", "Green"),
            xpd = TRUE, horiz = TRUE, 
            inset = c(0,0), 
            bty = "n", 
            col = 1:4, 
            cex = 0.8,
            title = "Bat Species",
            lty = c(1,1))

如果有人可以帮助制作轴,轴标签和图例,那么请提前多多谢谢。

enter image description here

数据帧

 bats1<-structure(list(Sampling.Station = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
      3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 
      8L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 
      7L, 7L, 7L, 8L, 8L, 8L, 1L, 1L, 1L), Light.Intensity.S = c(26.9, 
      25.16, 39, 29.81, 21.83, 20.22, 2.9, 2.1, 0.85, 0.62, 0.39, 0.26, 
      24.7, 21.99, 20.46, 26.32, 0, 0, 0.43, 0.02, 0.02, 0.03, 0.02, 
      0.03, 293.56, 167.79, 114.06, 17.22, 16.26, 4.76, 0.63, 0.56, 
      0.56, 86.63, 87.97, 88.59, 0.31, 0.04, 0.05, 0, 0.01, 0.01, 0.02, 
      2.6, 2.68, 2.62, 0.43, 0.44), Number.of.bat.passes = c(3L, 2L, 
      5L, 6L, 15L, 2L, 10L, 12L, 17L, 2L, 0L, 0L, 15L, 7L, 17L, 0L, 
      1L, 0L, 14L, 10L, 12L, 7L, 4L, 1L, 3L, 5L, 3L, 1L, 6L, 11L, 3L, 
      0L, 0L, 12L, 11L, 9L, 1L, 2L, 1L, 12L, 14L, 10L, 3L, 2L, 1L, 
      5L, 4L, 2L), Saparano.Pipestrelle = c(0.444444444, 0, 0,    0.027777778, 
      0, 0, 0.25, 0, 0.08650519, 0, 0, 0, 0.111111111, 0, 0.124567474, 
      0, 0, 0, 0.25, 0.01, 0.111111111, 0.081632653, 0, 0, 0, 0.04, 
      0, 1, 0.027777778, 0.033057851, 0.111111111, 0, 0, 0.027777778, 
      0.074380165, 0.012345679, 0, 0, 1, 0.173611111, 0.081632653, 
      0.16, 1, 0.25, 0, 0.04, 0.25, 0.25), Common.Pipestrelle = c(0.25, 
      0, 0, 7, 0, 0, 0, 0, 0.08, 0, 0, 0, 0.6, 0, 0.222222222, 0, 0, 
      0, 0.142857143, 9, 0.5, 1.25, 0, 0, 0, 4, 0, 0, 5, 2, 1, 0, 0, 
      1.25, 0.888888889, 5, 0, 0, 2, 0.28, 0.625, 0.375, 0, 1, 0, 4, 
      0.5, 1), Noctule = c(0, 0, 1, 0, 0.109375, 0, 0, 0, 0.75, 0, 
      0, 0, 0, 0.08, 0.046875, 0, 0, 0, 0, 0, 0.046875, 0, 0, 0, 0, 
      0.0625, 0, 0, 0, 0.015625, 0, 0, 0, 0.28, 0, 0.12, 0, 0, 0, 0, 
      0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Sampling.Station",        "Light.Intensity.S", 
     "Number.of.bat.passes", "Saparano.Pipestrelle",     "Common.Pipestrelle", 
      "Noctule"), row.names = c(NA, -48L), class = "data.frame")

0 个答案:

没有答案