ggplot 水平方向堆积条形图。Y审美有什么影响?

Len*_*enn 2 r ggplot2 tidyverse

所以我有这个数据:

\n
structure(list(names = structure(1:4, .Label = c("v1", "v2", \n"v3", "v4"), class = "factor"), count = c(55, 13, 2, 2), share = c(0.76, \n0.18, 0.03, 0.03), label = c("76 %", "18 %", NA, NA), color = c("#df91a3", \n"#A5AA99", "#A5AA99", "#A5AA99")), row.names = c("v1", "v2", \n"v3", "v4"), class = "data.frame")\n
Run Code Online (Sandbox Code Playgroud)\n

它看起来像这样:

\n
   names count share label   color\nv1    v1    55  0.76  76 % #df91a3\nv2    v2    13  0.18  18 % #A5AA99\nv3    v3     2  0.03  <NA> #A5AA99\nv4    v4     2  0.03  <NA> #A5AA99\n
Run Code Online (Sandbox Code Playgroud)\n

现在我想用 ggplot 创建一个堆积条形图。names从左到右堆叠组(从列)。

\n

所以我最初做了以下事情:

\n
\npl_not_works = ggplot(df) +\n  geom_col(\n    aes(x = share,\n        y = 1,\n        group = names),\n    color = "black",\n    fill = df$color,\n    position = ggplot2::position_fill()\n  ) \n
Run Code Online (Sandbox Code Playgroud)\n

这不起作用并产生以下情节:

\n

在此输入图像描述

\n

我确实将 y 美学设置为 1,因为我认为所有条形都可以以 1(或任何其他 y 值)为中心,并且这不会改变。通过谷歌搜索并记住一些旧的东西,我变成y=1y = "a"这样:

\n
pl_works = ggplot(df) +\n  geom_col(\n    aes(x = share,\n        y = "a",\n        group = names),\n    color = "black",\n    fill = df$color,\n    position = ggplot2::position_fill()\n  ) \n
Run Code Online (Sandbox Code Playgroud)\n

其按预期工作并产生此图:

\n

在此输入图像描述

\n

然后我使用这两个图并将它们放入函数中layer_data(),这是两者的输出(顶部:不起作用,底部:起作用)

\n
> ld_not_works\n# A tibble: 4 \xc3\x97 15\n      x     y group PANEL flipped_aes  ymin  ymax    xmin   xmax colour fill     size linetype alpha works\n  <dbl> <dbl> <int> <fct> <lgl>       <dbl> <dbl>   <dbl>  <dbl> <chr>  <chr>   <dbl>    <dbl> <lgl> <chr>\n1  0.76   1       1 1     FALSE         0     1    0.692  0.828  black  #df91a3   0.5        1 NA    n    \n2  0.18   1       2 1     FALSE         0     1    0.112  0.248  black  #A5AA99   0.5        1 NA    n    \n3  0.03   1       3 1     FALSE         0.5   1   -0.0375 0.0975 black  #A5AA99   0.5        1 NA    n    \n4  0.03   0.5     4 1     FALSE         0     0.5 -0.0375 0.0975 black  #A5AA99   0.5        1 NA    n    \n> ld_works\n# A tibble: 4 \xc3\x97 15\n      x y          group PANEL flipped_aes  xmin  xmax ymin       ymax       colour fill    size linetype alpha works\n  <dbl> <mppd_dsc> <int> <fct> <lgl>       <dbl> <dbl> <mppd_dsc> <mppd_dsc> <chr>  <chr>  <dbl>    <dbl> <lgl> <chr>\n1  1    1              1 1     TRUE         0.24  1    0.55       1.45       black  #df91\xe2\x80\xa6   0.5        1 NA    y    \n2  0.24 1              2 1     TRUE         0.06  0.24 0.55       1.45       black  #A5AA\xe2\x80\xa6   0.5        1 NA    y    \n3  0.06 1              3 1     TRUE         0.03  0.06 0.55       1.45       black  #A5AA\xe2\x80\xa6   0.5        1 NA    y    \n4  0.03 1              4 1     TRUE         0     0.03 0.55       1.45       black  #A5AA\xe2\x80\xa6   0.5        1 NA    y    \n
Run Code Online (Sandbox Code Playgroud)\n

对于工作轴,即我将 y 美学设置为字符向量的轴,轴似乎被翻转,尽管我没有告诉它这样做。

\n

所以我的问题是这里发生了什么,为什么我将数字或字符向量放入 y 美学很重要,以及创建水平堆叠条形图的最佳方法是什么?

\n

All*_*ron 7

y = "a"起作用但y = 1不起作用的原因是“a”被解释为一个因子,而1被解释为一个数字。这很重要,因为geom_col尝试从数据类型猜测方向。帮助文件的方向部分对此进行了详细说明:

该几何体以不同的方式对待每个轴,因此可以有两个方向。通常,方向很容易从给定映射和使用的位置比例类型的组合中推断出来。因此,ggplot2 默认情况下会尝试猜测图层应该具有哪个方向

如果你想覆盖猜测,你可以使用orientation参数:

ggplot(df) +
  geom_col(
    aes(x = share,
        y = 1,
        group = names),
    color = "black",
    fill = df$color,
    position = ggplot2::position_fill(),
    orientation = "y"
  ) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述