[R] fill histogram in ggplot

Ulrik Stervbo
2017-11-07T19:49:00Z

Hi Elahe,

You pass 'probable' to the fill aesthetic along the lines of;

ggplot(hist) +
aes(x=mms, fill = probable) +   geom_histogram(binwidth=1)

The NAs might give you three and not two colours.

I'm guessing you want distinct colours. In this case 'probable'
should be a
factor and not an integer.

HTH
Ulrik

Elahe chalabi via R-help <r-help at r-project.org> schrieb am Di., 7. Nov.
2017, 18:35:

> Hi all,
>
> I have the following data and I have a histogram for mms like
>
>      ggplot(hist,aes(x=hist$mms))+
>
geom_histogram(binwidth=1,fill="white",color="black")and
then I want to
> fill the color of histogram by probable=1 and probable=0, could anyone help
> me in this?
>
> My data:
> structure(list(probable = c(1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L,
> 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, NA, 0L, 1L, NA, NA, 0L, 1L,
> NA, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L,
> 1L, 0L, NA, 1L, NA, NA, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L,
> 1L, NA, 0L, 0L, 1L, 0L, 1L, NA, 1L, 0L, 0L, 0L, 0L, 0L, 1L, NA,
> 0L, 1L, 0L, 1L, NA, 0L, 1L, NA, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
> 1L, 1L, 0L, 0L, 1L, 0L, NA, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L,
> 0L, NA, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, NA, 0L, NA, 1L, 0L, 1L,
> 1L, 1L, 1L, NA, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, NA,
> 1L, NA, NA, 0L, 1L, 0L, 0L, 0L, NA, 1L, NA, NA, 1L, 0L, 1L, 1L,
> 0L, 1L, 0L, NA, 1L, 1L, 1L, NA, 0L, 0L, 1L, NA, NA, 1L, 0L, 0L,
> NA, 1L, 1L, 1L, 0L, 0L, NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, NA,
> 0L, 0L, 0L, 0L, 0L, NA, 0L, NA, 0L, 1L, NA, 1L, 1L, 1L, 1L, 1L,
> 0L, 1L, 1L, NA, 0L, 1L, NA, 1L, 1L, 1L, 1L, 0L, 1L, NA, 1L, 0L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, NA, 1L, 1L,
> 1L, 1L, NA, NA, 1L, NA, 1L, 1L, 1L, NA, NA, NA, 1L, NA, 1L, NA,
> NA, NA, 1L, 1L, 1L, NA, NA, NA, 1L, 1L, NA, 1L, NA, 1L, 1L, 1L,
> NA, 1L, 1L, NA, NA, NA, NA, NA, 1L, 1L, 1L, 1L, NA, 1L, NA, NA,
> NA, NA, 0L, 0L, 0L, 0L, 0L, NA, 1L, 1L, 0L, 0L, 1L, NA, NA, NA,
> NA, 1L, NA, NA, 1L, NA, 1L, 1L, NA, 1L, 1L, NA, NA, 1L, 1L, NA,
> NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, NA, NA, 1L, NA, NA, NA, 1L, NA,
> 1L, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA,
> NA, 1L, 1L, NA, 1L, 1L, 1L, NA, NA, NA, 1L, 1L, NA, NA, 1L, 1L,
> 1L, 1L, NA, NA, NA, NA, 1L, 1L, 1L, 1L, NA, NA, 1L, NA, 1L, NA,
> NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, 1L, 1L, 1L, NA, NA, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 1L, NA, 1L, 1L, 1L, 1L, 0L, NA,
> 1L, 1L, NA, 1L, 1L, NA, NA, NA, NA, 1L, 1L, 0L, 1L, 0L, 0L, 1L,
> 1L, NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA,
> 1L, 1L, 1L, NA, 0L, 1L, NA, 0L, 0L, 0L, 1L, 1L, NA, 1L, 1L, NA,
> 0L, 0L, 1L, NA, 0L, 0L, 1L, 0L, NA, 1L, 0L, 0L, 1L, 1L, 1L, 1L,
> 1L, NA, NA, 0L, 1L, 1L, NA, 1L, NA, NA, 0L, NA, 1L, NA, NA, 1L,
> NA, 1L, 1L, NA, 1L), mms = c(18L, 30L, 20L, 23L, 28L, 22L, 30L,
> 20L, 14L, 30L, 16L, 28L, 28L, 29L, 11L, 28L, 27L, 16L, 29L, 27L,
> 21L, 29L, 26L, 29L, 23L, 28L, 24L, 30L, 30L, 18L, 30L, 23L, 19L,
> 18L, 26L, 30L, 13L, 29L, 30L, 30L, 27L, 26L, 28L, 26L, 25L, 29L,
> 26L, 23L, 29L, 23L, 28L, 29L, 30L, 25L, 17L, 30L, 14L, 24L, 19L,
> 30L, 30L, 27L, 29L, 18L, 22L, 8L, 30L, 28L, 30L, 30L, 30L, 30L,
> 29L, 30L, 17L, 30L, 10L, 24L, 30L, 28L, 30L, 30L, 24L, 29L, 27L,
> 30L, 30L, 29L, 30L, 30L, 23L, 30L, 27L, 10L, 29L, 17L, 28L, 30L,
> 19L, 30L, 24L, 15L, 30L, 29L, 20L, 28L, 27L, 10L, 29L, 30L, 29L,
> 20L, 28L, 25L, 27L, 25L, 29L, 22L, 17L, 28L, 19L, 20L, 16L, 25L,
> 25L, 13L, 30L, 28L, 30L, 30L, 29L, 16L, 20L, 25L, 17L, 17L, 29L,
> 18L, 18L, 16L, 29L, 19L, 26L, 30L, 30L, 16L, 18L, 14L, 28L, 12L,
> 26L, 29L, 25L, 28L, 12L, 30L, 29L, 16L, 22L, 25L, 15L, 30L, 28L,
> 22L, 25L, 27L, 17L, 29L, 30L, 28L, 17L, 23L, 21L, 28L, 30L, 18L,
> 21L, 29L, 26L, 22L, 23L, 26L, 20L, 29L, 15L, 29L, 30L, 28L, 29L,
> 29L, 25L, 27L, 23L, 30L, 14L, 27L, 18L, 16L, 19L, 13L, 18L, 28L,
> 13L, 24L, 30L, 30L, 20L, 25L, 29L, 11L, 20L, 16L, 30L, 16L, 30L,
> 20L, 30L, 20L, 22L, 19L, 23L, 16L, 19L, 13L, 13L, 25L, 19L, 9L,
> 14L, 25L, 23L, 24L, 20L, 24L, 18L, 18L, 19L, 10L, 20L, 11L, 17L,
> 19L, 22L, 6L, 18L, 25L, 17L, 25L, 22L, 29L, 22L, 16L, 8L, 10L,
> 27L, 10L, 18L, 19L, 22L, 28L, 19L, 18L, 25L, 18L, 3L, 10L, 16L,
> 10L, 29L, 22L, 29L, 12L, 15L, 24L, 17L, 6L, 5L, 14L, 27L, 13L,
> 28L, 16L, 20L, 29L, 27L, 30L, 30L, 29L, 30L, 28L, 11L, 30L, 29L,
> 6L, 30L, 16L, 9L, 23L, 10L, 23L, 17L, 24L, 9L, 9L, 18L, 18L,
> 20L, 5L, 27L, 22L, 22L, 8L, 14L, 26L, 24L, 29L, 21L, 23L, 22L,
> 23L, 25L, 6L, 20L, 20L, 2L, 8L, 19L, 20L, 5L, 17L, 20L, 15L,
> 5L, 5L, 20L, 21L, 29L, 25L, 13L, 10L, 21L, 25L, 26L, 13L, 10L,
> 7L, 11L, 18L, 19L, 14L, 27L, 22L, 18L, 7L, 22L, 11L, 12L, 16L,
> 15L, 27L, 27L, 17L, 13L, 16L, 25L, 7L, 19L, 10L, 29L, 20L, 25L,
> 25L, 24L, 15L, 20L, 9L, 5L, 17L, 28L, 24L, 7L, 5L, 13L, 30L,
> 12L, 11L, 15L, 26L, 27L, 19L, 10L, 26L, 29L, 17L, 29L, 18L, 13L,
> 17L, 14L, 14L, 9L, 19L, 7L, 4L, 20L, 14L, 22L, 27L, 16L, 18L,
> 10L, 20L, 12L, 21L, 22L, 5L, 12L, 14L, 13L, 11L, 12L, 20L, 6L,
> 28L, 9L, 20L, 16L, 16L, 2L, 24L, 21L, 26L, 23L, 18L, 10L, 23L,
> 28L, 16L, 30L, 29L, 17L, 14L, 5L, 17L, 26L, 10L, 6L, 17L, 9L,
> 17L, 18L, 9L, 24L, 20L, 11L, 18L, 29L, 17L, 5L, 22L, 12L, 29L,
> 20L, 8L, 30L, 29L, 29L, 20L, 3L, 18L, 22L, 8L, 24L, 29L, 29L,
> 13L, 23L, 30L, 30L, 13L, 28L, 13L, 12L, 29L, 30L, 17L, 14L, 16L,
> 23L, 12L, 20L, 30L, 28L, 20L, 13L, 23L, 13L, 14L, 21L, 26L, 25L,
> 6L, 9L, 18L, 18L, 25L, 27L, 15L, 10L, 7L)), .Names =
c("probable",
> "mms"), row.names = c(NA, -510L), class = "data.frame")
>
> Thanks for any help!
> Elahe
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]