简体   繁体   English

R+ggplot2+facet_wrap:保留条形图

[英]R+ggplot2+facet_wrap: preserve the with of the bars

Please have a look at the reprex at the end of the post.请查看帖子末尾的 reprex。 It is a faceted barchart where the bars are placed in decreasing order in each subplot.这是一个多面条形图,其中条形图在每个子图中按降序排列。 My only question is how to ensure that the bars have the same consistent width in all the subplots.我唯一的问题是如何确保条形图在所有子图中具有相同的一致宽度。

I had a look at我看了一下

How to automatically adjust the width of each facet for facet_wrap? facet_wrap如何自动调整每个facet的宽度?

Equal bar width across facet_wrap barplots facet_wrap 条形图中的条宽相等

but I did not go very far.但我没有 go 很远。

Any suggestion is appreciated!任何建议表示赞赏!

library(tidyverse)


sec_ms_tcf_plot <- structure(list(member_state = c("BG", "EE", "EE", "HR", "HR", 
"HR", "HR", "HR", "HR", "HR", "LT", "LT", "LT", "LT", "LT", "LT", 
"LT", "LT", "MT", "MT", "MT", "PL", "PL", "PL", "PL", "PL", "PL", 
"PL", "PL", "PL", "PL", "PL", "PL", "PL", "PL", "PL", "PL", "RO", 
"RO", "RO", "RO", "RO", "RO", "RO", "RO", "RO", "RO", "RO", "RO", 
"RO", "RO", "RO", "RO", "RO", "RO", "SE", "SE", "SK", "SK", "SK", 
"SK", "SK", "SK", "SK", "SK", "SK", "SK", "SK", "SK", "SK", "SK", 
"SK", "SK", "SK", "SK"), sector = c("a_agriculture_forestry_and_fishing", 
"c_manufacturing", "a_agriculture_forestry_and_fishing", "b_mining_and_quarrying", 
"d_electricity_gas_steam_and_air_conditioning_supply", "a_agriculture_forestry_and_fishing", 
"g_wholesale_and_retail_trade_repair_of_motor_vehicles_and_motorcycles", 
"f_construction", "h_transportation_and_storage", "c_manufacturing", 
"i_accommodation_and_food_service_activities", "l_real_estate_activities", 
"h_transportation_and_storage", "e_water_supply_sewerage_waste_management_and_remediation_activities", 
"g_wholesale_and_retail_trade_repair_of_motor_vehicles_and_motorcycles", 
"d_electricity_gas_steam_and_air_conditioning_supply", "a_agriculture_forestry_and_fishing", 
"c_manufacturing", "a_agriculture_forestry_and_fishing", "h_transportation_and_storage", 
"c_manufacturing", "q_human_health_and_social_work_activities", 
"b_mining_and_quarrying", "l_real_estate_activities", "i_accommodation_and_food_service_activities", 
"s_other_service_activities", "e_water_supply_sewerage_waste_management_and_remediation_activities", 
"j_information_and_communication", "k_financial_and_insurance_activities", 
"m_professional_scientific_and_technical_activities", "h_transportation_and_storage", 
"n_administrative_and_support_service_activities", "d_electricity_gas_steam_and_air_conditioning_supply", 
"f_construction", "g_wholesale_and_retail_trade_repair_of_motor_vehicles_and_motorcycles", 
"c_manufacturing", "a_agriculture_forestry_and_fishing", "p_education", 
"k_financial_and_insurance_activities", "o_public_administration_and_defence_compulsory_social_security", 
"r_arts_entertainment_and_recreation", "s_other_service_activities", 
"b_mining_and_quarrying", "d_electricity_gas_steam_and_air_conditioning_supply", 
"q_human_health_and_social_work_activities", "e_water_supply_sewerage_waste_management_and_remediation_activities", 
"m_professional_scientific_and_technical_activities", "n_administrative_and_support_service_activities", 
"j_information_and_communication", "i_accommodation_and_food_service_activities", 
"a_agriculture_forestry_and_fishing", "h_transportation_and_storage", 
"f_construction", "c_manufacturing", "g_wholesale_and_retail_trade_repair_of_motor_vehicles_and_motorcycles", 
"a_agriculture_forestry_and_fishing", "d_electricity_gas_steam_and_air_conditioning_supply", 
"p_education", "n_administrative_and_support_service_activities", 
"s_other_service_activities", "j_information_and_communication", 
"m_professional_scientific_and_technical_activities", "o_public_administration_and_defence_compulsory_social_security", 
"f_construction", "r_arts_entertainment_and_recreation", "e_water_supply_sewerage_waste_management_and_remediation_activities", 
"b_mining_and_quarrying", "d_electricity_gas_steam_and_air_conditioning_supply", 
"h_transportation_and_storage", "q_human_health_and_social_work_activities", 
"i_accommodation_and_food_service_activities", "g_wholesale_and_retail_trade_repair_of_motor_vehicles_and_motorcycles", 
"l_real_estate_activities", "a_agriculture_forestry_and_fishing", 
"c_manufacturing"), sector_exp = c(217483, 2374, 3730, 31, 1217, 
1563, 3637, 5181, 5294, 33715, 8, 93, 271, 750, 857, 3180, 32106, 
43777, 715, 759, 14230, 129, 143, 224, 265, 383, 1016, 1569, 
1763, 2254, 2309, 2507, 5307, 15428, 38174, 38189, 558519, 416, 
673, 2194, 2726, 3381, 6841, 9425, 10731, 16835, 18968, 20845, 
23340, 43572, 170800, 175884, 205434, 272582, 892678, 149629, 
149629, 75, 466, 498, 607, 679, 918, 919, 1014, 1037, 1130, 1521, 
1635, 1639, 1837, 3715, 5275, 11874, 51306), sector_code = c("A", 
"C", "A", "B", "D", "A", "G", "F", "H", "C", "I", "L", "H", "E", 
"G", "D", "A", "C", "A", "H", "C", "Q", "B", "L", "I", "S", "E", 
"J", "K", "M", "H", "N", "D", "F", "G", "C", "A", "P", "K", "O", 
"R", "S", "B", "D", "Q", "E", "M", "N", "J", "I", "A", "H", "F", 
"C", "G", "A", "D", "P", "N", "S", "J", "M", "O", "F", "R", "E", 
"B", "D", "H", "Q", "I", "G", "L", "A", "C")), row.names = c(NA, 
-75L), class = c("tbl_df", "tbl", "data.frame"))
 


