我有一个带序数数据的列联表,想在R中看到这个。我在这里找到了一个很好的解决方案https://stats.stackexchange.com/q/148564,下面有相应的图表。但是,这是用Stata编码的。
有没有办法在R中实现这一目标?
以下是链接中显示的示例中的数据:
improvement treatment baseline frequency
none 0 mild 5
moderate 0 mild 41
substantial 0 mild 4
none 1 mild 19
moderate 1 mild 19
substantial 1 mild 12
none 0 moderate 19
moderate 0 moderate 24
substantial 0 moderate 7
none 1 moderate 20
moderate 1 moderate 14
substantial 1 moderate 16
none 0 severe 7
moderate 0 severe 21
substantial 0 severe 22
none 1 severe 12
moderate 1 severe 15
substantial 1 severe 23
更新:PoGibas'用上面的例子数据解决了这个问题。但如何在没有频率列的情况下解决它?这两个变量都是因素。
Size cDNA
4 0
2 3
4 4
2 3
2 0
2 1
3 2
3 3
3 1
4 1
答案 0 :(得分:2)
您可以使用:
ggplot(df, aes(factor(treatment))) +
geom_bar(aes(y = frequency, alpha = rev(improvement)),
stat = "identity", position = "dodge",
color = "#209f1b", fill = "#35665C") +
geom_text(aes(y = -5, label = frequency), color = "red") +
facet_grid(improvement ~ baseline, switch = "both") +
labs(title = "frequency",
y = "improvement",
x = "baseline and treatment") +
theme_minimal() +
theme(strip.background = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(size = 12),
axis.ticks = element_blank(),
strip.text.y = element_text(angle = 0),
strip.text = element_text(size = 12),
legend.position = "none")
获得这样的结果:
说明:
ggplot(df)
- 将您的数据(df
)传递给ggplot
aes(factor(treatment))
- 在x轴上添加treatment
(作为因子)geom_bar
- 创建条形图,在哪里
y = frequency
- 我们在y轴上添加frequency
alpha = rev(improvement))
- 缩放由improvement
填充“shadiness”(相反,因为它从最轻到最暗)geom_text(aes(y = -5, label = frequency)
- 在(frequency
)栏下添加文字(-5
)数据(df
):
structure(list(improvement = c("none", "moderate", "substantial",
"none", "moderate", "substantial", "none", "moderate", "substantial",
"none", "moderate", "substantial", "none", "moderate", "substantial",
"none", "moderate", "substantial"), treatment = c(0L, 0L, 0L,
1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L),
baseline = c("mild", "mild", "mild", "mild", "mild", "mild",
"moderate", "moderate", "moderate", "moderate", "moderate",
"moderate", "severe", "severe", "severe", "severe", "severe",
"severe"), frequency = c(5L, 41L, 4L, 19L, 19L, 12L, 19L,
24L, 7L, 20L, 14L, 16L, 7L, 21L, 22L, 12L, 15L, 23L)), .Names = c("improvement",
"treatment", "baseline", "frequency"), row.names = c(NA, -18L
), class = "data.frame")