-1

Im trying to recreate the following histogram with ggplot2 but without success.

set.seed(1234)
df <- data.frame(result=floor(rnorm(1000, 100, 20)))    
h <- hist(df$result, plot=FALSE, breaks=20)

# Selected breaks
brks <- c(80,85,90)

cols <- rep("lightblue", length(h$breaks))
# Find bars corresponding to breaks
brk_bars <- h$breaks %in% brks
cols[brk_bars] <- "darkblue"

plot(h, col=cols, main="")

I use:

library(ggplot2)
ggplot(h)+
  geom_histogram(color=cols)

but Im getting Error:datamust be a data frame, or other object coercible byfortify(), not an S3 object with class histogram

firmo23
  • 7,490
  • 2
  • 38
  • 114
  • 1
    I don't want to just regurgitate the error message, but you've called `ggplot` on `h`, which is your `hist` object, when it needs a data frame. Seems like a typo – camille Sep 22 '19 at 15:19
  • yes but I need it like a hist in order to create the breaks – firmo23 Sep 22 '19 at 15:21
  • You can do that with ggplot. You'll set your binwidth or number of bins in `geom_histogram`. But the docs and error message are pretty clear that what goes into the `ggplot` call needs to be a data frame or something easily coerced to one – camille Sep 22 '19 at 15:28
  • I got that but I do not know how to do this.that would the answer to the Q. – firmo23 Sep 22 '19 at 15:30
  • Possible duplicate of [Histogram conditional fill color](https://stackoverflow.com/questions/28323642/histogram-conditional-fill-color) – camille Sep 22 '19 at 16:00
  • 1
    Something like this? `ggplot(df, aes(result))+geom_histogram(bins=, binwidth = 1, aes(fill=(result >= 80&result<=90)))` – M.Viking Sep 22 '19 at 16:21
  • yes I gues with: result == df[1:10,5] – firmo23 Sep 22 '19 at 16:29
  • 1
    a different approach. `df$mycolor<-ifelse((df$result >= 80&df$result<=90), "Group A", "Group B"); ggplot(df, aes(result,fill=mycolor))+geom_histogram(bins=, binwidth=1, )+ scale_fill_manual(values=c("darkblue", "lightblue")) ` – M.Viking Sep 22 '19 at 16:30
  • when I use result == df[1:10,5] instead the bars seem to be on top of the others – firmo23 Sep 22 '19 at 16:36
  • using `seq()` to set the breaks. `df$mycolor<-ifelse((df$result > 80&df$result<=95), "Group A", "Group B"); ggplot(df, aes(result,fill=mycolor))+geom_histogram(bins=, binwidth = , breaks=seq(30, 165, 5))+ scale_fill_manual(values=c("darkblue", "lightblue"))` – M.Viking Sep 22 '19 at 16:42
  • thank you but when I use result == df[1:10,5] they appear on top???I do not want to use df$result > 80&df$result<=95 in my actual dataset – firmo23 Sep 22 '19 at 20:13

1 Answers1

2

Here's a close replication:

ggplot(df)+
  geom_histogram(aes(result, fill = (result<=80 | result > 95)),
                 binwidth = 5, center = 2.5, color = "black") +
  scale_fill_manual(values=c("darkblue", "lightblue"), guide = F) +
  labs(y = "Frequency", x = "Result") +
  theme_classic(base_size = 16)

enter image description here

Pretty similar to your original: enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53