【问题标题】:Boxplot on a continuous scale连续规模的箱线图
【发布时间】:2016-01-26 19:20:49
【问题描述】:

这样做的目的是将各种观察的箱线图放在一个尺度上,以可视化与植物大小相比数量影响(密度受损)的影响。

数据如下:

Site    Type    zscores
D19     tea     -2.094295799
D119    tea     -1.758031135
D119    tea     -0.41697259
D119    tea     -2.154827157
O15     tea     0.472853048
O15     tea     1.121169921
O15     tea     0.676107338
O15     tea     1.244862196
O15     tea     1.351503732
O15     tea     -1.918836703
O15     tea     -2.665327454
O15     tea     -0.95906288
O15     tea     1.12044707
O15     tea     0.538872924
O15     tea     -2.11275879
O15     tea     0.955244846
O15     tea     -0.841728712
O29     rose    -1.220383412
O29     rose    -2.046877257
O29     rose    -1.046225646
O29     rose    -1.894125458

在这个阶段,我绘制的唯一图表是以下箱线图:

ggplot(data = types, aes(Site, zscores, fill = Type)) +
geom_boxplot()

结果如下:

我怎样才能让不同的站点栏移动到站点内的密度受损植物

这个想法是有一个像这样的连续比例,但使用箱线图而不是简单的点和标准误差,x 轴上的密度示例,平均点和 SE:

【问题讨论】:

  • 每个站点观察到 300 株植物
  • 你有一个变量,其中包含密度受损植物的值吗?
  • 每个站点的受损植物密度等于观察次数/300,因为我已经从数据集中删除了未受损的植物

标签: r ggplot2


【解决方案1】:

我找到了一个漫长而乏味的方法。这有效,但还不是 100%。 此方法确实使用ggplot 来绘制图形

library(ggplot2)
types <- read.csv("data/stack.csv")
head(types)

##Site Type    zscores
##1  D19  tea -2.0942958
##2 D119  tea -1.7580311
##3 D119  tea -0.4169726
##4 D119  tea -2.1548272
##5  O15  tea  0.4728530
##6  O15  tea  1.1211699

sitelevels <- levels(types$Site) ##extract all the different sites
##[1] "D119" "D19"  "H42"  "L20"  "L55"  "O15"  "O29"  "O30"  "O32"  "O43"  "O44"  "O45"  "O47"  "O50" 
##[15] "O63"  "O68"  "O83"  "O85"  "W15c" "W17c" "W21b" "W27a" "W28a" "X27a" "X27b" "X29a" "X29b" "X29c"

这是计算每个单独地点的密度所必需的,因为在所有地点调查的植物数量为 300 株,types 数据集中只剩下受损植物

avedens <- c() ##Empty vector to store the site densities in
c<- 1

for(c in c(1:length(sitelevels))){  ##runs a for loop for the number of sites
  avedens[c] <- nrow(subset(types, types$Site == comps[c]))/300 ##calculate the value for each site and store it in avedens
}

use <- data.frame(site = sitelevels, density = avedens) ## create a table with the site names and corresponding densities

head(use)

##site     density
##1 D119 0.010000000
##2  D19 0.003333333
##3  H42 0.016666667
##4  L20 0.036666667
##5  L55 0.096666667
##6  O15 0.043333333

从该表中,站点密度值被分配给每个单独的观察值。

x <- 1

for(x in c(1:nrow(types))){ ##for loop to assign the plot density value to each of the separate observations
  types$dens[x] <- use$density[use$site == types$Site[x]] ##assign the right value using the site names to correlate the correct values
}

head(types)
  ##Site Type    zscores        dens
##1  D19  tea -2.0942958 0.003333333
##2 D119  tea -1.7580311 0.010000000
##3 D119  tea -0.4169726 0.010000000
##4 D119  tea -2.1548272 0.010000000
##5  O15  tea  0.4728530 0.043333333
##6  O15  tea  1.1211699 0.043333333

然后使用 X 轴上的密度 (dens) 和 Y 轴上的 zscore 和 ggplot 绘制箱线图 ggplot(data = types, aes(dens, zscores, fill = Site)) + geom_boxplot() ##绘制箱线图

Boxplots at the correct positions as a points graph

现在唯一的问题是让所有框的宽度都相同

【讨论】:

  • 会不会是因为这些站点具有相同的损坏密度?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
  • 2017-12-19
  • 2018-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多