尽管标准化了 y 轴(Siber r 包),Geom_bar 在极坐标图中仍缺少值

问题描述 投票:0回答:1

我正在修改本教程以生成极向量的ggplot。在我自己的数据中,并且在教程中没有出现,我想用测试组来分面我的图(

hist.by.groups_deg$Testnames
)。当我用教程中预期的变量对图进行分面时 (
comparison
),每个分面只有一个极向量,并且 y 轴似乎已正确缩放。然而,当我切换到
Testnames
并尝试在同一面上绘制多个向量时,我收到以下错误:

警告信息: 删除了 6 行包含缺失值的行 (

geom_bar()
)。

教程代码通过将这些计数除以直方图中的最高计数来标准化 y 轴直方图计数,这样 y 轴的数据范围应该是 0 到 1,当我检查时似乎就是这种情况绘图之前的数据框。然而,在收到警告消息后,我增加了 y 轴上的最大值,有几个突然超过 1。有人可以帮助我正确标准化我的 y 轴,以便我可以在同一方面比较我的组吗?

rm(list=ls())
graphics.off()

set.seed(1)

library(SIBER)
library(ggplot2)
library(magrittr) # to enable piping
library(dplyr)

# load in the included demonstration dataset
data("demo.siber.data")

# create the siber object
siber.example <- createSiberObject(demo.siber.data)

# 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

parms$save.output = FALSE
parms$save.dir = tempdir()

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

# extract the centroids from the fitted model object
centroids <- siberCentroids(ellipses.posterior)

# calculate pairwise polar vectors among all groups
angles_distances <- allCentroidVectors(centroids, do.plot = FALSE)

my.hist <- function(df){
  test <- hist(df$angles, 
               breaks = seq(from = -pi, to = pi, length = 60), 
               plot = FALSE)
  
  X <- data.frame(counts = test$counts, mids = test$mids, dens = test$density, 
                  counts.stdzd = test$counts / max(test$counts))
  
  return(X)
  
}

# calculate the points for each group's ellipse
hist.by.groups <- angles_distances %>% group_by(comparison) %>%
  do(my.hist(.))

### HERE IS WHERE I START MODIFYING THINGS ###

# Radians stress me out so I'm going to try converting them to degrees
hist.by.groups_deg <- hist.by.groups %>% 
  mutate(Degrees = mids*(180/pi))

# Creating test names to facet by
hist.by.groups_deg$TestName = c(rep("A", 125),
                                rep("B", 125),
                                rep("C", 635))
                                

# Generating polar histograms
cols <- c("#0570b0", "#8c96c6", "#74a9cf", "#8856a7", "#d7b5d8", "#b3cde3", "#810f7c", "#66c2a4", "#bdc9e1", "#df65b0","salmon","pink","gray","beige","tan")


all.roses <- ggplot(data = hist.by.groups_deg, aes(x = Degrees, y = counts.stdzd, color = comparison, fill = comparison)) +
  # Default here is stat = "count", but we are providing the y values so we override with stat = "identity"
  geom_hline(yintercept = seq(0, 1, by = 0.25), colour = "grey90", linewidth = 0.2) +
  geom_vline(xintercept = seq(0, 180, by = 45), colour = "grey90", linewidth = 0.2) +
  geom_vline(xintercept = seq(0, -180, by = -45), colour = "grey90", linewidth = 0.2) +
  geom_bar(stat = "identity", alpha = .7) + 
  scale_x_continuous(
    limits = c(-180,180),
    breaks = c(-180, -90, 0, 90, 180),
    labels = c("","-90","0","90", "")) +
  scale_y_continuous(
    limits = c(0,1)) +
  # Even though we've changed our units to degrees, start argument is in radians
  coord_polar(start = pi/2, direction = -1) +
  facet_wrap( ~ TestName, nrow = 2) +
  scale_fill_manual(
    values = cols) +
  scale_color_manual(
    values = cols) +
  theme(axis.ticks.y = element_blank(), 
        axis.text.y = element_blank(),
        legend.position = "none",
        panel.border = element_blank(),
        panel.grid = element_blank()) + 
  labs(y = "Counts (standardized by the proportion of total values)",
       x = expression("Angle between ellipse centroids (\u00B0)"))
