R:geom_histogram(stat =“identity”),缺少参数“env”

时间:2015-07-13 20:42:47

标签: r ggplot2

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

我在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")

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

  

存在错误(name,envir = env,mode = mode):    参数“env”缺失,没有默认值

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

或者,是否有人知道如何更改旁边颜色旁边的内容,因此我可以保留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 个答案:

答案 0 :(得分:0)

已在评论中答复

  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"))