r - Can't seem to get plot to order by value -- am I using group_by() incorrectly? Or is it something else? - Stack Over

r plot of NIH grantee dataHello!I am an R beginner and really enjoying it. I was hoping to create a p

r plot of NIH grantee data

Hello!

I am an R beginner and really enjoying it. I was hoping to create a plot of National Institutes of Health (NIH) grants to research anizations in Massachusetts.

Everything works except for one thing -- my plot keeps getting ordered alphabetically by the anization's name, not by the total value of the grants received. Here's the code I'm using -- what am I doing wrong?

#let's filter out grants under $3M
mass_nih_big_grants <- filter(mass_NIH_data_2023_desc, TOTAL_COST >= 3000000) 

# let's group them by anization. 
# many anizations have more than 1 NIH grant, we want to see the total value
# of grants to an anization. 

mass_nih_big_grantees <- mass_nih_big_grants %>%
  group_by(ORG_NAME) 

# let's put that in descending order before we plot it
mass_nih_big_grantees_desc <- mass_nih_big_grantees[order(-mass_nih_big_grantees$TOTAL_COST),]
 
# do we have to group them again? 
mass_nih_big_grantees_desc %>% 
  group_by(ORG_NAME)
  
# let's plot what we've got. 
plot_mass_nih_big_grantees_desc <- ggplot(mass_nih_big_grantees_desc, aes(x=ORG_NAME, y=TOTAL_COST)) +
  geom_bar(stat="identity", fill="dodgerblue") +
  coord_flip()
  
plot_mass_nih_big_grantees_desc + theme_light()  
# this works; it creates a plot but rearder is not working. It's sorting
# by  name descending (so, Z-A). 

r plot of NIH grantee data

Hello!

I am an R beginner and really enjoying it. I was hoping to create a plot of National Institutes of Health (NIH) grants to research anizations in Massachusetts.

Everything works except for one thing -- my plot keeps getting ordered alphabetically by the anization's name, not by the total value of the grants received. Here's the code I'm using -- what am I doing wrong?

#let's filter out grants under $3M
mass_nih_big_grants <- filter(mass_NIH_data_2023_desc, TOTAL_COST >= 3000000) 

# let's group them by anization. 
# many anizations have more than 1 NIH grant, we want to see the total value
# of grants to an anization. 

mass_nih_big_grantees <- mass_nih_big_grants %>%
  group_by(ORG_NAME) 

# let's put that in descending order before we plot it
mass_nih_big_grantees_desc <- mass_nih_big_grantees[order(-mass_nih_big_grantees$TOTAL_COST),]
 
# do we have to group them again? 
mass_nih_big_grantees_desc %>% 
  group_by(ORG_NAME)
  
# let's plot what we've got. 
plot_mass_nih_big_grantees_desc <- ggplot(mass_nih_big_grantees_desc, aes(x=ORG_NAME, y=TOTAL_COST)) +
  geom_bar(stat="identity", fill="dodgerblue") +
  coord_flip()
  
plot_mass_nih_big_grantees_desc + theme_light()  
# this works; it creates a plot but rearder is not working. It's sorting
# by  name descending (so, Z-A). 
Share Improve this question edited Mar 30 at 5:29 M-- 29.7k10 gold badges70 silver badges106 bronze badges asked Nov 17, 2024 at 4:35 Lisa WilliamsLisa Williams 1
Add a comment  | 

1 Answer 1

Reset to default 1

Reordering the data will not reorder the plot and group_by() is for grouping data during summary calculations. The plot will be ordered by the order of the factor on the x axis. The default is ordering alphabetically, but you can either manually set the order using the levels parameter of the factor function or you can use the fct_reorder() function from forcats to order the factor according to the value of a function. The default function for ordering is the median, which works if the data are already summarized. Compare these two plots where I reorder the factor in the second case.

library(forcats)
library(ggplot2)
DF <- data.frame(Org = c("A","C","B"), Cost = c(4,3,6))
ggplot(DF, aes(x = Org, y = Cost)) + geom_col()

#Now reorder the Org by the Cost
ggplot(DF, aes(x = fct_reorder(Org, Cost), y = Cost)) + geom_col()

Created on 2024-11-16 with reprex v2.1.1

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745638510a4637543.html

