具有不同 Y 尺度的多个变量的箱线图

Ecg*_*Ecg 1 r ggplot2 boxplot

我有 4 个变量(A、B、C、D),在 3 个位置上具有相似的模式。我想绘制一个箱线图(变量作为 Y 轴上的点,位置作为 X)。但变量具有不同数量级的值。有没有办法缩放 Y 轴并将所有变量绘制在箱线图上?可能是颜色不同吧。

Location = c("Washington","Washington","Washington","Washington","Washington","Washington", "Maine","Maine","Maine","Maine","Maine", "Florida","Florida","Florida","Florida","Florida","Florida")
A = c(0.000693156,  0.000677354,    0.000727863,    0.000650822,    0.000908343,    0.001126689,    0.001316292,    0.000975274,    0.00109082, 0.001057585,    0.000927826,    0.000552769,    0.000532546,    0.000559781,    0.000771569,    0.000563436,    0.000551136)
B = c(0.001915388,  0.001936627,    0.001476521,    0.001573681,    0.002584282,    0.00738909, 0.008089839,    0.006616564,    0.00495211, 0.004515925,    0.003791596,    0.000653847,    0.000350701,    0.000559781,    0.001920087,    0.000738206,    0.001077627)
C = c(0.000138966,  0.000104745,    0.000145573,    0.000103305,    5.08255E-05,    0.000361988,    0.000264876,    0.000454172,    0.000277471,    0.000117919,    8.9214E-05, 0.000173727,    0.000108241,    8.54628E-05,    2.35593E-05,    3.1302E-05, 1.12019E-05)
D = c(0.000108829,  0.000135005,    0.000120617,    9.29746E-05,    0.000105561,    9.27596E-05,    0.000121317,    0.000131471,    0.000152503,    0.000128974,    0.000196271,    0.000142141,    0.000147208,    0.00013674, 0.000147246,    0.000185204,    0.000103058)

df = data.frame(Location, A, B, C, D)
Run Code Online (Sandbox Code Playgroud)

这就是我尝试将两个变量作为单独的图表

library(ggplot2)
a <- ggplot(df, aes(x=Location, y=A)) + 
  geom_boxplot()
a + geom_dotplot(binaxis='y', stackdir='center', dotsize=1, fill="red")

b <- ggplot(df, aes(x=Location, y=B)) + 
      geom_boxplot()
b + geom_dotplot(binaxis='y', stackdir='center', dotsize=1, fill="blue")
Run Code Online (Sandbox Code Playgroud)
  • 我可以将 1 个图表中的所有 4 个变量与缩放的 Y 轴合并吗?
  • 我可以添加仅显示“A”和“D”的图例吗?

在此输入图像描述

jdo*_*res 5

如果将数据重新调整为“长”格式,分面是一种选择。请注意,您必须设置scales = 'free'facet_wrap().

library(tidyverse)

df.long <- df %>% 
  pivot_longer(A:D, names_to = 'variable', values_to = 'value')

g <- ggplot(data = df.long, aes(x = Location, y = value)) +
  geom_boxplot() +
  facet_wrap(facets = ~variable, scales = 'free')
print(g)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果您想在一个图上获得所有内容,则必须重新调整每组的数据。在这里,我将每个数据点相对于其原始比例标准化为 0 到 1 之间。

df.long <- df %>% 
  pivot_longer(A:D, names_to = 'variable', values_to = 'value') %>% 
  group_by(variable) %>% 
  mutate(value_norm = value - min(value), 
         value_norm = value_norm / max(value_norm)
  )

g.norm <- ggplot(data = df.long, aes(x = Location, y = value_norm, fill = variable)) +
  geom_boxplot()
print(g.norm)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述