lkasquilici
lkasquilici

Reputation: 45

Filling up histograms with ggplot - Changing colours

I am learning how to use the R and ggplot library and got stucked with the challenge to change the colour of the following Histogram! I did manage to change the full colour from the graph but it was with solid colours! I wanted to get count with different colours and somehow I am not managing to find out the right way to do so! Can you help me to change it to another colour?

df4 <- data.frame(rnorm(10000,100,10))
colnames(df4) <- c("Value")

histi_base2 <- ggplot(df4, aes(x=Value))

histi5 <- histi_base2 + geom_histogram(binwidth = 1, colour="blue", alpha=0.8, aes(fill=..count..)) + labs(title="My first Histogram", subtitle = "in blue")
histi5

Blue Histogram

Upvotes: 3

Views: 1315

Answers (2)

Marco_CH
Marco_CH

Reputation: 3294

Try:

df4 <- data.frame(rnorm(10000,100,10))
colnames(df4) <- c("Value")

histi_base2 <- ggplot(df4, aes(x=Value))
histi5 <- histi_base2 + 
  geom_histogram(binwidth = 1, alpha=0.8, fill="red", color="black") + 
  labs(title="Your second Histogram", subtitle = "now in solid red")
histi5

With fill you're changing the color inside the bars and with color you're changing the borders. enter image description here

PS: sorry, think I haven't understand it correctly. Martins solution seems to fit better.

Upvotes: 1

Martin Gal
Martin Gal

Reputation: 16978

You could use scale_fill_gradient:

df4 <- data.frame(rnorm(10000,100,10))
colnames(df4) <- c("Value")

library(ggplot2)

ggplot(df4, aes(x=Value)) + 
  geom_histogram(binwidth = 1, alpha=0.8, aes(fill=..count..)) + 
  scale_fill_gradient(low = "red", high = "green") +
  labs(title="My first Histogram")

enter image description here

Upvotes: 4

Related Questions