如何解决ggplot bar_col中的降序
下面是数据集,
# A tibble: 449 x 7
`Country or Area` `Region 1` Year Rate MinCI MaxCI Average
<chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 Afghanistan Southern Asia 2011 4.2 2.6 6.2 4.4
2 Afghanistan Southern Asia 2016 5.5 3.4 8.1 5.75
3 Aland Islands Northern Europe NA NA NA NA NA
4 Albania Southern Europe 2011 18.8 14.8 23 18.9
5 Albania Southern Europe 2016 21.7 17 26.7 21.8
6 Algeria Northern Africa 2011 24 19.9 28.4 24.2
7 Algeria Northern Africa 2016 27.4 22.5 32.7 27.6
8 American Samoa Polynesia NA NA NA NA NA
9 Andorra Southern Europe 2011 24.6 19.8 29.8 24.8
10 Andorra Southern Europe 2016 25.6 20.1 31.3 25.7
我需要使用上述数据集绘制bar_col以比较每个区域的平均肥胖率。此外,我需要按从最高到最低的顺序排序。
我还如上所述计算了平均肥胖率。
下面是我用于生成ggplot的代码,但无法弄清楚如何从最高到最低排序。
region_plot <- ggplot(continent) + aes(x = continent$`Region 1`,y = continent$Average,fill = Average) +
geom_col() +
xlab("Region") + ylab("Average Obesity") +
theme(axis.text.x = element_text(angle = 90,hjust = 1)) +
ggtitle("Average obesity rate of each region")
region_plot
解决方法
检查数据后,您将拥有多个区域,因此为了显示每个区域的平均值,您必须先计算然后绘制。您可以使用dplyr
和group_by()
与summarise()
进行此操作。您的数据有限,但对于真实数据,NA
不应该存在。这里的代码使用了部分共享数据。使用真实数据时,请小心使用名称。 reorder()
函数可以排列小节。这里的代码:
library(dplyr)
library(ggplot2)
#Code
df %>% group_by(Region) %>%
summarise(Avg=mean(Average,na.rm=T)) %>%
filter(!is.na(Avg)) %>%
ggplot(aes(x=reorder(Region,-Avg),y=Avg,fill=Region))+
geom_col() +
xlab("Region") + ylab("Average Obesity") +
theme(axis.text.x = element_text(angle = 90,hjust = 1)) +
ggtitle("Average obesity rate of each region")
输出:
使用了一些数据:
#Data
df <- structure(list(Region = c("Southern Asia","Southern Asia","Northern Europe","Southern Europe","Northern Africa","Polynesia","Southern Europe"),Year = c(2011L,2016L,NA,2011L,2016L),Rate = c(4.2,5.5,18.8,21.7,24,27.4,24.6,25.6),MinCI = c(2.6,3.4,14.8,17,19.9,22.5,19.8,20.1),MaxCI = c(6.2,8.1,23,26.7,28.4,32.7,29.8,31.3),Average = c(4.4,5.75,18.9,21.8,24.2,27.6,24.8,25.7)),row.names = c(NA,-10L),class = "data.frame")
,
可以通过预处理数据并按Average
对结果进行排序来解决问题。然后强制Region 1
进行分解。
library(ggplot2)
library(dplyr)
continent %>%
group_by(`Region 1`) %>%
summarise(Average = mean(Average,na.rm = TRUE)) %>%
arrange(desc(Average)) %>%
mutate(`Region 1` = factor(`Region 1`,levels = unique(`Region 1`))) %>%
ggplot(aes(x = `Region 1`,y = Average,fill = Average)) +
geom_col() +
xlab("Region") + ylab("Average Obesity") +
ggtitle("Average obesity rate of each region") +
theme(axis.text.x = element_text(angle = 90,hjust = 1)) -> region_plot
region_plot
数据
continent <- read.table(text = "
'Country or Area' 'Region 1' Year Rate MinCI MaxCI Average
1 Afghanistan 'Southern Asia' 2011 4.2 2.6 6.2 4.4
2 Afghanistan 'Southern Asia' 2016 5.5 3.4 8.1 5.75
3 'Aland Islands' 'Northern Europe' NA NA NA NA NA
4 Albania 'Southern Europe' 2011 18.8 14.8 23 18.9
5 Albania 'Southern Europe' 2016 21.7 17 26.7 21.8
6 Algeria 'Northern Africa' 2011 24 19.9 28.4 24.2
7 Algeria 'Northern Africa' 2016 27.4 22.5 32.7 27.6
8 'American Samoa' Polynesia NA NA NA NA NA
9 Andorra 'Southern Europe' 2011 24.6 19.8 29.8 24.8
10 Andorra 'Southern Europe' 2016 25.6 20.1 31.3 25.7
",header = TRUE,check.names = FALSE)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。