I am trying to make a grouped bar chart with 5 different variables attached to each country which would look something like this (just incase i got the name wrong);
Here is a snipet of my data and my current code using just 1 variable:
econ4m
Country_Name aggri_change fisheries_pdiff food_pdiff freshwater_pdiff co2_pdiff
Albania -9.52 7.38 3.00 -1.69 6.79
Algeria -5.13 -2.78 -1.69 1.17 -0.73
Bosnia -8.05 2.82 -0.13 -5.22 1.95
Croatia -2.87 5.23 0.74 1.35 2.12
Cyprus -1.29 -28.98 -77.64 -1.19 -1.28
Egypt 9.64 3.75 -2.56 -0.94 4.13
France -9.97 -2.04 -1.92 -1.10 0.87
Greece 2.63 0.60 0.58 0.65 0.13
current code
ggplot(econ4m, aes(x = Country_Name, y = aggri_change))
geom_col()
coord_flip()
I tried putting in another geom_line argument but it seems to just stack the bars
all data in dput format;
structure(list(Country_Name = c("Albania", "Algeria", "Bosnia and Herzegovina",
"Croatia", "Cyprus", "Egypt, Arab Rep.", "France", "Greece",
"Israel", "Italy", "Lebanon", "Libya", "Malta", "Morocco", "Slovenia",
"Spain", "Syrian Arab Republic", "Tunisia", "Turkiye"), aggri_change = c(-9.52,
-5.13, -8.05, -2.87, -1.29, 9.64, -9.97, 2.63, -3.08, 0.86, 6.93,
1.16, -9.67, 4.13, -9.56, 1.03, 1.87, 5.17, -3.51), fisheries_pdiff = c(7.38,
-2.78, 2.82, 5.23, -28.98, 3.75, -2.04, 0.6, -4.07, -3.75, -3.39,
-3.77, 1.61, 1.25, -3.35, -2.81, -4.79, 0.4, -0.58), food_pdiff = c(3,
-1.69, -0.13, 0.74, -77.64, -2.56, -1.92, 0.58, -2.28, 0.02,
-1.36, -1.54, 4.57, -21.98, -0.71, -2.9, -6.34, -5.01, -0.23),
freshwater_pdiff = c(-1.69, 1.17, -5.22, 1.35, -1.19, -0.94,
-1.1, 0.65, -3.76, -1.66, -1.78, -0.11, 1.01, -1.97, 1.71,
-1.25, -0.94, 0.46, 1.07), co2_pdiff = c(6.79, -0.73, 1.95,
2.12, -1.28, 4.13, 0.87, 0.13, 0.71, -1.72, -0.02, -2.84,
-0.55, 5.15, -0.21, -0.42, -0.19, 3.3, 2.17)), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 15L, 16L,
17L, 18L, 19L, 20L), class = "data.frame")
CodePudding user response:
The main thing is to get the data into 'long' format, after that it's about manipulating ggplot
functions and arguments to make it look the way you want.
I've assumed your dataset is called df1
.
library(ggplot2)
library(tidyr)
df1 |>
pivot_longer(-Country_Name) |>
ggplot(aes(Country_Name, value, fill = name))
geom_col(position = position_dodge())
labs(x = "Country",
fill = "Variable")
theme(legend.position = "bottom",
axis.text.x = element_text(angle = 90))
Created on 2022-11-26 with reprex v2.0.2