Search code examples
rggplot2treemap

How to make scale_fill_manual utilize decimal values?


Currently attempting to utilize cut() and scale_fill_manual to populate a treemap. After troubleshooting I am not sure why my output is behaving the way it is. As far as I can tell, the scale_fill_manual() is only responding to whole numbers and not decimals. Any thoughts on overcoming this issue? I'd like my output to be similar to the first code block.

ggplot(tree_values,aes(area = fidelity_prop,
                                               label = routine_type,
                                               fill = cut(current_score,c(-Inf, .75,.89, Inf))),
                               stat = "identity")+
                               geom_treemap()+
                               geom_treemap_text(fontface = "italic", colour = "white", place = "centre",
                                                grow = F, reflow = T)+
                               scale_fill_manual(name = "Fidelity Condition",
                                                 values = c("(-Inf,.75]" = "red",
                                                            "(.75,.89]" = "yellow",
                                                            "(.89,Inf]" = "green"),
                                                 labels = c("Not Adequate",
                                                            "Needs Improvement",
                                                            "Adequate"))

This code provides this output: .

If I alter the code to this format:

ggplot(tree_values,aes(area = fidelity_prop,
                                                   label = routine_type,
                                                   fill = cut(current_score,c(-Inf, 75,89, Inf))),
                                   stat = "identity")+
                                   geom_treemap()+
                                   geom_treemap_text(fontface = "italic", colour = "white", place = "centre",
                                                    grow = F, reflow = T)+
                                   scale_fill_manual(name = "Fidelity Condition",
                                                     values = c("(-Inf,75]" = "red",
                                                                "(75,89]" = "yellow",
                                                                "(89,Inf]" = "green"),
                                                     labels = c("Not Adequate",
                                                                "Needs Improvement",
                                                                "Adequate"))

This code block displays the following: enter image description here

EDIT: dput() output:

structure(list(routine_type = c("Behavior Management", "Daily Language/Opening Routines", 
"Positive Climate", "Productivity", "Schedule Allows Adequate ELA Instruction", 
"Schedule Components Match Journeys TE and Time Guidelines", 
"Skills and Strategies", "Small Group Time Doesn't Compromise Whole Group", 
"Small Group/Independent Studies", "Word Work"), fidelity_summary = c(8.65, 
15.8, 7.9, 9.65, 0.9, 0.8, 21.6, 0.8, 8.35, 21.95), fidelity_prop = c(0.0670542635658915, 
0.122480620155039, 0.0612403100775194, 0.0748062015503876, 0.00697674418604651, 
0.0062015503875969, 0.167441860465116, 0.0062015503875969, 0.0647286821705426, 
0.17015503875969), high_score = c(0.09302326, 0.1627907, 0.09302326, 
0.09302326, 0.007751938, 0.007751938, 0.2325581, 0.007751938, 
0.09302326, 0.2093023), current_score = c(0.720833300895835, 
0.752380941632653, 0.658333303708335, 0.804166630479168, 0.8999999982, 
0.7999999984, 0.720000122400021, 0.7999999984, 0.695833302020835, 
0.812963062325115)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-10L))

Solution

  • After working through some more. @Camille showed my values were not formatted in the same way cut creates them. The following code works for my use case.

     ggplot(tree_values,aes(area = fidelity_prop,
                                                   label = routine_type,
                                                   fill = cut(current_score,c(-Inf, 0.75,0.9, Inf))),
                                   stat = "identity")+
                                   geom_treemap()+
                                   geom_treemap_text(fontface = "italic", colour = "white", place = "centre",
                                                    grow = F, reflow = T)+
                                   scale_fill_manual(name = "Fidelity Condition",
                                                     values = c("(-Inf,0.75]" = "red",
                                                                "(0.75,0.9]" = "yellow",
                                                                "(0.90,Inf]" = "green"),
                                                     labels = c("Not Adequate",
                                                                "Needs Improvement",
                                                                "Adequate"))
    

    enter image description here