如何使用 geom_bar() 在 R 中创建两个分组列
How to use geom_bar() to create two grouped columns in R
必须有一种简单的方法来为感知列创建一组分组条,为严重列创建另一组分组条,每组中的各个条是各自区域的值。最终图像应与附加图像相似。
谢谢!
data I'm working with
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")
可以肯定的是,您必须将数据重新格式化为 long,以便您在行格式中拥有所需的变量以便进行绘制。这里的代码使用您分享的数据截图:
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')
输出:
一个选项是转向 'long' 格式,然后使用 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")
-输出
数据
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))
必须有一种简单的方法来为感知列创建一组分组条,为严重列创建另一组分组条,每组中的各个条是各自区域的值。最终图像应与附加图像相似。 谢谢!
data I'm working with
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")
可以肯定的是,您必须将数据重新格式化为 long,以便您在行格式中拥有所需的变量以便进行绘制。这里的代码使用您分享的数据截图:
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')
输出:
一个选项是转向 'long' 格式,然后使用 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")
-输出
数据
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))