r - 在给定其他变量的情况下显示一个变量的存在百分比
问题描述
我目前正在处理一些情节,并且根据我目前的 ggplot2 知识,我遇到了一个我现在无法解决的问题。
我将尝试使用我在 R 中创建的虚构数据来解释我的问题。下面我将 str 命令的输出留在我的虚构数据框中:
'data.frame': 15 obs. of 4 variables:
$ x: Factor w/ 2 levels "0","1": 2 1 2 1 1 1 2 2 1 2 ...
$ y: Factor w/ 2 levels "0","1": 1 2 2 1 2 2 2 1 1 1 ...
$ w: Factor w/ 2 levels "0","1": 2 1 2 2 1 1 1 1 2 2 ...
$ z: Factor w/ 2 levels "0","1": 2 1 2 2 1 1 2 2 1 2 ...
如您所见,这些都是二分变量。让我们考虑我的因变量是 y。我想做的图是一个条形图,如下图:
我真的很想制作这样的情节。另一个像这样的,但也添加了一个 y 流行度条,将自变量(x、w 和 z)上为 1 的组与自变量(x、w 和 z)上为 0 的组进行比较。所以,在第二个想法中,它将是 6 个小节,而不是只有 3 个。但是这两个想法中的任何一个都可以很好地满足我的需要。在此先感谢社区,您总是非常乐于助人。
示例数据:
d <- structure(list(x = structure(c(2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L), .Label = c("0", "1"), class = "factor"), y = structure(c(1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L), .Label = c("0", "1"), class = "factor"), w = structure(c(2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L), .Label = c("0", "1"), class = "factor"), z = structure(c(2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor")), row.names = c(NA, -15L), class = "data.frame")
解决方案
推荐阅读
- c++ - 为什么一个工作的主函数会显示这个智能感知错误?
- zsh - 如何让 zsh 完成以匹配文件名中间忽略大小写?
- excel - sumif/countif 的 VBA 替代方案
- ios - 将无法识别的选择器作为按钮发送到集合视图单元格的实例,该按钮将数据从按钮发送到另一个视图控制器
- vb.net - 如何以编程方式在 vb.net(visual basic) 中导入 DLL 文件?
- android - 不同风格的文本 PC 和智能手机
- python - 如何在手机屏幕中为“tinymce”设置小图标大小?
- python - 当从 csv 复制到“id”列中的空值时,复制命令不更新 Id_sequence 列违反非空约束
- react-native - 在 React Native 中更改目标元素的值
- reactjs - ReactJS:无法对未安装的组件执行 React 状态更新