ggplot bar_col中的降序

如何解决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

解决方法

检查数据后,您将拥有多个区域,因此为了显示每个区域的平均值,您必须先计算然后绘制。您可以使用dplyrgroup_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")

输出:

enter image description here

使用了一些数据:

#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

enter image description here

数据

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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-