gpt4 book ai didi

r - 如何通过条形图融化 R data.frame 和绘图组

转载 作者:行者123 更新时间:2023-12-03 23:12:45 25 4
gpt4 key购买 nike

我有以下 R data.frame:

  group match unmatch unmatch_active match_active
1 A 10 4 0 0
2 B 116 20 0 3
3 c 160 27 1 4
4 D 79 17 0 3
5 E 309 84 4 14
6 F 643 244 10 23
...

我的目标是按条形图( http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2)/具有更多变量的截面图)绘制一个组,如链接中所示。

我意识到在开始之前我需要将数据转换为以下格式
  group variable value
1 A match 10
2 B match 116
3 C match 160
4 D match 79
5 E match 309
6 F match 643
7 A unmatch 4
8 B unmatch 20
...

我使用了融化功能:
groups.df.melt <- melt(groups.df[,c('group','match','unmatch', 'unmatch_active', 'match_active')],id.vars = 1)

我认为我没有正确地进行融化,因为在我执行上述 groups.df.melt 后有 1000 多行对我来说没有意义。

我怎么看 Draw histograms per row over multiple columns in R并尝试遵循相同的方法,但我没有得到我想要的图表。

此外,我收到以下错误:当我尝试进行绘图时:
ggplot(groups.df.melt, aes(x='group', y=value)) + geom_bar(aes(fill = variable), position="dodge") + scale_y_log10()

Mapping a variable to y and also using stat="bin".
With stat="bin", it will attempt to set the y value to the count of cases in each group.
This can result in unexpected behavior and will not be allowed in a future version of ggplot2.
If you want y to represent counts of cases, use stat="bin" and don't map a variable to y.
If you want y to represent values in the data, use stat="identity".
See ?geom_bar for examples. (Deprecated; last used in version 0.9.2)
Error in pmin(y, 0) : object 'y' not found

最佳答案

尝试:

mm <- melt(ddf, id='group')
ggplot(data = mm, aes(x = group, y = value, fill = variable)) +
geom_bar(stat = 'identity', position = 'dodge')

或者
ggplot(data = mm, aes(x = group, y = value, fill = variable)) + 
# `geom_col()` uses `stat_identity()`: it leaves the data as is.
geom_col(position = 'dodge')

enter image description here

关于r - 如何通过条形图融化 R data.frame 和绘图组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26345427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com