相关推荐

  • 怎么用html写出哆啦A梦?

    用HTML和CSS来画哆啦A梦(Doraemon)是一项有趣且具有挑战性的任务。虽然HTML和CSS主要用于网页布局和样式,但通过巧妙的组合和定位,可以创建出相对简单的图形和图案。下面是一个简单的示例,展示了如何用HTML和CSS绘制哆啦A

    1小时前
    00
  • AI驱动的幼儿跌倒检测——视频安全系统的技术解析

    幼儿跌倒检测系统利用AI视频技术,结合人体姿态识别和实时报警功能,提供了一种智能化解决方案。本文将深入剖析其技术架构、模块实现与应用场景,探讨开源技术如何提升幼儿园安全管理。技术架构与实现项目背景幼儿在活动区域(如教室、操场)易发生跌倒,需

    1小时前
    00
  • 用安信可Ai

    以下作品由安信可社区用户业余菜狗制作前言自从接触智能家居之后,笔者就变得很依赖智能家居(绝对不是懒!)比如卧室灯,就在进门的地方,进门开灯很方便,但是晚上睡觉关灯就很不方便。之前是买了一款Wi-Fi灯,是用手机APP操作,刚开始用的时候感觉

    1小时前
    00
  • 电脑密码在哪里设置win11,win11电脑开机密码怎么设置

    Win11系统由于许多设置和以前系统不一样了&#xff0c;所以很多用户们操作非常不习惯&#xff0c;有很多的小伙伴不知道win11系统怎么设置开机密码。给电脑设置密码&#xff0c;只有自己能打开进入系统桌面&a

    52分钟前
    00
  • 我用AI监控了奥特曼,当他一发推特AI就会自动给我打电话。

    上周我真的扛不住了。奥特曼这个孙贼,发了个X说,“要发一个礼拜的好东西”。我信了他的邪,明明出差1周,每天早上9点不到就要起来参加活动,但是晚上根本不敢睡觉,天天蹲到凌晨3点半,蹲到他们那边时间中午12点多,我才敢去睡觉。真的,那一整周,我

    46分钟前
    00
  • 非nvidia卡torchvision报错修复: operator torchvision::nms does not exist

    在Ascend 910b上安装vllm, 会自动把torchaudio和torchvision安装上去.安装前代码语言:shell复制pip list | grep torchtorch

    40分钟前
    00
  • AlignRAG:浙江大学提出的可泛化推理对齐框架,助力 RAG 系统解决推理失配问题

    近年来,检索增强生成(Retrieval-Augmented Generation, RAG)成为知识驱动文本生成的核心范式。然而,现有的 RAG 系统在推理过程中常常出现“推理失配”问题,即模型的推理路径与检索到的证据不一致,导致生成内容

    37分钟前
    00
  • OWASP TOP10

    什么是OWASP?它的全称是 Open Web Application Security Project(开放式 Web 应用程序 安全 项目)TOP 10OWASP Top 10的意思就是10项最严重的Web 应用程序安全风险列表 ,它总

    32分钟前
    00
  • Prometheus配置docker采集器

    Prometheus 配置 Docker 采集器Prometheus 是一个开源的监控系统和时间序列数据库,广泛用于容器化环境中。通过监控 Docker 容器,用户可以实时获取服务性能、资源使用情况等信息。本文将介绍如何为 Docker 容

    29分钟前
    00
  • ascend pytorch 踩坑.

    在910b上安装pytorch 和 pytorch_npu, 因为后续准备装vllm, 所以torch_npu是特殊的版本.代码语言:shell复制pip install torch==2.5.1 --extra-index pip in

    27分钟前
    00
  • 如何快速判断 Flutter 库是否需要适配鸿蒙?纯 Dart 库无需适配!

    在鸿蒙开发中,选择合适的 Flutter 库至关重要。纯 Dart 库因其跨平台特性,无需适配即可直接使用。但对于新手来说,如何判断一个库是否为纯 Dart 库呢?本文将为你提供清晰的判断方法和实用技巧。一、检查 pubspec.yaml

    21分钟前
    00
  • 3、win10重装系统后Mysql环境和数据的恢复

    因为电脑是机哥的原因&#xff0c;重装了好几次电脑&#xff0c;因为我习惯把软件都装在D盘。所以很多东西都还比较好恢复&#xff0c;在网上学会了怎么不卸载重装数据库&#xff0c;自己记录以备后面自己查

    20分钟前
    00
  • CUT&amp;amp;Tag 数据处理和分析教程(7)

    过滤某些项目可能需要对比对质量分数进行更严格的过滤。本文细讨论了bowtie如何分配质量分数,并举例说明。MAPQ(x) = -10 * log10log10(P(x is mapped wrongly)) = -10 * log10(p)

    17分钟前
    10
  • 推荐一个轻量级的监控平台并且支持移动端

    简介XUGOU 是基于Cloudflare构建的轻量化监控平台,专精于系统资源监控与可视化状态页面服务。该平台提供英文简体中文双语支持,满足全球化部署需求。面向开发者及中小团队,项目致力于提供高可用性的监控解决方案。核心功能与实现平台功能

    11分钟前
    00
  • module &#x27;torch.

    踩坑Ascend, 安装 pytorch 2.5.1 和 pytorch_npu 2.5.1, import torch 报错.执行 python -c "import torch;import torch_npu;"时

    10分钟前
    10
  • 重装系统只影响C盘吗?深入解析系统重装的全过程

    重装系统只影响C盘吗?深入解析系统重装的全过程 在计算机的日常使用中,重装系统是一个常见的操作,尤其是在系统出现故障、感染病毒或需要优化系统性能时。然而,许多用户对于重装系统的具体过程和影响存在误解,认为重装系统仅仅是对C盘进行清空和重置

    8分钟前
    00
  • 大模型驱动金融数据应用的实战探索

    近年来,人工智能技术的飞速发展正在重塑全球各行各业的生态格局,金融行业作为数据密集型领域,更是首当其冲。大模型凭借其强大的自然语言处理、逻辑推理和生成能力,逐渐成为金融数据应用的核心驱动力。本文将从行业背景与趋势、核心场景重构、产品能力提升

    6分钟前
    00
  • Windows Server20192022 Evaluation评估版未激活导致关机问题

    摘要&#xff1a;在安装Windows Server 20192022后&#xff0c;会出现系统版本为 Evaluation 评估版情况&#xff0c;如提示Windows许可证已到期&#xff0c;就

    1分钟前
    00
  • 人工智能与ai有什么区别

    一、引言:概念之辨的必要性在科技浪潮席卷全球的当下,人工智能(Artificial Intelligence,简称AI)已成为人们耳熟能详的词汇。然而,当我们深入探讨时,会发现“人工智能”与“AI”这两个表述在语义和使用场景上存在微妙差异。

    1分钟前
    00
  • 实现一个 MySQL 配置对比脚本需要考虑哪些细节?

    作者:李彬,爱可生 DBA 团队成员,负责项目日常问题处理及公司平台问题排查。爱好有亿点点多,吉他、旅行、打游戏爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。本文约 1500 字,预计阅读需要 3 分钟。引言想

    16秒前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信