如何在ggplot中的彩色条形图上层上一个概述的条形图? 我的数据看起来像这样: Expect_Data resp_migration_status KMCluster 百分比 预期的 1个非移民 1 21.9 30.5 2非移民 2 30.1 27.4 3非移民 3 24.7 19.9 4非移民 ...

问题描述 投票:0回答:0

resp_migration_status

kmcluster百分点1非移民 30.5230.127.43非移民 423.322.35移民 6移民 222.627.47移民 3416.122.39流离失所 36.9226.227.411流离失所 19.942522.3kmclusterRESP_MIGRATION_STATUSggplot(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

aes上明确映射的第一个栏的顶部添加第二个栏,以仍然获得躲闪的条形图。

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) )

enter image description heredata

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 ) )

r ggplot2
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.