How can I fill and color based on a variable

llrs

In my df I have a column which are color names ie red, green... I want to use those colors for my filling and coloring. However I can't manage to plot it like that, and black is converted to orange, yellow is pink...

enter image description here

new.df <- structure(list(Expr = c(1, 0.75, 0.5, 0.25, 0, 1, 0.75, 0.5, 
0.25, 0, 1, 0.75, 0.5, 0.25, 0, 1, 0.75, 0.5, 0.25, 0, 1, 0.75, 
0.5, 0.25, 0, 1, 0.75, 0.5, 0.25, 0, 1, 0.75, 0.5, 0.25, 0, 1, 
0.75, 0.5, 0.25, 0), react = c(0, 0.25, 0.5, 0.75, 1, 0, 0.25, 
0.5, 0.75, 1, 0, 0.25, 0.5, 0.75, 1, 0, 0.25, 0.5, 0.75, 1, 0, 
0.25, 0.5, 0.75, 1, 0, 0.25, 0.5, 0.75, 1, 0, 0.25, 0.5, 0.75, 
1, 0, 0.25, 0.5, 0.75, 1), variable = structure(c(1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 
8L, 8L, 8L, 8L, 8L), .Label = c("black", "blue", "brown", "green", 
"pink", "red", "turquoise", "yellow"), class = c("ordered", "factor"
)), value = c(0.230403800475059, 0.0617577197149644, 0.0843230403800475, 
0.0819477434679335, 0.0819477434679335, 0.200712589073634, 0.187648456057007, 
0.144893111638955, 0.156769596199525, 0.142517814726841, 0.0451306413301663, 
0.114014251781473, 0.125890736342043, 0.133016627078385, 0.130641330166271, 
0.328978622327791, 0.0665083135391924, 0.0902612826603325, 0.0973871733966746, 
0.0855106888361045, 0.194774346793349, 0.0605700712589074, 0.0451306413301663, 
0.0700712589073634, 0.0653206650831354, NA, 0.0617577197149644, 
0.0605700712589074, 0.0795724465558195, 0.0783847980997625, NA, 
0.336104513064133, 0.0617577197149644, 0.0819477434679335, 0.0617577197149644, 
NA, 0.111638954869359, 0.0498812351543943, 0.168646080760095, 
0.0831353919239905)), row.names = c(NA, -40L), .Names = c("Expr", 
"react", "variable", "value"), class = "data.frame")
> scal <- scale_colour_manual(name = "variable", values = new.df$variable)
>   ggplot(new.df, aes(x = value, fill = variable)) +
+     facet_wrap(~Expr+react, labeller = label_wrap_gen(multi_line = FALSE)) +
+     geom_histogram(bins = 5) + theme_bw() + scal

As per this answer I tried to create manually color scale, without success.

Sandipan Dey

You just need to change one line of your code:

scal <- scale_fill_manual(values = unique(as.character(new.df$variable)))

and keep the remaining part as it is:

ggplot(new.df, aes(x = value, fill = variable)) +
  facet_wrap(~Expr+react, labeller = label_wrap_gen(multi_line = FALSE)) +
  geom_histogram(bins = 5) + theme_bw() + scal

to get this output:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I fill color the tile leaflet?

How can I change the fill color in boxplot

How can i fill part of image with color?

How can I change the color based on my grouping value instead of fill value in ggplot in R?

How can I add fill gradient color to a density plot in R with only one variable

How can we count agents in a statechart based on their fill color in AnyLogic?

How can I fill an html table data cell with a color?

How can I create matplotlib line with z dependent fill color

GoJS: How can I change Node fill color?

how can i fill the of the bottle with color using clipPath

How can I depict a variable as a color in pandas?

how can i use a backgroundresource with variable color

How can I fill columns based on values in another column?

How can I fill missing values based on a condition in R

Can I set different fill color based on a given threshold in Chart.js?

How to change the inside color (fill) of a subset of points with the same shape based on a second variable in ggplot2?

How to fill color in ggridges based on height aesthetics?

SVG: Why can I not override the fill color?

How can I do a matplotlib scatter plot with a categorical x-axis, that allows me to specify the marker and color based on a third variable?

How do I combine feature-state and property based fill-color in Mapbox GL JS?

How can I set a BIT variable based on a value variable?

how can I color specific points based on two data frames

How can I color each cluster differently based on geoJson feature?

How can I change ChartJS line color based on the value?

How can I color Git branches based on their names?

How Can I Color geoJSON features differently based on their id? (googlemaps)

Color/fill bars in geom_col based on another variable?

.tmux.conf: How can I conditionally set a color variable?

How can i change css variable to scss color type?