如何为数据框的每一行绘制直方图(第一列作为字符值)
How to plot an histogram for every row of a data frame (with the first column as a character value)
我在绘制一系列直方图来说明我的数据时遇到困难。
我有一个数据框,其中包含许多文档的主题流行度。我想绘制一组直方图,在 y 轴上显示每个文档的主题流行度。每一行都是它自己的直方图
我的数据是这样的:
元1
主题 1
主题 2
主题 3
主题4
主题5
文档 1
0.296
1.06
0.0418
3.85
3.61
文档 2
2.06
1.07
0.223
1.57
2.18
Doc3
4.26
1.68
0.215
7.76
14.5
Doc4
1.72
23.6
0.900
3.53
1.41
这是我尝试做的(我真的很不擅长使用ggplot)
plot2%>%
keep(is.numeric) %>%
gather() %>%
ggplot(aes(value)) +
facet_wrap(~ key, scales = "free") +
geom_histogram()
我收到此错误消息:
Error in UseMethod("gather_") :
no applicable method for 'gather_' applied to an object of class "character"
我猜问题出在第一列是字符向量。如果您对此有任何帮助,我将不胜感激!
如果 plot2
是数据框的名称,请尝试 -
library(tidyverse)
plot2 %>%
pivot_longer(cols = -meta1) %>%
ggplot(aes(value)) +
facet_wrap(~ meta1, scales = "free") +
geom_histogram()
我在绘制一系列直方图来说明我的数据时遇到困难。
我有一个数据框,其中包含许多文档的主题流行度。我想绘制一组直方图,在 y 轴上显示每个文档的主题流行度。每一行都是它自己的直方图
我的数据是这样的:
元1 | 主题 1 | 主题 2 | 主题 3 | 主题4 | 主题5 |
---|---|---|---|---|---|
文档 1 | 0.296 | 1.06 | 0.0418 | 3.85 | 3.61 |
文档 2 | 2.06 | 1.07 | 0.223 | 1.57 | 2.18 |
Doc3 | 4.26 | 1.68 | 0.215 | 7.76 | 14.5 |
Doc4 | 1.72 | 23.6 | 0.900 | 3.53 | 1.41 |
这是我尝试做的(我真的很不擅长使用ggplot)
plot2%>%
keep(is.numeric) %>%
gather() %>%
ggplot(aes(value)) +
facet_wrap(~ key, scales = "free") +
geom_histogram()
我收到此错误消息:
Error in UseMethod("gather_") :
no applicable method for 'gather_' applied to an object of class "character"
我猜问题出在第一列是字符向量。如果您对此有任何帮助,我将不胜感激!
如果 plot2
是数据框的名称,请尝试 -
library(tidyverse)
plot2 %>%
pivot_longer(cols = -meta1) %>%
ggplot(aes(value)) +
facet_wrap(~ meta1, scales = "free") +
geom_histogram()