简体   繁体   中英

scale_fill_manual with contradicting results

I am puzzled by contradicting results I am getting from a ggplot with scale_fill_manual .

With the code below I intend to plot red tiles when the variable heat is -1, grey tiles when it is 0, and green tiles when it is 1. This code is nested in a loop and overwhelmingly produces the expected graphs. For some unknown reasons (to me at least), there are some iterations which produce graphs with colors contradicting the data.

Data 1 below should result in a graph with two green tiles (the rest grey) - however I get overwhelming red tiles (which should be grey) and two grey tiles (which should be green). There is no observation in the data with heat of value -1, yet almost all tiles are red (those which have value 0)

For the sake of contrast, I provide also an example which produces correct results (Data 2 & Graph 2 at the very bottom).

Is this a bug related to this https://github.com/hadley/ggplot2/issues/384 ? Is there something wrong with the code? Or am I missing something?

Code

comp.plot <- ggplot(df, aes(y=variable, x=as.factor(as.character(year)), fill=as.factor(heat)))+
  geom_tile()+
  ggtitle(paste("Difference"))+
  theme(plot.title=element_text(face="bold"),
        legend.position="bottom", 
        legend.title=element_text(size=7), 
        legend.text=element_text(size=5),  
        legend.box="vertical",
        axis.title.x = element_blank(),
        axis.text.x  = element_text(angle=90, size=6), 
        axis.title.y = element_blank(),
        axis.text.y  = element_blank(),
        axis.ticks.y = element_blank()) + 

  guides(fill=guide_legend(title.position="top",
                           keywidth=0.5, keyheight=0.5))+

  scale_fill_manual(name="Promise vs Practice",
                    breaks=c(-1,0,1),
                    labels=c("No Practice","No Promise","Practice"),
                    drop=FALSE,
                    values=c("darkred","lightgrey","darkgreen")) 

Data 1

df <– as.data.frame(structure(list(variable = structure(c(11L, 14L, 12L, 13L, 4L, 
3L, 2L, 1L, 16L, 15L, 8L, 6L, 7L, 9L, 5L, 10L), .Label = c("eps_commission", 
"eps_company", "mps_armyint", "mps_milcmd", "other_constitution", 
"other_parlelect", "other_preselect", "other_proprep", "other_referendum", 
"other_unresolved", "pps_cabinet", "pps_nsencabinet", "pps_parlquota", 
"pps_sencabinet", "tps_autonomy", "tps_devolution"), class = "factor"), 
    year = c(2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 
    2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006), heat = c(0, 
    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0)), .Names = c("variable", 
"year", "heat"), class = "data.frame", row.names = c(7L, 86L, 
165L, 244L, 323L, 402L, 481L, 560L, 639L, 718L, 797L, 876L, 955L, 
1034L, 1113L, 1192L)))

Graph 1

在此处输入图片说明

Data 2

df2 <– as.data.frame(structure(list(variable = structure(c(11L, 14L, 12L, 13L, 4L, 
3L, 2L, 1L, 16L, 15L, 8L, 6L, 7L, 9L, 5L, 10L), .Label = c("eps_commission", 
"eps_company", "mps_armyint", "mps_milcmd", "other_constitution", 
"other_parlelect", "other_preselect", "other_proprep", "other_referendum", 
"other_unresolved", "pps_cabinet", "pps_nsencabinet", "pps_parlquota", 
"pps_sencabinet", "tps_autonomy", "tps_devolution"), class = "factor"), 
    year = c(1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 
    1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999), heat = c(1, 
    1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 1)), .Names = c("variable", 
"year", "heat"), class = "data.frame", row.names = c(11L, 90L, 
169L, 248L, 327L, 406L, 485L, 564L, 643L, 722L, 801L, 880L, 959L, 
1038L, 1117L, 1196L)))

Graph 2

在此处输入图片说明

Your code for making the data.frame isn't working for me, but I think I might see the problem. When you check the class of the variable "heat" in your data.frame df, is it numeric or factor? I bet it's numeric. When I made a sample data.frame similar to the one you've listed and just left the "heat" variable as numeric data, the graph looked like the one you've posted. However, when I did this:

 df$heat <- factor(df$heat, levels = c(-1, 0, 1))

and then ran the snippet for creating the graph, the values seem to be mapped correctly, ie the "0" values were gray and the "1" values were green. I think that ggplot2 is mapping the lowest value it encounters to the first color listed in the scale_fill_manual values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM