3

I want to make a color-coded Histogram and ran into a problem.

I use ggplot on R 3.1.1

The initial attempt seen below, worked just fine as long as the indicators were numerical. When changing the indicators to strings to make it easier to understand the graph, the order in which the bars are is scrambled.

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

The next step was to add stat="identity"

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

Now I get the following error message, which I do not know how to fix.

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

Does someone know how to fix the error message?

Alternatively,does someone know how to change what is written next to the colors on the side, so I can keep the indicators numerical and simply edit the graph afterwards?

Sorry for the way I put the data in. I don't know how I am supposed to put the data in.

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

Edit: The output of dput(head(yourData, 10)) for when the data is numeric.

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

The output of dput(head(yourData, 10)) for when the data is in strings.

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")`
HiDeoo
  • 10,353
  • 8
  • 47
  • 47
oldmansaur
  • 163
  • 1
  • 9
  • You should post some data to go with this. – Mike Wise Jul 13 '15 at 20:44
  • 2
    If you have discrete data, you should not be plotting a histogram. Histograms are for continuous data. Try a bar plot instead. What does your data look like? Add a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make it easier to help you. – MrFlick Jul 13 '15 at 20:45
  • to post correctly (and painless for us) your data you could just `dput(head(yourData, 10))` – SabDeM Jul 13 '15 at 21:04
  • 1
    `stat="identity"` is used when you compute the summary data you want to plot before plotting them. In your case, it seems that you want `stat="bin"` (count the number of occurences of a given `hard_failure` value and plot the count). Also, you should make sure hard_failure is an integer and indicator is a factor (from your first dataset : `df_2$indicator <- factor(df_2$indicator, levels=c("1","2"),labels=c("Voltage-rule","Time-rule"))` – scoa Jul 13 '15 at 21:59
  • Thanks that fixed the problem. The labels are correct now, without scrambling the x axis. – oldmansaur Jul 16 '15 at 06:51

1 Answers1

0

Answered in comments:

  1. stat="identity" is used when you compute the summary data you want to plot before plotting them.
  2. In this case, it seems that you want stat="bin" (count the number of occurences of a given hard_failure value and plot the count).
  3. Also, you should make sure hard_failure is an integer and indicator is a factor:
df_2$indicator <- factor(df_2$indicator, 
                         levels =c("1", "2"),
                         labels =c("Voltage-rule", "Time-rule"))
Community
  • 1
  • 1
Roman
  • 4,744
  • 2
  • 16
  • 58