print(all.roses)
r ggplot2 facet-wrap polar-plot
1个回答
0
投票

事实证明这是

geom_bar
的问题,而不是
SIBER
coord_polar
的问题。尽管在输入数据框中标准化了我自己的 y 轴,但这被 geom_bar 中的默认
position
参数(“堆叠”)覆盖,因此当我向同一个方面添加多个条形图时,任何重叠的条形都会堆叠在彼此重叠,有时是 y 轴比例的两倍或三倍。指定position =“identity”允许条形图重叠而不堆叠:

rm(list=ls())
graphics.off()

set.seed(1)

library(SIBER)
library(ggplot2)
library(magrittr) # to enable piping
library(dplyr)

# load in the included demonstration dataset
data("demo.siber.data")

# create the siber object
siber.example <- createSiberObject(demo.siber.data)

# 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

parms$save.output = FALSE
parms$save.dir = tempdir()

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

# extract the centroids from the fitted model object
centroids <- siberCentroids(ellipses.posterior)

# calculate pairwise polar vectors among all groups
angles_distances <- allCentroidVectors(centroids, do.plot = FALSE)

my.hist <- function(df){
  test <- hist(df$angles, 
               breaks = seq(from = -pi, to = pi, length = 60), 
               plot = FALSE)
  
  X <- data.frame(counts = test$counts, mids = test$mids, dens = test$density, 
                  counts.stdzd = test$counts / max(test$counts))
  
  return(X)
  
}

# calculate the points for each group's ellipse
hist.by.groups <- angles_distances %>% group_by(comparison) %>%
  do(my.hist(.))

### HERE IS WHERE I START MODIFYING THINGS ###

# Radians stress me out so I'm going to try converting them to degrees
hist.by.groups_deg <- hist.by.groups %>% 
  mutate(Degrees = mids*(180/pi))

# Creating test names to facet by
hist.by.groups_deg$TestName = c(rep("A", 125),
                                rep("B", 125),
                                rep("C", 635))
                                

# Generating polar histograms
cols <- c("#0570b0", "#8c96c6", "#74a9cf", "#8856a7", "#d7b5d8", "#b3cde3", "#810f7c", "#66c2a4", "#bdc9e1", "#df65b0","salmon","pink","gray","beige","tan")


all.roses <- ggplot(data = hist.by.groups_deg, aes(x = Degrees, y = counts.stdzd, color = comparison, fill = comparison)) +
  # Default here is stat = "count", but we are providing the y values so we override with stat = "identity"
  geom_hline(yintercept = seq(0, 1, by = 0.25), colour = "grey90", linewidth = 0.2) +
  geom_vline(xintercept = seq(0, 180, by = 45), colour = "grey90", linewidth = 0.2) +
  geom_vline(xintercept = seq(0, -180, by = -45), colour = "grey90", linewidth = 0.2) +
  geom_bar(stat = "identity", alpha = .7, position = "identity") + 
  scale_x_continuous(
    limits = c(-180,180),
    breaks = c(-180, -90, 0, 90, 180),
    labels = c("","-90","0","90", "")) +
  scale_y_continuous(
    limits = c(0,1)) +
  # Even though we've changed our units to degrees, start argument is in radians
  coord_polar(start = pi/2, direction = -1) +
  facet_wrap( ~ TestName, nrow = 2) +
  scale_fill_manual(
    values = cols) +
  scale_color_manual(
    values = cols) +
  theme(axis.ticks.y = element_blank(), 
        axis.text.y = element_blank(),
        legend.position = "none",
        panel.border = element_blank(),
        panel.grid = element_blank()) + 
  labs(y = "Counts (standardized by the proportion of total values)",
       x = expression("Angle between ellipse centroids (\u00B0)"))
print(all.roses)
© www.soinside.com 2019 - 2024. All rights reserved.