首页 文章

如何在每个数据类别中绘制多个多边形?

提问于
浏览
0

我正在使用ggplot使用一组单独的工具来绘制组中的双变量数据以及这些数据的标准椭圆 . 这些返回n = 100 x,y坐标定义每个椭圆,然后对于每个组,我想绘制大约10-25个椭圆 .

从概念上讲,如何实现这一目标?我可以使用geom_polygon轻松地绘制单个椭圆,但我很困惑如何组织数据以使其工作,因此绘制了多个椭圆并且每组应用指南(颜色,填充,线型等) .

在传统的R绘图中,我可以继续使用for循环添加行 .

谢谢!

更新:这是一个包含100个单个椭圆坐标的CSV .

Data

假设我有三组采用椭圆拟合的双变量数据:绿色,红色,蓝色 . 对于每个组,我想绘制几个椭圆 .

我不知道如何以这样的方式组织数据以ggplot提供的长格式工作并保留组关联 . 列表是否有效?

UPDATE2:

这是一个原始x和y数据的csv,分为两组:河流和湖泊

Data

数据图如下:

test.data <- read.csv("ellipse_test_data.csv")
ggplot(test.data) +
  geom_point(aes(x, y, color = group)) +
  theme_classic()

enter image description here

我正在使用一个名为SIBER的包,它将贝叶斯椭圆拟合到用于按椭圆区域等比较组的数据 . 以下输出创建一个包含元素数量的列表=数据组数,每个元素包含一个6每个拟合椭圆的xn(n =绘制数) - 前四列是矢量格式的协方差矩阵Sigma,后两个是双变量均值:

# options for running jags
parms <- list()
parms$n.iter <- 2 * 10^5   # number of iterations to run the model for
parms$n.burnin <- 1 * 10^3 # discard the first set of values
parms$n.thin <- 100     # thin the posterior by this many
parms$n.chains <- 2        # run this many chains

# define the priors
priors <- list()
priors$R <- 1 * diag(2)
priors$k <- 2
priors$tau.mu <- 1.0E-3

# fit the ellipses which uses an Inverse Wishart prior
# on the covariance matrix Sigma, and a vague normal prior on the 
# means. Fitting is via the JAGS method.
ellipses.test <- siberMVN(siber.test, parms, priors)

列表中第一个元素的前几行:

$`1.river`
     Sigma2[1,1]   Sigma2[2,1]   Sigma2[1,2] Sigma2[2,2]     mu[1]    mu[2]
[1,]   1.2882740  2.407070e-01  2.407070e-01    1.922637 -15.52846 12.14774
[2,]   1.0677979 -3.997169e-02 -3.997169e-02    2.448872 -15.49182 12.37709
[3,]   1.1440816  7.257331e-01  7.257331e-01    4.040416 -15.30151 12.14947

我希望能够提取这些椭圆的随机数,并使用alpha透明度使用ggplot绘制它们 .

包SIBER有一个函数(addEllipse)将'6 x n'条目转换为定义椭圆的设定数量的x和y点,但我不知道如何组织ggplot的输出 . 我认为可能有一种优雅的方式来处理ggplot内部的所有内容 .

理想的输出将是这样的,但在ggplot中,省略号可以匹配数据级别的美学:
enter image description here

