There has got to be an easy way to create one set of grouped bars for aware column and a another set of grouped bars for serious column with the individual bars within each group being the value for the respective regions. Final image should look similar to attached image. Thanks!
Image I'm seeking approximately
by_region <- country_obs_df %>%
group_by(Region) %>%
summarize(
#region_avg_gdp = GDPperUS,
#region_avg_co2 = CO2emi,
#region_avg_pop = Population.2008,
region_avg_aware = mean(Aware),
region_avg_serious = mean(Serious)
)
ggplot(by_region) +
geom_col(mapping = aes(fill = Region, x = Region, y=region_avg_aware), position = "dodge") +
labs(y = "Percent")
For sure, it is possible, you have to reformat your data to long so that you have the required variables in a row format in order to be plotted. Here the code using the data screenshot you shared:
library(tidyverse)
#Data
df <- data.frame(Region=c('Africa','Asia','Europe','Europe (North America)',
'Europe (Oceania)','Latin America & Caribbean'),
region_avg_aware=c(39.9,60.9,88.3,96.6,97.3,63.8),
region_avg_serious=c(82.3,76.3,67.7,71.1,78.2,93.8))
#Plot
df %>% pivot_longer(-Region) %>%
ggplot(aes(x=name,y=value,group=Region,fill=Region))+
geom_bar(stat = 'identity',position = position_dodge(0.9))+
theme_bw()+
theme(axis.text = element_text(color='black',face='bold'),
axis.title = element_text(color='black',face='bold'),
legend.text = element_text(color='black',face='bold'),
legend.title = element_text(color='black',face='bold'),
panel.grid = element_blank())+
xlab('Variable')
Output:
An option is to pivot to 'long' format then, use geom_col
library(dplyr)
library(ggplot2)
library(tidyr)
by_region %>%
pivot_longer(cols = -Region, names_to = 'region_avg') %>%
ggplot(aes(x = region_avg, y = value, fill = Region)) +
geom_col( position = "dodge") +
labs(y = "Percent")
-output
by_region <- structure(list(Region = c("Africa", "Asia", "Europe", "Europe (North America)",
"Europe (Oceania)", "Latin America & Caribbean"), region_avg_aware = c(39.9,
60.9, 88.3, 96.6, 97.3, 63.8), region_avg_serious = c(82.3, 76.3,
67.7, 71.1, 78.2, 93.8)), class = "data.frame", row.names = c(NA,
-6L))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.