Reputation: 69
I have this histogram separated in five categories depending the age. The problem is that I cannot change the color depending the category. I tried to use the marker function with an array filled with the colors I want, but it didn't work as expected:
As you can see, the colors are all bugged.
This is what I tried:
less20 <- subset(dataset, dataset$EDAD <20)
between20n40 <- subset(dataset, dataset$EDAD >=20 & dataset$EDAD <40)
between40n60 <- subset(dataset, dataset$EDAD >=40 & dataset$EDAD <60)
between60n80 <- subset(dataset, dataset$EDAD >=60 & dataset$EDAD <80)
more80 <- subset(dataset, dataset$EDAD >=80)
plot_ly(alpha = 0.7, orientation = 'h', marker = list(color = c('rgba(31,119,180,1)','rgba(105,122,125,1)','rgba(183,124,67,1)', 'rgba(243,127,23,1)','rgba(255,127,14,1)'))) %>%
add_histogram(y = more80$EDAD, name = "More than 80") %>%
add_histogram(y = between60n80$EDAD, name = "Between 60 and 79") %>%
add_histogram(y = between40n60$EDAD, name = "Between 40 and 59") %>%
add_histogram(y = between20n40$EDAD, name = "Between 20 and 39") %>%
add_histogram(y = less20$EDAD, name = "Less than 20") %>%
layout(barmode = "group", title = "",orientation="h")
The correct color order is the next one:
However, I want to change those colors.
Any recomendations? Thanks in advance :)
Upvotes: 0
Views: 528
Reputation: 69
I just had to add the marker function inside add_histogram. That way, I only change the color of each histogram added.
plot_ly(alpha = 0.7, orientation = 'h') %>%
add_histogram(y = more80$EDAD, name = "More than 80", marker = list(color ='rgba(31,119,180,1)')) %>%
add_histogram(y = between60n80$EDAD, name = "Between 60 and 79", marker = list(color ='rgba(105,122,125,1)')) %>%
Thank you for your answers!
Upvotes: 1
Reputation: 97
I think it might be easier if you put the values along with their corresponding ranges inside a dataframe and color the plot using these values and ranges. This is my solution to this using ggplot2
. You can define Values
with your original dataset for the histogram and should obtain a similar result to yours. This solution uses a 1000 normally distributed sample with SD = 30 and MEAN = 70 in order to produce the plot.
# Import ggplot2
library("ggplot2")
# Obtain sample values for histogram
set.seed(1234)
Values = rnorm(n = 1000, mean = 70, sd = 30)
Range = c()
# Get ranges for each value in data
for(i in 1:length(Values)){
if(Values[i] >= 80){
Range[i] = "More than 80"
} else if (Values[i] < 80 & Values[i] >= 60){
Range[i] = "Between 60 and 79"
} else if (Values[i] < 60 & Values[i] >= 40){
Range[i] = "Between 40 and 59"
} else if (Values[i] < 40 & Values[i] >= 20){
Range[i] = "Between 20 and 39"
} else {
Range[i] = "Less than 20"
}
}
# Put all data inside a data frame
plot_dat = data.frame(Values, Range)
# Order plot labels
plot_dat$Range <- factor(plot_dat$Range, levels = c("More than 80", "Between 60 and 79", "Between 40 and 59", "Between 20 and 39", "Less than 20"))
# Produce plot
ggplot(plot_dat, aes(x=Values, fill=Range)) + geom_histogram(binwidth = 5) + coord_flip() + ggtitle("Sample Histogram")
Output
Upvotes: 1