Reputation: 1007
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
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:
Upvotes: 1
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")
Upvotes: 4
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.
Upvotes: 3
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)
Upvotes: 2
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