Search code examples
rggplot2scale-color-manualmetr

Why does ggplot scale_colour_manual na.value parameter not work?


I ran in trouble while using scale_colour_manual's na.value parameter. It simply does nothing. I use the following code to generate a filled contour plot.

p1<-ggplot(wc_results_daily, aes(date_day, depth_num, z=daily_mean)) + 
  
  scale_color_manual(aesthetics = "fill", 
                      values = break_vec$break_colors, 
                      labels = break_vec$break_labels, 
                      name="Water Content",
                      drop=FALSE,
                      na.value = "black")+
  
  geom_contour_filled(show.legend = TRUE, 
                      colour="white", 
                      breaks = break_vec$breaks_bining)+
  
  geom_text_contour(aes(z = daily_mean))+
  
  scale_y_continuous(labels=c("200 cm","135 cm","90 cm","40 cm","20 cm"), 
                     name="Depth")+
  
  scale_x_date(name="Date And Time", date_breaks = "1 month" ,date_labels = "%b %d")+
  theme_classic()+
  labs(title="LP 4 Watercontent Over Time", subtitle=paste0("from ",first(wc_results_daily$date_day), " to ", last(wc_results_daily$date_day)))

p1

The data I'm ploting looks like this:

wc_results_daily[1051:1070,]
# A tibble: 20 x 6
# Groups:   date_day [4]
date_day   depth  daily_mean bins     rn_2 depth_num
   <date>     <fct>       <dbl> <fct>   <dbl>     <dbl>
 1 2021-07-30 135 cm       20.2 18%-21%   210         2
 2 2021-07-30 20 cm        NA   NA        210         5
 3 2021-07-30 200 cm       26.8 24%-27%   210         1
 4 2021-07-30 40 cm        32.2 30%-33%   210         4
 5 2021-07-30 90 cm        27.4 27%-30%   210         3
 6 2021-07-31 135 cm       20.2 18%-21%   211         2
 7 2021-07-31 20 cm        NA   NA        211         5
 8 2021-07-31 200 cm       26.8 24%-27%   211         1
 9 2021-07-31 40 cm        32.2 30%-33%   211         4
10 2021-07-31 90 cm        27.4 27%-30%   211         3
11 2021-08-01 135 cm       20.2 18%-21%   212         2
12 2021-08-01 20 cm        NA   NA        212         5
13 2021-08-01 200 cm       26.8 24%-27%   212         1
14 2021-08-01 40 cm        32.2 30%-33%   212         4
15 2021-08-01 90 cm        27.4 27%-30%   212         3
16 2021-08-02 135 cm       20.2 18%-21%   213         2
17 2021-08-02 20 cm        NA   NA        213         5
18 2021-08-02 200 cm       26.8 24%-27%   213         1
19 2021-08-02 40 cm        32.2 30%-33%   213         4
20 2021-08-02 90 cm        27.4 27%-30%   213         3

I´m plotting date_day on the x-axes and depth on y-axes and daily_mean on the z-axes. The remaining columns are not used in the plot.

The plot is generated without problems, but there are na values contained in wc_results_daily dataframe. But they are just not ploted in the graph. I just get a blank area representing the na values.

example plot with blank area represanting NAs

I am pretty sure, that this problem relates to the binned scale I'm using. I defined the breaks and labels and colors within a dataframe:


> break_vec
   break_labels break_colors breaks_bining
1         0%-3%      #FF0000             3
2         3%-6%      #DD2200             6
3         6%-9%      #BB4400             9
4        9%-12%      #996600            12
5       12%-15%      #778800            15
6       15%-18%      #55AA00            18
7       18%-21%      #32CC00            21
8       21%-24%      #10EE00            24
9       24%-27%      #00EE10            27
10      27%-30%      #00CC32            30
11      30%-33%      #00AA54            33
12      33%-35%      #008876            35
13      35%-38%      #006599            38
14      38%-41%      #0043BB            41
15      41%-44%      #0021DD            44
16      44%-47%      #0000FF            47

What I would like to achieve is that, na values are ploted in the color I defined and that the na-label is added to the legend.

A sample of my dataset dput():

