[英]Add bar plot in a bar plot (ggplot2)
I would like create a plot like this: ie i would like to add another bar plot inside in my bar plot basic, whould could i do something like this ?我想创建一个这样的图:即我想在我的条形图基本中添加另一个条形图,我可以做这样的事情吗?
i have no idea about how create this,我不知道如何创建这个,
data:数据:
structure(list(Impacted_sector = structure(c(3L, 3L, 1L, 1L,
5L, 2L, 4L), .Label = c("Authorities-Stakeholders", "Public and social welfare",
"Agriculture", "Variety", "Environment"), class = "factor", scores = structure(c(Agriculture = -4.49129192,
`Authorities-Stakeholders` = -3125.027684115, Environment = -0.33176146,
`Public and social welfare` = -15.46511976, Variety = -0.39712811
), .Dim = 5L, .Dimnames = list(c("Agriculture", "Authorities-Stakeholders",
"Environment", "Public and social welfare", "Variety")))), Type_of_cost_merged = structure(c(2L,
1L, 1L, 3L, 1L, 2L, 1L), .Label = c("Management", "Damage", "Mixed"
), class = "factor", scores = structure(c(Damage = -7.803309445,
Management = -1564.7958562425, Mixed = -0.44191754), .Dim = 3L, .Dimnames = list(
c("Damage", "Management", "Mixed")))), cost = c(141499.13,
8841084.71, 6249613450.69, 441917.54, 331761.46, 15465119.76,
397128.11), Million = c(0.14149913, 8.84108471, 6249.61345069,
0.44191754, 0.33176146, 15.46511976, 0.39712811)), row.names = c(NA,
-7L), groups = structure(list(Impacted_sector = structure(1:5, .Label = c("Authorities-Stakeholders",
"Public and social welfare", "Agriculture", "Variety", "Environment"
), scores = structure(c(Agriculture = -4.49129192, `Authorities-Stakeholders` = -3125.027684115,
Environment = -0.33176146, `Public and social welfare` = -15.46511976,
Variety = -0.39712811), .Dim = 5L, .Dimnames = list(c("Agriculture",
"Authorities-Stakeholders", "Environment", "Public and social welfare",
"Variety"))), class = "factor"), .rows = structure(list(3:4,
6L, 1:2, 7L, 5L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
All help is appreciate感谢所有帮助
As I mentioned in my comment one option would be to make use of patchwork::inset_element
.正如我在评论中提到的,一种选择是使用
patchwork::inset_element
。 As in your data the cost for Authorities-Stakeholders
differs heavily from the cost of the other categories I opted for adding Authorities-Stakeholders
as an inset plot.在您的数据中,
Authorities-Stakeholders
的成本与我选择添加Authorities-Stakeholders
作为插图的其他类别的成本有很大不同。
library(ggplot2)
library(patchwork)
library(dplyr)
p1 <- dd %>%
filter(!Impacted_sector == "Authorities-Stakeholders") %>%
ggplot(aes(Impacted_sector, cost, fill = Type_of_cost_merged)) +
geom_col() +
scale_y_continuous(labels = scales::number_format(scale = 1e-6), expand = c(0, .05)) +
theme_minimal() +
guides(fill = guide_legend(override.aes = list(color = "black"))) +
labs(fill = "Type of cost")
p2 <- dd %>%
filter(Impacted_sector == "Authorities-Stakeholders") %>%
ggplot(aes(Impacted_sector, cost, fill = Type_of_cost_merged)) +
geom_col() +
scale_y_continuous(labels = scales::number_format(scale = 1e-6), expand = c(0, .05)) +
theme_minimal() +
theme(plot.background = element_rect(fill = "white", color = NA)) +
guides(fill = "none") +
labs(x = NULL, y = NULL)
p1 + patchwork::inset_element(p2, .6, .6, 1, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.