Skip to content

Two datasets one colour scale using na.value #511

Closed
@bart1

Description

@bart1

Hi the use case where i encountered this bug was when i was trying to plot county borders overlay-ed with colored lines of specific routes. I tried to plot the countries with geom_map.

The example is a bit complicated therefore a small example that produces exactly the same traceback

My idea was to use the NA colour to give a specific colour to one dataset (the country borders) and then use the color scale to give colours to the other dataset. So one dataset has only NA. I realize this is already a work around for plotting the borders but setting the colour arugment for geom_map did not seem to work:

require(ggplot2)
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
crimes$Murder<-NA
states_map <- map_data("state")
ggplot(crimes, aes(map_id = state)) +
   geom_map(aes(fill = Murder), map = states_map, color="green") +
   expand_limits(x = states_map$long, y = states_map$lat) +
        scale_fill_hue(na.value=NA)

produces an empty plot instead of green borders as expected. Using colour mapped as an aes seams to work:

ggplot(crimes, aes(map_id = state)) +
   geom_map(aes(fill = Murder, colour=Murder), map = states_map) +
   expand_limits(x = states_map$long, y = states_map$lat) +
        scale_fill_hue(na.value=NA)

but then i run into this problem below there one dataset has only NA in the colour column

> a<-data.frame(x=1:10, y=1:10, col=1:10);
> b<-data.frame(x=1:10, y=10:1, col=NA); 
> ggplot(data=a,aes(x=x,y=y,colour=col))+geom_point()+geom_point(data=b)+scale_colour_gradient(na.value="red")
Error: Discrete value supplied to continuous scale
> traceback()
12: stop("Discrete value supplied to continuous scale", call. = FALSE)
11: train_continuous(x, range)
10: scale$range$train(x)
9: scale_train.continuous(scale, df[[aesthetic]])
8: scale_train(scale, df[[aesthetic]])
7: FUN(X[[1L]], ...)
6: lapply(scales$scales, scale_train_df, df = df)
5: FUN(X[[2L]], ...)
4: lapply(data, scales_train_df, scales = npscales)
3: ggplot_build(x)
2: print.ggplot(list(data = list(x = 1:10, y = 1:10, col = 1:10), 
       layers = list(<environment>, <environment>), scales = <S4 object of class "Scales">, 
       mapping = list(x = x, y = y, colour = col), options = list(
           labels = list(x = "x", y = "y", colour = "col")), coordinates = list(
           limits = list(x = NULL, y = NULL), wise = FALSE), facet = list(
           shrink = TRUE), plot_env = <environment>))
1: print(list(data = list(x = 1:10, y = 1:10, col = 1:10), layers = list(
       <environment>, <environment>), scales = <S4 object of class "Scales">, 
       mapping = list(x = x, y = y, colour = col), options = list(
           labels = list(x = "x", y = "y", colour = "col")), coordinates = list(
           limits = list(x = NULL, y = NULL), wise = FALSE), facet = list(
           shrink = TRUE), plot_env = <environment>))

But it seems to work as soon as the colour column of the b dataset does contain one non na value

> a<-data.frame(x=1:10, y=1:10, col=1:10);b<-data.frame(x=1:10, y=10:1, col=c(1,rep(NA,9))); ggplot(data=a,aes(x=x,y=y,colour=col))+geom_point()+geom_point(data=b)+scale_colour_gradient(na.value="red")
> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.9.0

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       grid_2.15.0       
 [5] MASS_7.3-17        memoise_0.1        munsell_0.3        plyr_1.7.1        
 [9] proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1     scales_0.2.0      
[13] stringr_0.6  

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions