为什么ggplot scale_colour_manual数值参数不起作用?

vsaztqbk  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(277)

我在使用scale_colour_manual的 na.value 参数时遇到了麻烦。它什么也没做。我使用下面的代码来生成一个填充的等值线图。

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

我绘制的数据如下所示:

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

我在x轴上绘制 date_day,在y轴上绘制 depth,在z轴上绘制 daily_mean,其余列在图中不使用。
生成的图没有问题,但是在 wc_results_daily Dataframe 中包含了na值,但是它们没有在图中绘制出来,我只得到了一个代表na值的空白区域。
example plot with blank area represanting NAs
我很确定,这个问题与我使用的面元尺度有关。我在 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

我想要实现的是,na值以我定义的颜色绘制,并且na标签被添加到图例中。
我的数据集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))

如有任何帮助,
谢谢

ws51t4hk

ws51t4hk1#

毕竟这看起来不起作用!所以我决定自己回答我的问题。看起来填充的等值线图不支持“NA颜色”。我尝试的其他一切只是导致图形的错误显示。我现在使用这个问题的变通方案,通过使用na.approx插入丢失的数据()。如果有太多的数据缺失,以至于插值不可信,我不得不忍受空白...
谢谢所有的评论,干杯...

相关问题