resp_migration_status
ggplot(expected_data, aes(x = resp_migration_status, y = percentage, fill = kmcluster)) +
geom_bar(stat = "identity", position = "dodge") + # Use stat = "identity" for pre-computed values
labs(
title = "Percentage distribution of network cluster by migration status",
x = "Migration Status",
y = "Percentage",
fill = "Cluster"
) +
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Format y-axis as percentages
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1)
)
kmclusterRESP_MIGRATION_STATUS我如何在原始图上覆盖一个非常基本的(黑色概述)条形图? 我有此代码:ggplot(expected_data, aes(x = resp_migration_status, y = expected, fill = kmcluster)) +
geom_bar(stat = "identity", position = "dodge", color = "black", fill = NA) + # Use stat = "identity" for pre-computed values, bars with black outlines
labs(
title = "Expected percentage distribution of network cluster by migration status",
x = "Migration Status",
y = "Percentage"
) +
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Format y-axis as percentages
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1)
)
但在geom_bar中添加填充= na覆盖了AES中的填充=群集,因此它不再将数据划分为群集类型,并且可以使其变成一些奇怪的堆积栏(请参阅图像)。
预期 | |||
---|---|---|---|
1 | 21.9 | ||
2非移民 | |||
3 | 24.7 | 19.9 | |
4非移民 | |||
1 | 41.9 | 30.5 | |
19.4 | 19.9 | 8移民 | |
1 | |||
30.5 | 10流离失所 | ||
3 | 11.9 | ||
12流离失所 | |||
我想构建一个条形图,该栏图显示了 | 以及 | 。 我已经成功地使用此代码完成了此操作: | |
在此条形图上进行了延误,我想做另一个带有黑色轮廓的图形,该图显示了 | 和 | 的the the the the。 本质上,这是卡方检验的图形表示:与“实际”分布相比,与某些迁移类型在一个集群中不成比例的“实际”分布相比,了解群集的分布是什么。 |
所以第一个问题是: 我如何将数据除以迁移类型和群集,而不在每个栏中着色,而只是用黑色概述它们?
第二:
我如何将此条形图叠加在原始图表上?
要在必须在group
library(ggplot2)
ggplot(expected_data, aes(
x = resp_migration_status,
y = percentage, fill = factor(kmcluster)
)) +
geom_col(position = "dodge") +
geom_col(aes(y = expected, group = factor(kmcluster)),
color = "black", fill = NA, position = "dodge"
) +
labs(
title = "Percentage distribution of network cluster by migration status",
x = "Migration Status",
y = "Percentage",
fill = "Cluster"
) +
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Format y-axis as percentages
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1)
)
expected_data <- data.frame(
resp_migration_status = c(
"Non-migrant", "Non-migrant", "Non-migrant", "Non-migrant",
"Migrant", "Migrant", "Migrant", "Migrant",
"Displaced", "Displaced", "Displaced", "Displaced"
),
kmcluster = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L),
percentage = c(
21.9, 30.1, 24.7,
23.3, 41.9, 22.6, 19.4, 16.1, 36.9, 26.2, 11.9, 25
),
expected = c(
30.5, 27.4, 19.9,
22.3, 30.5, 27.4, 19.9, 22.3, 30.5, 27.4, 19.9, 22.3
)
)