r - 使用 ggplot 和手动数据框一行 5 列创建条形图
问题描述
这是我的数据框。我找不到使用 ggplot 构造条形图的方法,其中列名和 x 作为指示的值。
解决方案
如评论中所述,发布代码/数据/错误的图像是不好的做法。我只复制了数据框的前两个条目。一般来说,对于 ggplot(和整个 tidyverse)观察应该在行而不是列中。因此,您首先需要转换数据,然后绘图相当简单。
library(tidyverse)
df1 <- data.frame(white = 48.8, asian = 40)
df1 %>%
t() %>% # convert columns to rows
as.data.frame() %>% # need to change it to a data frame as t() converts it to a matrix
rownames_to_column("id") %>%
ggplot(aes(x = id, y = V1)) +
geom_col()
导致:
推荐阅读
- mysql - 在 Mysql 中翻译 sql server 中使用的 Merge 语句
- go - 使用 bufio.NewScanner 逐行读取文件时的性能问题
- python - 如何使用 Python 从 SQLite 中的 n 个不同表中获取最后 n 条记录
- r - 从意义矩阵或手动显示紧凑型字母
- c# - ASP.NET - 我如何创建具有匿名类型的列表
- c# - WPF Material Design:创建语言选择
- python - Quart 和 websocket:如何仅向选定的用户发送数据(私人消息)
- javascript - 单击按钮时显示来自 API 的数据。使用 ReactJS
- node.js - MongoDB 嵌套数组聚合更新数组
- python - 如何使用python在列中找到常见的序列模式?