1

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

3
  • 3
    Similar to this stackoverflow.com/questions/5219671/… Commented Jul 18, 2021 at 14:30
  • Please explain which bars you intend to put into the main plot and which go into the inset barplot. I suspect @rbasa's link is applicable here, though. Commented Jul 18, 2021 at 14:43
  • 1
    Another options not mentioned in the post linked by @rbasa would be to make use of patchwork::inset_element Commented Jul 18, 2021 at 15:13

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.