## See https://stackoverflow.com/questions/5409776/how-to-order-bars-in-faceted-ggplot2-bar-chart/5414445#5414445


dd2 <- sec_ms_tcf_plot |>
    mutate(category2=factor(paste(member_state, sector_code))) |>
    mutate(category2=reorder(category2, rank(sector_exp)))


gpl <- ggplot(dd2, aes(y=category2, x=sector_exp/1e3)) +
  geom_bar(stat = "identity") +
  facet_wrap(. ~ member_state, scales = "free_y", nrow=3) +
    scale_y_discrete(labels=dd2$sector_code, breaks=dd2$category2,
                     )+
        xlab("Sector Expenditure (Mio \u20ac)")+
    ylab("Sector NACE Code")


ggsave( "expenditure.png", gpl, width=10, height=13)



##Is there any way to have the same bar width in all the subplots?

Created on 2023-05-26 with reprex v2.0.2创建于 2023-05-26,使用reprex v2.0.2

This is not great but sort of does it.这不是很好,但可以做到。

It's tricky since ggplot bar widths are scaled in proportion to the number of categories in each facet, and you want those to vary.这很棘手,因为 ggplot 条形宽度与每个方面的类别数量成比例缩放,并且您希望它们有所不同。 One hack would be to add blank categories into each facet to augment them so that every facet has as many categories as the most crowded facet.一种 hack 是在每个方面添加空白类别以扩充它们,以便每个方面都有与最拥挤的方面一样多的类别。

pad_facets <- function(df, facet_var, cat_var) {
  df %>%
    distinct({{facet_var}}, {{cat_var}}) %>%            # keep 1 row per facet/cat
    count({{facet_var}}) %>%                            # how many cat per facet?
    transmute({{facet_var}}, cat_to_add = max(n) - n) %>% # calc # cat to add
    uncount(cat_to_add) %>%                               # copy each cat x times
    mutate({{cat_var}} := as_factor(row_number())) %>%  # add new factor level
    bind_rows(df)                                         # add on original data
}

dd2 %>%
  pad_facets(member_state, category2) %>%

ggplot(aes(y=category2, x=sector_exp/1e3)) +
  geom_bar(stat = "identity",
           position = position_dodge(preserve = "total")) +
  facet_wrap(. ~ member_state, scales = "free_y", nrow=3) +
  scale_y_discrete(labels=dd2$sector_code, breaks=dd2$category2,
  )+
  xlab("Sector Expenditure (Mio \u20ac)")+
  ylab("Sector NACE Code")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure搜索——如何实现多切面搜索? - Azure search - how to implement multiple facet search? 在 ListView 中使用 Wrap 和 Chips - Using Wrap, and Chips inside of ListView 用不同的条渲染图表 - render chart with different bars 加载到 BigQuery 时如何保留 ASCII 控制字符 - How to preserve ASCII control characters when loading into BigQuery 在 Go 中包装 API Json 响应 - Wrap API Json response in Go 添加 gt 表和 ggplot 图片到 email - Add gt table and ggplot images to email 如何防止开发数据存储在重启时自行清除 - How to preserve development datastore from clearing itself on reboot 如何用 RxJava Single<> 包装 Firebase OnCompleteListener? - How do I wrap a Firebase OnCompleteListener with a RxJava Single<>? Flutter/Firestore:如何在滚动时将项目添加到 stream(完成获取后保留滚动位置)? - Flutter/Firestore: How to add items to stream on scroll (preserve scrollposition when done fetching)? 如何使用 AWS appsync 将现有的 REST API 包装到 graphQL API 中? - How can I wrap an existing REST API into graphQL API using AWS appsync?
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM