r - 用 ggplot 和 2 个过滤器叠加 2 个条形图
问题描述
我有一个数据框,其中包含按会话和条件分组的数据。我想绘制会话与#hits,按条件着色并按性别过滤。
这是我的数据样本:
library(tidyverse)
df <- tribble(
~session, ~condition, ~sex, ~Obs_target_hits_mean
, 0 , 1 , "F" , 3
, 1 , 2 , "F" , 3
, 2 , 1 , "F" , 1
, 3 , 1 , "F" , 2
, 0 , 3 , "M" , 4
, 1 , 2 , "M" , 1
, 2 , 2 , "M" , 1
, 3 , 3 , "M" , 1
, 0 , 1 , "M" , 2
, 1 , 1 , "M" , 3
)
这是我的代码,但这个数字看起来很奇怪。理想情况下,我想要类似于 MATLAB 的保持功能的图
df1 <- df %>% filter(sex=="F")
df2 <- df %>% filter(sex=="M")
ggplot() +
geom_bar(stat = "identity", data = df1, aes(x = df1$session, y =
df1$obs_target_hits_mean), fill = df1$condition) +
geom_bar(stat = "identity", data = df2, aes(x = df2$session, y =
df2$obs_target_hits_mean), color= df2$condition, alpha = 0.5)
M 和女性的数字看起来并不明显。我应该怎么办?
谢谢,特里希娜
解决方案
我不完全知道,预期的结果应该是什么,但这有帮助吗?
library(tidyverse)
df <- tribble(
~session, ~condition, ~sex, ~hits
, 0 , 1 , "F" , 3
, 1 , 2 , "F" , 3
, 2 , 1 , "F" , 1
, 3 , 1 , "F" , 2
, 0 , 3 , "M" , 4
, 1 , 2 , "M" , 1
, 2 , 2 , "M" , 1
, 3 , 3 , "M" , 1
, 0 , 1 , "M" , 2
, 1 , 1 , "M" , 3
)
df %>%
group_by(session, condition, sex) %>%
summarise(obs_target_hits_mean = mean(hits)) %>%
ggplot(aes(session, obs_target_hits_mean, fill = factor(condition))) +
geom_bar(stat = "identity", position = "dodge") +
facet_grid(. ~ sex)
推荐阅读
- u-boot - PCIEx4 的 Uboot SERDES 配置
- typescript - 打字稿联合类型坏了?(当使用对象类型 + 对象文字时)
- css - 标签对齐。一样高。Bootstrap 4 - 表单
- flutter - 对作为 Stream Provider 的列表进行排序,但不会更改实际 llist 的顺序
- postgresql-9.5 - 可以在两个“选择”语句之间进行选择
- mysql - 如何只有在改变之后才能获得价值
- java - 如果测试方法不存在,则阻止 TestNG 继续套件
- .net - 从 FTP 服务器下载名称包含特定字符串的文件
- php - 中间件中的 Laravel Auth
- javascript - 在 nodeja/nodered 中,我如何解决这个属性?