structure(list(date_day = structure(c(18827, 18828, 18828, 18828, 
18828, 18828, 18829, 18829, 18829, 18829, 18829, 18830, 18830, 
18830, 18830, 18830, 18831, 18831, 18831, 18831, 18831, 18832, 
18832, 18832, 18832, 18832, 18833, 18833, 18833, 18833, 18833, 
18834, 18834, 18834, 18834, 18834, 18835, 18835, 18835, 18835, 
18835, 18836, 18836, 18836, 18836, 18836, 18837, 18837, 18837, 
18837, 18837, 18838), class = "Date"), depth = structure(c(3L, 
2L, 5L, 1L, 4L, 3L, 2L, 5L, 1L, 4L, 3L, 2L, 5L, 1L, 4L, 3L, 2L, 
5L, 1L, 4L, 3L, 2L, 5L, 1L, 4L, 3L, 2L, 5L, 1L, 4L, 3L, 2L, 5L, 
1L, 4L, 3L, 2L, 5L, 1L, 4L, 3L, 2L, 5L, 1L, 4L, 3L, 2L, 5L, 1L, 
4L, 3L, 2L), .Label = c("200 cm", "135 cm", "90 cm", "40 cm", 
"20 cm"), class = "factor"), daily_mean = c(27.3045833333333, 
19.9475, 23.414375, 26.8395833333333, 31.5583333333333, 27.3114583333333, 
19.9672916666667, 23.2239583333333, 26.8389583333333, 31.6847916666667, 
27.290625, 19.9825, 23.0616666666667, 26.8416666666667, 31.7829166666667, 
27.273125, 19.99625, 22.9054166666667, 26.841875, 31.8722916666667, 
27.2620833333333, 20.01375, 22.7541666666667, 26.8435416666667, 
31.951875, 27.2627083333333, 20.0347916666667, 22.59375, 26.8447916666667, 
32.035, 27.2697916666667, 20.0664583333333, 22.4314583333333, 
26.8472916666667, 32.1279166666667, 27.2947916666667, 20.0879166666667, 
NA, 26.8379166666667, 32.1947916666667, 27.324375, 20.1245833333333, 
NA, 26.8385416666667, 32.2370833333333, 27.3508333333333, 20.16125, 
NA, 26.8385416666667, 32.249375, 27.375625, 20.1854166666667), 
    bins = structure(c(10L, 7L, 8L, 9L, 11L, 10L, 7L, 8L, 9L, 
    11L, 10L, 7L, 8L, 9L, 11L, 10L, 7L, 8L, 9L, 11L, 10L, 7L, 
    8L, 9L, 11L, 10L, 7L, 8L, 9L, 11L, 10L, 7L, 8L, 9L, 11L, 
    10L, 7L, NA, 9L, 11L, 10L, 7L, NA, 9L, 11L, 10L, 7L, NA, 
    9L, 11L, 10L, 7L), .Label = c("0%-3%", "3%-6%", "6%-9%", 
    "9%-12%", "12%-15%", "15%-18%", "18%-21%", "21%-24%", "24%-27%", 
    "27%-30%", "30%-33%", "33%-35%", "35%-38%", "38%-41%", "41%-44%", 
    "44%-47%", "47%-50%"), class = "factor"), bins_val = c(30, 
    21, 24, 27, 33, 30, 21, 24, 27, 33, 30, 21, 24, 27, 33, 30, 
    21, 24, 27, 33, 30, 21, 24, 27, 33, 30, 21, 24, 27, 33, 30, 
    21, 24, 27, 33, 30, 21, NA, 27, 33, 30, 21, NA, 27, 33, 30, 
    21, NA, 27, 33, 30, 21), rn_2 = c(199, 200, 200, 200, 200, 
    200, 201, 201, 201, 201, 201, 202, 202, 202, 202, 202, 203, 
    203, 203, 203, 203, 204, 204, 204, 204, 204, 205, 205, 205, 
    205, 205, 206, 206, 206, 206, 206, 207, 207, 207, 207, 207, 
    208, 208, 208, 208, 208, 209, 209, 209, 209, 209, 210), depth_num = c(3, 
    2, 5, 1, 4, 3, 2, 5, 1, 4, 3, 2, 5, 1, 4, 3, 2, 5, 1, 4, 
    3, 2, 5, 1, 4, 3, 2, 5, 1, 4, 3, 2, 5, 1, 4, 3, 2, 5, 1, 
    4, 3, 2, 5, 1, 4, 3, 2, 5, 1, 4, 3, 2)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -52L), groups = structure(list(
    date_day = structure(c(18827, 18828, 18829, 18830, 18831, 
    18832, 18833, 18834, 18835, 18836, 18837, 18838), class = "Date"), 
    .rows = structure(list(1L, 2:6, 7:11, 12:16, 17:21, 22:26, 
        27:31, 32:36, 37:41, 42:46, 47:51, 52L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -12L), .drop = TRUE))

Any help is appreciated

thx...


Solution

  • After all this seems not to work! So I decided to answer my question by my self. It seems that the filled contourplot does not support the "NA color". Everything else I tried just leads to erroneous display of the graph. I'm using a workaround for this problem now, by interpolating the missing data using na.approx(). Where ever there are to many missing data, such that interpolation is not trustworthy, I have to live with the blank...

    Thx for all comments cheers...