如何使用 geom_rect (R ggplot) 绘制具有可变条形宽度的 x 轴(xmin 和 xmax)上具有负值和正值的条形?

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

我正在尝试制作一个 ggplot,该图显示 x 轴上的生物量(称为迁移生物量)和 y 轴上的深度(作为连续变量)的差异。我还希望 y 轴上标签之间的条形和间距能够指示采样层的实际深度(有 9 个宽度范围从 50 到 250 m)。

我最初尝试使用 geom_bar 绘制 ggplot 条形图,但后来我被迫将 y 值(深度)绘制为分类值。似乎不可能将两个连续变量(生物量和深度)绘制为 geom_bar 图。 geom_bar plot

所以,我现在使用 geom_rect 来制作我的条形图,它允许我指定每个条形图的 xmin、xmax、ymin 和 ymax。这非常适合将深度显示为连续变量并指定每个条形的宽度(ymin 和 ymax),但我不知道如何绘制具有负 x 值 (xmin) 和 xmax=0 的条形。

对于如何制作带有这样的正负条形图的任何帮助或其他想法,我将不胜感激? Example of a plot I am trying to make with positive and negative bars, indicative of depth sampled.

这是我正在尝试的代码:

注意:对于所有正的生物量差异,xmin=WW_x_min 为 0。对于所有负差值来说,它都是一个负权重值。 xmax= WW_x_max 是所有正的生物量差异的正权重值。对于所有负的生物量差异,其值为 0。

ggplot() + 
  scale_x_continuous(name="Wet Weight (mg m-3)", limits=c(-600, 300)) + 
  scale_y_continuous(name="Depth (m)", trans = "reverse", 
                     breaks = c(0,50,100,300,500,700,800,1000,1250,1500), 
                     labels = c("0","50","100","300","500","700","800","1000","1250","1500")) + 
  geom_rect(data=DG5B_DG5C_SFComb_Wide_Simple, 
            mapping=aes(xmin= WW_x_min, xmax= WW_x_max, ymin=Depth_max, ymax=Depth_min), 
            fill="skyblue", color="black", alpha=1) + 
  facet_wrap(~Cruise+Site)+ labs(title="Migrant Biomass") + 
  theme_bw()+ theme(plot.title = element_text(hjust = 0.5)) + 
  theme(text=element_text(size=16), #change font size of all text 
        axis.text=element_text(size=16), #change font size of axis 
        text axis.title=element_text(size=16), #change font size of axis titles 
        plot.title=element_text(size=30), #change font size of plot title 
        legend.text=element_text(size=16), #change font size of legend text 
        legend.title=element_text(size=16)) + #change font size of legend title 
  theme(strip.text.x = element_text(size = 16)) #change front size of facet wrap text

但我收到此错误:

-x 中的错误:一元运算符的参数无效

r ggplot2 geom-bar geom
1个回答
0
投票

下面是一些代码,主要复制 linked example plot in your question,提供了绘制不同宽度的正负条的示例。这种方法的主要区别是使用 2 个方面来使图的正负部分看起来不错(特别是当比例显着不同时)。您问题中的代码已经在

~Cruise+Site
上有一个方面,因此可能需要对下面的代码进行一些更改才能适应这一点。

library(ggplot2)
library(ggh4x)

df <- data.frame(
  side = c(rep(-1, 6), rep(1, 6)),
  strip = c(rep("Abundance~(fish~10^{-6}~m^{-3})", 6),
            rep('Biomass~(kg~ww~10^{-6}~m^{-3})', 6)),
  value = c(-2338, -2182, -1042, -1595, -770, -2219,
            5.51, 4.77, 5.44, 6.30, 1.84, 8.98),
  depth = c(0, 200, 750, 1500, 2300, 3300,
            0, 200, 750, 1500, 2300, 3300),
  depth_end = c(200, 750, 1500, 2300, 3000, 3500,
                200, 750, 1500, 2300, 3000, 3500)
)

scales <- list(
  `-1` = scale_x_continuous(limits = c(-3300, 0),
                            breaks = c(-3000, -2000, -1000), 
                            labels = c('3000', '2000', '1000'),
                            expand = c(0, 0), 
                            position = "top"),
  `1` = scale_x_continuous(limits = c(0, 12),
                           breaks = 1:10,
                           expand = c(0, 0), position = "top"))

ggplot(data = df) +
  geom_rect(mapping = aes(xmin = 0, xmax = value,
                          ymin = depth, ymax = depth_end,
                          fill = factor(side))) +
  geom_text(mapping = aes(x = value + ifelse(side == 1, 0.1, -50), 
                          y = (depth + depth_end)/2, 
                          hjust = 1 - (side > 0),
                          label = paste0('(', abs(value), ')'))) +
  geom_vline(xintercept = 0, linewidth = 2) +
  scale_y_continuous(breaks = c(0, 200, 750, 1500, 2300, 3000, 3500),
                     labels = c('0', '200', '750', '1500', '2300', '3000', ''),
                     expand = expansion(mult = c(0, 0), add = c(0, 100)),
                     trans = 'reverse') +
  facet_wrap(strip ~ ., scales = "free_x",
             labeller = label_parsed) +
  labs(y = 'Depth (m)', tag = 'Near bottom') +
  ggh4x::facetted_pos_scales(
    x = scales
  ) +
  scale_fill_manual(values = c('black', 'grey')) +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y= element_line(colour = "#AAAAAA", linewidth = 1,
                                         linetype = "dashed"),
        panel.background = element_blank(),
        panel.spacing = unit(0, "lines"),
        plot.tag = element_text(hjust = 0, vjust = 0, face = 'bold'),
        plot.tag.position = c(0, 0),
        axis.title.x.top = element_blank(),
        axis.ticks.y = element_blank(),
        axis.ticks.length.x = unit(10, 'pt'),
        axis.line.x = element_line(),
        strip.placement = 'outside',
        strip.background = element_blank(),
        legend.position = 'none',
        text = element_text(size = 16))

© www.soinside.com 2019 - 2024. All rights reserved.