我正在手动组合具有不同y轴的框图,并试图使它们看起来更漂亮.在这个例子中,我使用的是mtars df,所以情节是荒谬的,但它是一个最小限度可重现的例子.

使用以下代码:

ggplot() +
  geom_boxplot(data=mtcars, aes(y=hp),
               position = position_nudge(x=-0.2),
               width=.3, alpha=.6) +
  geom_boxplot(data=mtcars, aes(y=disp),
               position = position_nudge(x=0.2),
               width=.3, alpha=.6)

我得到了:

enter image description here

这是理所当然的.但一旦我添加了Fill,奇怪的事情就开始发生了:

ggplot() +
  geom_boxplot(data=mtcars, aes(y=hp, fill="green"),
               position = position_nudge(x=-0.2),
               width=.3, alpha=.6) +
  geom_boxplot(data=mtcars, aes(y=disp, fill="red"),
               position = position_nudge(x=0.2),
               width=.3, alpha=.6)

这里,左边的框图应该是绿色的,右边的应该是红色的,但是:

enter image description here

如果我再次改变 colored颜色 :

ggplot() +
  geom_boxplot(data=mtcars, aes(y=hp, fill="blue"),
               position = position_nudge(x=-0.2),
               width=.3, alpha=.6) +
  geom_boxplot(data=mtcars, aes(y=disp, fill="blue"),
               position = position_nudge(x=0.2),
               width=.3, alpha=.6)

enter image description here

除了红色和蓝色之外,它似乎不能让我使用其他任何东西.但即使我只使用这些 colored颜色 ,它也会奇怪地赋予它们.它不会抛出任何错误或警告.我有一个有四个框图的版本,它允许我使用紫色和绿色以及红色和蓝色,但它做了同样奇怪的 colored颜色 分配.使用十六进制代码无济于事.有人知道发生了什么事吗?

推荐答案

fill=(和其他美学)被定义为aes(..)中的outside时,它们被逐字使用,即,您提供的 colored颜色 就是所使用的 colored颜色 .然而,当fill=左右被定义为aes(..)中的inside时,它们被视为变量,就好像帧中有一列具有该不变的值.如果我们从使用 colored颜色 词改为使用其他词,这一点可能会更清楚:

ggplot() +
  geom_boxplot(data=mtcars, aes(y=hp, fill="HP Data"),
               position = position_nudge(x=-0.2),
               width=.3, alpha=.6) +
  geom_boxplot(data=mtcars, aes(y=disp, fill="DISP Data"),
               position = position_nudge(x=0.2),
               width=.3, alpha=.6)

enter image description here

要手动将特定 colored颜色 指定给特定级别(即,"HP Data"是一个级别),请使用scale_*_manual(values=..),并在其中提供命名向量.

ggplot() +
  geom_boxplot(data=mtcars, aes(y=hp, fill="HP Data"),
               position = position_nudge(x=-0.2),
               width=.3, alpha=.6) +
  geom_boxplot(data=mtcars, aes(y=disp, fill="DISP Data"),
               position = position_nudge(x=0.2),
               width=.3, alpha=.6) +
  scale_fill_manual(values = c("HP Data"="green", "DISP Data"="red"))

enter image description here

在这种情况下(不确定您的数据在其他地方是如何组织的),通常更好的做法是将数据从宽格式reshape /透视为长格式.在这种情况下,您只需要使用geom_boxplot once,如果您有更多的级别(并且必须为每一列键入geom_boxplot(..)),这会特别有利.

我将使用dplyr(对于select)和tidyr::pivot_longer进行透视,不过如果您愿意的话,在tidyVerse之外还有其他方法.

library(dplyr)
library(tidyr) # pivot_longer
select(mtcars, hp, disp) |>
  pivot_longer(cols = everything()) |>
  mutate(name = relevel(factor(name), "hp")) |>
  ggplot() +
  geom_boxplot(aes(y=value, fill=name),
               width=.3, alpha=.6) +
  scale_fill_manual(values = c("hp"="green", "disp"="red"))

enter image description here

请注意更改:

  • pivot_longer(如果您查看中间结果)现在包含两个变量,name(有"hp""disp")和value(所有数字);
  • 一个geom_boxplot,没有position轻推
  • 我将命名的向量更改为使用原始名称"hp""disp",但如果需要,您当然可以在原始数据中重命名它们
  • 我使用relevel只是为了获得相同的变量顺序:您可以通过手动控制position来控制哪些变量在左侧,但如果没有这样的顺序,ggplot将把它们放在数据的顺序中,要么是字母(字符串),要么是factor(和relevel)定义的顺序.

有关旋转/reshape 的更多信息,请访问:

R相关问答推荐

如何在四进制仪表板值框中显示值(使用shiny 的服务器计算)

MCMC和零事件二元逻辑回归

向gggplot 2中的数据和轴标签添加大写和星号

如何按排序顺序打印一个框架中所有精确的唯一值?

矩阵%*%矩阵中的错误:需要数字/复杂矩阵/向量参数

如何删除gggvenn与gggplot绘制的空白?

计算具有奇数日期的运行金额

在RStudio中堆叠条形图和折线图

为什么我使用geom_density的绘图不能到达x轴?

从非重叠(非滚动)周期中的最新数据向后开窗并在周期内计数

减go R中列表的所有唯一元素对

观察器中的inaliateLater的位置

我如何使用tidyselect来传递一个符号数组,比如Pivot_Long?

R -基线图-图形周围的阴影区域

带RStatix的Wilcoxon环内检验

有没有办法通过str_Detect()或其他字符串匹配函数来连接两个长度不等的数据帧?

如何获取R chromote中的当前URL?

条形图中的条形图没有try 赋予它们的 colored颜色

如何将数据框压缩为更宽,同时将行输入保持为行输入,而不是R中的列名?

以列名的字符向量作为参数按行应用自定义函数