0

我在食品数据框中有两个二进制值列,如下所示:

foods$veryHealthy:

"False" "False" "True"  "True"  "False" "False" "False" "True"  "False" "False" "True"  "False"

"True"  "False" "False" "True"  "False" "True"  "False" "False" "True"  "False" "False" "False" ...

foods$dairyFree:

"True"  "True"  "True"  "True"  "True"  "True"  "True"  "True"  "False" "True"  "True"  "False"

"True"  "False" "False" "True"  "True"  "True"  "True"  "False" "True"  "False" "True"  "True" ...

I want to get some plot like:

![

对于 x 轴而不是四个类别,我想使用二进制值。

请注意条的粗细如何根据每个类中的实例数而变化。

我尝试了以下代码,但没有得到结果:

ggplot(foods, aes(x=veryHealthy, y=dairyFree, fill=dairyFree)) + 
  geom_col(position = "fill")  

编辑:我必须使用 ggplot2 而不是任何内置或其他库。

4

1 回答 1

0

我猜你正在寻找包ggmosaichere你可以找到一些例子的vingette

# dummy data
df <- data.frame(veryHealthy = c("False","False","True","True","False","False","False","True","False","False","True","False","True","False","False","True","False","True","False","False","True","False","False","False"),
                 dairyFree = c("True","True","True","True","True","True","True","True","False","True","True","False","True","False","False","True","True","True","True","False","True","False","True","True"))

library(ggplot2)
library(ggmosaic)

# simple implementation with your dummy data
ggplot2::ggplot(data = df) +
    ggmosaic::geom_mosaic(aes(x = product(veryHealthy, dairyFree), fill = veryHealthy ))

在此处输入图像描述

编辑:

也许这对你有用(使用与以前相同的虚拟数据):

plot(table(df))

在此处输入图像描述

在这里你可以找到一些“手动”的方法

于 2021-11-27T12:19:51.510 回答