Isma Soto Almena
Isma Soto Almena

Reputation: 227

Add bar plot in a bar plot (ggplot2)

I would like create a plot like this: i.e. i would like to add another bar plot inside in my bar plot basic, whould could i do something like this ?

enter image description here

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

Upvotes: 1

Views: 257

Answers (1)

stefan
stefan

Reputation: 123903

As I mentioned in my comment one option would be to make use of 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.

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)

Upvotes: 1

Related Questions