Geoff
Geoff

Reputation: 1007

r ggplot colored histogram is wrong colour

I have stripped down my code to the following MWE:

library(tidyverse)
library(ggplot2)

x<-rnorm(1000)

df <- data.frame(x = x)
df <- mutate(df, colors = 'blue')

p1<-ggplot(df, aes(x=x, fill=colors)) +
  geom_histogram()
p1

p2<-ggplot(df, aes(x=x), fill=colors) +
  geom_histogram()
p2

p3<-ggplot(df, aes(x=x)) +
  geom_histogram(fill=colors)
p3

I want my histogram to be blue, but p1 is red, p2 is grey and p3 throws an error.

In my actual application I will use more than one colour, hence the use of mutate, I assume it I can get it to work for one, it will work for the others too.

Perhaps my MWE above was too MWE. Here is a more elaborate code:

x<-rnorm(1000)

df <- data.frame(x = x)
df <- mutate(df, colors = case_when(
    x < quantile(x,0.25) ~ "red",
    x >= quantile(x,0.25) & x < quantile(x,0.5) ~ "orange",
    x >= quantile(x,0.5) & x < quantile(x,0.75) ~ "blue",
    x >= quantile(x,0.75) ~ "green"
    )
  )

p1<-ggplot(df, aes(x=x, fill=colors)) +
  geom_histogram()
p1

This returns a histogram with four colours, but they are not blue, green orange and red!

Upvotes: 0

Views: 766

Answers (5)

Neel Kamal
Neel Kamal

Reputation: 1076

Main Idea: Convert the color field as factor and use the levels to get the diff colors in the scale_fill_manual function. Hope it helps, if I understand your Q correctly!!!

x<-rnorm(1000)
col <- if_else(between(x,-1,1), "red", "blue")

#color <- c("blue", "red")
df <- data.frame(x = x, color = as.factor(col))

p1 <- ggplot(df, aes(x=x, fill = color)) +
  geom_histogram() +
  scale_fill_manual(values = levels(df$color))
plot(p1)

You will get the desired result as below:

enter image description here

Upvotes: 1

lroha
lroha

Reputation: 34386

If the desired values are already contained in the dataframe, use scale_*_identity(). By default, there is no guide produced so you need to use the guide argument so that one is generated if needed.

ggplot(df, aes(x = x, fill = colors)) +
  geom_histogram(color = "white") +
  scale_fill_identity(guide = "legend")

enter image description here

Upvotes: 4

John Smith
John Smith

Reputation: 1698

You can do this

x<-rnorm(1000)

df <- data.frame(x = x)

p1<-ggplot() +
  geom_histogram(data=df, aes(x=x),fill="blue")
p1

The fill argument can't understand the value of one column.

If you have different colors, this will do:

p4 <- ggplot(df, aes(x=x, fill=colors)) +
  geom_histogram() +
  scale_fill_manual(values=c("blue","green","orange","red"))
p4

Please note the colors in alphabetical order.

enter image description here

Upvotes: 3

Edward
Edward

Reputation: 18573

p4 <- ggplot(df, aes(x=x, fill=colors)) +
  geom_histogram(col="white") +
  scale_fill_manual(values=df$colors)
p4

If you specify the colour directly in your data frame, then use the scale_fill_manual function and specify the colour manually. In this case, you only have one value (a constant).

It's easier to see with more than one colour.

df <- mutate(df, colors = sample(c('blue','red'), replace=TRUE, size=1000))

ggplot(df, aes(x=x, fill=colors)) +
      geom_histogram(col="white") +
      scale_fill_manual(values=df$colors)

enter image description here

Upvotes: 2

Asif LalDin
Asif LalDin

Reputation: 121

library(ggplot2)

x<-rnorm(1000)

df <- data.frame(x = x)

p1<-ggplot(df, aes(x=x, )) +
  geom_histogram(fill='blue',col='white')
p1

p2<-ggplot(df, aes(x=x)) +
  geom_histogram(fill='red',col='white')
p2

p3<-ggplot(df, aes(x=x)) +
  geom_histogram(fill='green',col='white')
p3

This is a simple of doing it.

Upvotes: -1

Related Questions