gpt4 book ai didi

R: geom_histogram(stat ="identity), argument "env"丢失

转载 作者:行者123 更新时间:2023-12-02 04:47:54 25 4
gpt4 key购买 nike

我想制作一个颜色编码的直方图,但遇到了问题。

我在 R 3.1.1 上使用 ggplot

下面看到的初始尝试,只要 indicators 就可以正常工作是数字。当将 indicators 更改为字符串以使其更易于理解图形时,条形图的顺序被打乱了。

ggplot(df_2,aes(hard_failures,fill=indicator)) + geom_histogram() 

下一步是添加 stat="identity"

ggplot(df_2,aes(hard_failures,fill=indicator)) + geom_histogram(stat="identity")

现在我收到以下错误消息,我不知道如何修复。

Error in exists(name, envir = env, mode = mode) : argument "env" is missing, with no default

有人知道如何修复错误消息吗?

或者,有人知道如何更改侧面颜色旁边的内容,这样我就可以保持 indicators 数值,然后简单地编辑图形吗?

对于我放入数据的方式感到抱歉。我不知道应该如何放入数据。

hard_failures   indicator   
36 2
3 1
46 2
36 2
54 2
3 1
6 1
47 2

hard_failures indicator
36 "Time-Rule"
3 "Voltage-Rule"
46 "Time-Rule"
36 "Time-Rule"
54 "Time-Rule"
3 "Voltage-Rule"
6 Voltage-Rule
47 Time-Rule

编辑:当数据为数字时,dput(head(yourData, 10)) 的输出。

structure(list(hard_failures = c(36, 3, 46, 36, 54, 3, 6, 47, 
55, 2), indicator = structure(c(2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L,
2L, 1L), .Label = c("1", "2"), class = "factor")), .Names = c("hard_failures",
"indicator"), row.names = c(NA, 10L), class = "data.frame")`

当数据为字符串时,dput(head(yourData, 10)) 的输出。

structure(list(hard_failures = structure(c(21L, 14L, 32L, 21L, 
41L, 14L, 43L, 33L, 42L, 8L), .Label = c("0", "1", "10", "11",
"12", "14", "19", "2", "20", "21", "23", "28", "29", "3", "30",
"31", "32", "33", "34", "35", "36", "37", "38", "39", "4", "40",
"41", "42", "43", "44", "45", "46", "47", "48", "49", "5", "50",
"51", "52", "53", "54", "55", "6", "7", "8", "9"), class = "factor"),
indicator = structure(c(1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L,
2L), .Label = c("Time-Rule", "Voltage-Rule"), class = "factor")), .Names >= c("hard_failures",
"indicator"), row.names = c(NA, 10L), class = "data.frame")`

最佳答案

在评论中回答:

  1. stat="identity" 用于在绘制之前计算要绘制的汇总数据。
  2. 在这种情况下,您似乎需要 stat="bin"(计算给定 hard_failure 值的出现次数并绘制计数)。<
  3. 此外,您应该确保 hard_failure 是一个整数并且 indicator 是一个因素:
df_2$indicator <- factor(df_2$indicator, 
levels =c("1", "2"),
labels =c("Voltage-rule", "Time-rule"))

关于R: geom_histogram(stat ="identity), argument "env"丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31393071/

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