1 回答

  • 1

    一些代码在SIBRE的捆绑演示数据集上执行此操作 .

    在这个例子中,我们尝试使用ggplot2创建后椭圆的多个样本的一些图 .

    library(SIBER)
    library(ggplot2)
    library(dplyr)
    library(ellipse)
    

    使基本SIBER模型适合与包捆绑在一起的示例数据 .

    # load in the included demonstration dataset
    data("demo.siber.data")
    #
    # create the siber object
    siber.example <- createSiberObject(demo.siber.data)
    
    # Calculate summary statistics for each group: TA, SEA and SEAc
    group.ML <- groupMetricsML(siber.example)
    
    # options for running jags
    parms <- list()
    parms$n.iter <- 2 * 10^4   # number of iterations to run the model for
    parms$n.burnin <- 1 * 10^3 # discard the first set of values
    parms$n.thin <- 10     # thin the posterior by this many
    parms$n.chains <- 2        # run this many chains
    
    # define the priors
    priors <- list()
    priors$R <- 1 * diag(2)
    priors$k <- 2
    priors$tau.mu <- 1.0E-3
    
    # fit the ellipses which uses an Inverse Wishart prior
    # on the covariance matrix Sigma, and a vague normal prior on the 
    # means. Fitting is via the JAGS method.
    ellipses.posterior <- siberMVN(siber.example, parms, priors)
    
    # The posterior estimates of the ellipses for each group can be used to
    # calculate the SEA.B for each group.
    SEA.B <- siberEllipses(ellipses.posterior)
    
    siberDensityPlot(SEA.B, xticklabels = colnames(group.ML), 
                    xlab = c("Community | Group"),
                    ylab = expression("Standard Ellipse Area " ('\u2030' ^2) ),
                    bty = "L",
                    las = 1,
                    main = "SIBER ellipses on each group"
                    )
    

    现在我们想要从这些分布中创建一些样本椭圆的图 . 我们需要为每个组创建所有省略号的data.frame对象 . 在这个例子中,我们简单地将第10个后部绘制假设它们彼此独立,但如果您愿意,可以随机抽取样本 .

    # how many of the posterior draws do you want?
    n.posts <- 10
    
    # decide how big an ellipse you want to draw
    p.ell <- 0.95
    
    # for a standard ellipse use
    # p.ell <- pchisq(1,2)
    
    
    
    
    # a list to store the results
    all_ellipses <- list()
    
    # loop over groups
    for (i in 1:length(ellipses.posterior)){
    
      # a dummy variable to build in the loop
      ell <- NULL
      post.id <- NULL
    
      for ( j in 1:n.posts){
    
        # covariance matrix
        Sigma  <- matrix(ellipses.posterior[[i]][j,1:4], 2, 2)
    
        # mean
        mu     <- ellipses.posterior[[i]][j,5:6]
    
        # ellipse points
    
        out <- ellipse::ellipse(Sigma, centre = mu , level = p.ell)
    
    
        ell <- rbind(ell, out)
        post.id <- c(post.id, rep(j, nrow(out)))
    
      }
      ell <- as.data.frame(ell)
      ell$rep <- post.id
      all_ellipses[[i]] <- ell
    }
    
    ellipse_df <- bind_rows(all_ellipses, .id = "id")
    
    
    # now we need the group and community names
    
    # extract them from the ellipses.posterior list
    group_comm_names <- names(ellipses.posterior)[as.numeric(ellipse_df$id)]
    
    # split them and conver to a matrix, NB byrow = T
    split_group_comm <- matrix(unlist(strsplit(group_comm_names, "[.]")),
                               nrow(ellipse_df), 2, byrow = TRUE)
    
    ellipse_df$community <- split_group_comm[,1]
    ellipse_df$group     <- split_group_comm[,2]
    
    ellipse_df <- dplyr::rename(ellipse_df, iso1 = x, iso2 = y)
    

    现在来创建情节 . 首先根据需要绘制所有原始数据 .

    first.plot <- ggplot(data = demo.siber.data, aes(iso1, iso2)) +
      geom_point(aes(color = factor(group):factor(community)), size = 2)+
      ylab(expression(paste(delta^{15}, "N (\u2030)")))+
      xlab(expression(paste(delta^{13}, "C (\u2030)"))) + 
      theme(text = element_text(size=15))
    print(first.plot)
    

    现在我们可以尝试按组添加顶部和刻面上的后椭圆

    second.plot <- first.plot + facet_wrap(~factor(group):factor(community))
    print(second.plot)
    
    # rename columns of ellipse_df to match the aesthetics
    
    third.plot <- second.plot + 
      geom_polygon(data = ellipse_df,
                  mapping = aes(iso1, iso2,
                                 group = rep,
                                 color = factor(group):factor(community),
                                 fill = NULL),
                   fill = NA,
                   alpha = 0.2)
    print(third.plot)
    

    Facet-wrapped plot of sample of posterior ellipses by group

相关问题