Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot 2将镶嵌面包裹与频率计数的多个堆叠条形图一起使用_R_Ggplot2_Facet Wrap - Fatal编程技术网

R ggplot 2将镶嵌面包裹与频率计数的多个堆叠条形图一起使用

R ggplot 2将镶嵌面包裹与频率计数的多个堆叠条形图一起使用,r,ggplot2,facet-wrap,R,Ggplot2,Facet Wrap,我有许多二元变量,我想根据条件表达行为发生的频率(1=存在,0=不存在)。我可以使用以下语法成功地为各个变量绘制此图: require(tidyverse) require(ggplot2) require(ggsignif) require(ggpubr) condition <- c("a", "a", "a", "b", "b", "b", "c&qu

我有许多二元变量,我想根据条件表达行为发生的频率(1=存在,0=不存在)。我可以使用以下语法成功地为各个变量绘制此图:

require(tidyverse)
require(ggplot2)
require(ggsignif)
require(ggpubr)


condition <- c("a", "a", "a", "b", "b", "b", "c", "c", "c", "c")
binary_1 <- c(0,0,0,0,0,1,1,1,1,1)
binary_2 <- c(1,1,1,1,1,1,0,0,0,0)
binary_3 <- c(0,1,1,1,1,1,1,1,0,0)
binary_4 <- c(1,1,1,0,0,0,0,0,0,0)


df <- data.frame(condition, binary_1, binary_2, binary_3, binary_4)
df

gg_df <- df %>%
  mutate(binary_1 = as.factor(binary_1), binary_2 = as.factor(binary_2), binary_3 = as.factor(binary_3), binary_4 = as.factor(binary_4))

gg_melt <- melt(gg_df)

# example for one of the variables (binary_1), I just swap the variable out for each graph
gg_1 <- ggplot(gg_melt, aes(x=condition, fill = binary_1)) +
  geom_bar(stat="count") +
  scale_fill_manual(values = c("#FDAE61", "#9E0142"), name = "Behaviour Observed", labels = c("0" = "Absent", "1" = "Present")) +
  scale_x_discrete(labels = c(a = "Condition A", b = "Condition B", c = "Condition C")) + 
  xlab("Condition") + 
  ylab("Number of Participants") +
  theme(aspect.ratio = 1)

如何创建一个包含计数数据和变量的数据帧,以便能够正确使用镶嵌面包裹?

这可以通过重塑数据来实现,使四个二进制变量成为一个变量的类别。为此,我使用了
tidyr::pivot\u longer
而不是
reforme2::melt
。重塑后,您可以通过新变量
facet\u wrap

library(ggplot2)
library(tidyr)
library(dplyr)
gg_df <- df %>%
   mutate(across(starts_with("binary"), as.factor))

gg_melt <- tidyr::pivot_longer(gg_df, -condition, names_to = "binary")

ggplot(gg_melt, aes(x=condition, fill = value)) +
  geom_bar(stat="count") +
  scale_fill_manual(values = c("#FDAE61", "#9E0142"), name = "Behaviour Observed", labels = c("0" = "Absent", "1" = "Present")) +
  scale_x_discrete(labels = c(a = "Condition A", b = "Condition B", c = "Condition C")) + 
  xlab("Condition") + 
  ylab("Number of Participants") +
  theme(aspect.ratio = 1) +
  facet_wrap(~binary)
库(ggplot2)
图书馆(tidyr)
图书馆(dplyr)
gg_df%
变异(跨越(以(“二进制”)开头,如.factor))

那么,你想根据条件分割面吗?我没有完全理解这个问题。非常感谢!作为后续问题,您是否知道如何加宽图形或增加条形的宽度,以使y轴上的标签不重叠?嗯。通常,图形的宽度不是通过ggplot设置的,而是由所谓的“设备”确定的,例如,如果增加绘图窗口的大小,则绘图将调整以适应窗口,或者如果保存绘图(例如通过ggsave),则可以通过width参数设置宽度。除此之外,您可以翻转绘图,使类别位于y轴上,将图例放在图表下方,以减小绘图宽度,使用缩放x离散中的
guide=guide\u轴(n.dodge=2)
将标签放在两行上,更改标签的角度或旋转标签,…干杯!使用
pdf(“gg_binary.pdf”,width=12)gg_binary dev.off()
成功了!还有一个问题,对不起,可以重新排列刻面的顺序吗?我尝试了
gg\u melt%mutate(二进制,levels=c(“二进制4”,“二进制3”,“二进制2”,“二进制1”))
但它似乎不起作用,我猜是因为数据格式很长(:它是
mutate(二进制=因子(二进制,levels=c(“二进制4”,“二进制3”,“二进制2”,“二进制1”))
或者你可以使用
mutate(binary=forcats::fct_rev(binary))
以反转级别顺序。
library(ggplot2)
library(tidyr)
library(dplyr)
gg_df <- df %>%
   mutate(across(starts_with("binary"), as.factor))

gg_melt <- tidyr::pivot_longer(gg_df, -condition, names_to = "binary")

ggplot(gg_melt, aes(x=condition, fill = value)) +
  geom_bar(stat="count") +
  scale_fill_manual(values = c("#FDAE61", "#9E0142"), name = "Behaviour Observed", labels = c("0" = "Absent", "1" = "Present")) +
  scale_x_discrete(labels = c(a = "Condition A", b = "Condition B", c = "Condition C")) + 
  xlab("Condition") + 
  ylab("Number of Participants") +
  theme(aspect.ratio = 1) +
  facet_wrap(~binary)