r - facet_wrap 带有垂直第二个标签和自由刻度
问题描述
我facet_wrap
用来展示 4 个地块,每个国家 2 个地块。如何在左侧的 y 轴上垂直显示国家/地区的名称?(即,我希望第一行用于加拿大,第二行用于美国,并删除第二行图的顶部标签)
注意:我不能使用facet_grid
,因为那不允许我拥有自由秤。
ggplot( df ,aes(x=year, y = value,fill=scenario)) +
geom_bar(stat = "identity", position = "dodge",show.legend = TRUE) +
facet_wrap(~ variable + country, ncol=2, scale="free")
这是数据集:
df <- structure(list(variable = structure(c(2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("Direct emissions costs",
"Indirect costs", "Capital expenditure", "Revenue"), class = "factor"),
country = c("USA", "CAN", "USA", "CAN", "USA", "CAN", "USA",
"CAN", "USA", "CAN", "USA", "CAN", "USA", "CAN", "USA", "CAN"
), sector = c("LIVE", "LIVE", "LIVE", "LIVE", "LIVE", "LIVE",
"LIVE", "LIVE", "LIVE", "LIVE", "LIVE", "LIVE", "LIVE", "LIVE",
"LIVE", "LIVE"), year = c("2030", "2030", "2035", "2035",
"2030", "2030", "2035", "2035", "2030", "2030", "2035", "2035",
"2030", "2030", "2035", "2035"), scenario = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("2Ci", "2Cd"), class = "factor"), value = c(0.46,
1.79, 0.28, 0.68, 520.03, 137.17, 1099.74, 336.89, 0, 0,
-1, 0.08, 0, 0, 594.12, 262.43)), row.names = c(NA, -16L), groups = structure(list(
variable = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Direct emissions costs",
"Indirect costs", "Capital expenditure", "Revenue"), class = "factor"),
country = c("CAN", "CAN", "USA", "USA", "CAN", "CAN", "USA",
"USA"), year = c("2030", "2035", "2030", "2035", "2030",
"2035", "2030", "2035"), sector = c("LIVE", "LIVE", "LIVE",
"LIVE", "LIVE", "LIVE", "LIVE", "LIVE"), .rows = structure(list(
c(6L, 14L), c(8L, 16L), c(5L, 13L), c(7L, 15L), c(2L,
10L), c(4L, 12L), c(1L, 9L), c(3L, 11L)), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -8L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
解决方案
您可以使用 library(cowplot) 中的 plot_grid() 函数
canDf <- df %>%
filter(country == 'CAN')
p1 <- ggplot( canDf ,aes(x=year, y = value,fill=scenario)) +
geom_bar(stat = "identity", position = "dodge",show.legend = TRUE)+
facet_wrap(~ variable, ncol=2, scale="free") +
labs(title = canDf$country)
usDf <- df %>%
filter(country == 'USA')
p2 <- ggplot( usDf ,aes(x=year, y = value,fill=scenario)) +
geom_bar(stat = "identity", position = "dodge",show.legend = TRUE)+
facet_wrap(~ variable, ncol=2, scale="free") +
labs(title = usDf$country)
library(cowplot)
plot_grid(p1, p2, ncol = 1)
推荐阅读
- python - how to copy values of one column of a dataframe to another column of other dataframe in pandas?
- angular - 角度 10:从拦截器中的 http 请求中的 observable 订阅后,我的请求未发送
- string - 在带有换行符的文本之间搜索包含 2 次或多次出现特定单词的文件
- angular - Get image from specified path in typescript
- c# - 如何在不使用查询参数的情况下显示临时成功消息?
- java - ActiveMQ Artemis 和 5.x 监听器同时 - NullPointerException
- node.js - Nginx nodejs problems
- python - Django - 尝试通过计数器访问 for 循环中的数字
- html - 段落没有到位
- mysql - 多个 SQL 查询不适用于 DBeaver 上的分隔符