I am trying to plot my data with manual color scale based on values. However the colors that are displayed nowhere near correspond to the values that I provide. My data looks like this:
# A tibble: 100 x 2
chunk avg
<dbl> <dbl>
1 0 0.0202
2 1 0.0405
3 2 0.0648
4 3 0.0405
5 4 0.0283
6 5 -0.00806
7 6 -0.0526
8 7 -0.0364
9 8 -0.00810
10 9 0.0243
# ... with 90 more rows
Then I pipe it to ggplot2:
data %>%
ggplot(
aes(
chunk,
avg,
fill = cut(
avg,
c(-Inf, -0.01, 0.01, Inf)
)
)
) +
geom_bar(stat = "identity", show.legend = FALSE) +
scale_color_manual(
values = c(
"(-Inf, -0.01)" = "red",
"[-0.01, 0.01]" = "yellow",
"(0.01, Inf)" = "green"
)
)
As you can see, I want to color my bars based on values, below -0.01 red, above 0.01 green and bettween - yellow.
This is the result I receive:
What am I missing?

The reason you are getting different colours I think is because
ggplotisn't automatically making a connection between the colours you have supplied and the groups you have supplied. I'm not 100% sure why this is the case, but I can offer a solution.You can create a new column in the data before you send it to
ggplotfor plotting. We will call itcolour_groupbut you can call it anything. We populate this new column based on the values ofavg(I have made sample data as you haven't supplied all of yours). We useifelse()which tests a condition against the data, and returns a value based on if thetestisTRUEorFALSE.In the below code,
colour_group = ifelse(avg < -0.01, 'red', NA)may be read aloud as: "If my value of avg is less than -0.01, make the value for thecolour_groupcolumn 'red', otherwise make itNA". For subsequent lines, we want theFALSEresult to keep the results already in thecolour_groupcolumn - the ones made on the previous lines.Now we can plot the data, and specify that we want to use the
colour_groupcolumn as thefillaesthetic. When specifyingscale_fill_manual, we then tellggplotthat if we have the value ofgreenin thecolour_groupcolumn, we want the bar to be a green colour, and so on for the other colours.It is slightly confusing, in a way having to specify the colour twice. However, we could specify the values of
colour_groupas anything, such as 1, 2, 3 or low, med, high. In this instance, you would do the same code but modify theifelsestatements, and changescale_fill_manualto match these values. For example: