修剪离散变量的 ggplot 修改的雨云图中的空白空间。

huangapple go评论87阅读模式
英文:

Trim white space in ggplot modified raincloud plot with discrete variables

问题

I'm having difficulty trimming white space from the discrete axis of a ggplot. See reprex below - how can I remove the white space between the data for "large" and the x-axis? I've tried various versions of scale_x_discrete but it doesn't seem to have any effect.

# Prepare data
sample_data <- iris %>%
    filter(Species == "setosa" | Species == "virginica") %>%
    mutate(category = case_when(Sepal.Length > 5 ~ "large",
                                Sepal.Length <= 5 ~ "small")) %>%
    select(Sepal.Width,
           Species,
           category)

# Figure
ggplot(data = sample_data) +
    aes(x = category,
        y = Sepal.Width,
        fill = category) +
    ggdist::stat_halfeye(
        adjust = 0.5,
        justification = -.1,
        .width = 0,
        point_colour = NA
    ) +
    geom_boxplot(width = .05,
                 outlier.colour = NA,
                 alpha = 0.5) +
    ggdist::stat_dots(
        side = "left",
        justification = 1.1,
        binwidth = 0.015) +
    facet_grid(~ Species) +
    labs(title = "Figure 1: Change in sepal width with sepal length",
         x = "Sepal length") +
    coord_flip() +
    tidyquant::scale_fill_tq() +
    tidyquant::theme_tq()

修剪离散变量的 ggplot 修改的雨云图中的空白空间。

英文:

I'm having difficulty trimming white space from the discrete axis of a ggplot. See reprex below - how can I remove the white space between the data for "large" and the x-axis? I've tried various versions of scale_x_discrete but it doesn't seem to have any effect.

# Prepare data
sample_data &lt;- iris %&gt;%
    filter(Species == &quot;setosa&quot; | Species == &quot;virginica&quot;) %&gt;%
    mutate(category = case_when(Sepal.Length &gt; 5 ~ &quot;large&quot;,
                                Sepal.Length &lt;= 5 ~ &quot;small&quot;)) %&gt;%
    select(Sepal.Width,
           Species,
           category)
    
# Figure
ggplot(data = sample_data) +
    aes(x = category,
        y = Sepal.Width,
        fill = category) +
    ggdist::stat_halfeye(
        adjust = 0.5,
        justification = -.1,
        .width = 0,
        point_colour = NA
    ) +
    geom_boxplot(width = .05,
                 outlier.colour = NA,
                 alpha = 0.5) +
    ggdist::stat_dots(
        side = &quot;left&quot;,
        justification = 1.1,
        binwidth = 0.015) +
    facet_grid(~ Species) +
    labs(title = &quot;Figure 1: Change in sepal width with sepal length&quot;,
         x = &quot;Sepal length&quot;) +
    coord_flip() +
    tidyquant::scale_fill_tq() +
    tidyquant::theme_tq()

修剪离散变量的 ggplot 修改的雨云图中的空白空间。

答案1

得分: 1

一个修整空白的选项是通过coord_flipxlim参数来设置x轴的限制。但请注意,设置限制的下界需要一些调整,这取决于绘图的大小或高度。

英文:

One option to trim the whitespace would be to set the limits for the x scale via the xlim argument of coord_flip. Note however that setting the lower bound for the limits will require some fiddling and depends on the plot size or height.

library(ggplot2)
library(ggdist)
library(tidyquant)

ggplot(data = sample_data) +
  aes(
    x = category,
    y = Sepal.Width,
    fill = category
  ) +
  ggdist::stat_halfeye(
    adjust = 0.5,
    justification = -.1,
    .width = 0,
    point_colour = NA
  ) +
  geom_boxplot(
    width = .05,
    outlier.colour = NA,
    alpha = 0.5
  ) +
  ggdist::stat_dots(
    side = &quot;left&quot;,
    justification = 1.1,
    binwidth = 0.015
  ) +
  facet_grid(~Species) +
  labs(
    title = &quot;Figure 1: Change in sepal width with sepal length&quot;,
    x = &quot;Sepal length&quot;
  ) +
  tidyquant::scale_fill_tq() +
  tidyquant::theme_tq() +
  coord_flip(xlim = c(1.25, NA))

修剪离散变量的 ggplot 修改的雨云图中的空白空间。

huangapple
  • 本文由 发表于 2023年6月6日 12:47:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定