简体   繁体   English

我如何在 fill.contour 空白中制作关键轴(z 图例),但保留绘图尺寸?

[英]how do I make the key axis (z legend) in filled.contour blank, but retain the plot dimensions?

I am trying to make a filled.contour plot with the legend transparent or blank.我正在尝试使用透明或空白的图例制作一个 fill.contour 图。 But, I need to retain the space that was occupied by the legend.但是,我需要保留传说所占据的空间。

I've tried to add a white rect(), but i don't quite understand the coordinates as i think the legend key is actually a separate axis or plot.我试图添加一个白色的 rect(),但我不太明白坐标,因为我认为图例键实际上是一个单独的轴或图。

#the data
wtr.dates<-as.POSIXct(c('2019-06-22 16:01:01', '2019-06-22 20:01:01', '2019-06-23 00:01:01', '2019-06-23 04:01:01', '2019-06-23 08:01:01', '2019-06-23 12:01:01', '2019-06-23 16:01:01', '2019-06-23 20:01:01', '2019-06-24 00:01:01', '2019-06-24 04:01:01', '2019-06-24 08:01:01', '2019-06-24 12:01:01', '2019-06-24 16:01:01', '2019-06-24 20:01:01', '2019-06-25 00:01:01', '2019-06-25 04:01:01', '2019-06-25 08:01:01', '2019-06-25 12:01:01', '2019-06-25 16:01:01', '2019-06-25 20:01:01', '2019-06-26 00:01:01', '2019-06-26 04:01:01', '2019-06-26 08:01:01', '2019-06-26 12:01:01', '2019-06-26 16:01:01', '2019-06-26 20:01:01', '2019-06-27 00:01:01', '2019-06-27 04:01:01', '2019-06-27 08:01:01', '2019-06-27 12:01:01', '2019-06-27 16:01:01'), format="%Y-%m-%d %H:%M:%OS")
wtr.mat<-as.matrix(data.frame(wtr_1=runif(31, 10, 20), wtr_2=runif(31, 10, 20), wtr_3=runif(31, 10, 20)))

#the plot
plot.new()
graphics::filled.contour(wtr.dates
                     , 1:3
                     , wtr.mat
                     , ylim=c(3,1)
                     , zlim=c(min(wtr.mat,na.rm=TRUE) , max(wtr.mat,na.rm=TRUE))
                     , nlevels=100
                     , key.axes =axis(1, col="transparent", labels=FALSE)
                     , color.palette = grDevices::colorRampPalette(c("violet","blue","cyan", "green3", "yellow", "orange", "red")
                                                                   , bias = 1
                                                                   , space = "rgb")
                     , ylab=""
                     , key.title=graphics::title((main="Temperature (\u00B0C)")
                                                 ,adj=0.2, col="red", cex.main=1)
                     ,plot.axes = {#graphics::lines(x=wtr.dates,y=wtr.all$thermo.depth,col="black",lwd = 2)
                       #graphics::lines(x=wtr.dates,y=wtr.all$top, col="gray50", lwd = 2)
                       #graphics::lines(x=wtr.dates,y=wtr.all$bottom,col="gray80", lwd = 2)
                       graphics::axis(side = 2, col="transparent", labels=FALSE)
                       #axis.Date(side=1, x=wtr.dates, at=datestoshow, format=ttformat)
                       graphics::axis(side = 1, labels=format(wtr.dates[c(1, 10, 20, 30)], "%d %b %H:%M"), at = wtr.dates[c(1, 10, 20, 30)], pos = c(max(1:3)), tck = -0.03)})

在此处输入图片说明

I just want to get rid of the legend on the right and keep the space it held white or transparent.我只想去掉右边的图例,并保持它的空间是白色或透明的。

I suspect a solution is hacky (overplotting white) or hard (rebuilding the component parts of filled.contour ).我怀疑一个解决方案是 hacky (overplotting white) 或 hard ( filled.contour the component part filled.contour )。 Alternatively, you can do it in ggplot with metR::geom_contour_fill (which gets a little slow with lots of bins, unfortunately):或者,您可以在 ggplot 中使用metR::geom_contour_fill (不幸的是,由于很多 bin 会变得有点慢):

library(tidyverse)
set.seed(47)

wtr <- tibble(timestamp = as.POSIXct(c('2019-06-22 16:01:01', '2019-06-22 20:01:01', '2019-06-23 00:01:01', '2019-06-23 04:01:01', '2019-06-23 08:01:01', '2019-06-23 12:01:01', '2019-06-23 16:01:01', '2019-06-23 20:01:01', '2019-06-24 00:01:01', '2019-06-24 04:01:01', '2019-06-24 08:01:01', '2019-06-24 12:01:01', '2019-06-24 16:01:01', '2019-06-24 20:01:01', '2019-06-25 00:01:01', '2019-06-25 04:01:01', '2019-06-25 08:01:01', '2019-06-25 12:01:01', '2019-06-25 16:01:01', '2019-06-25 20:01:01', '2019-06-26 00:01:01', '2019-06-26 04:01:01', '2019-06-26 08:01:01', '2019-06-26 12:01:01', '2019-06-26 16:01:01', '2019-06-26 20:01:01', '2019-06-27 00:01:01', '2019-06-27 04:01:01', '2019-06-27 08:01:01', '2019-06-27 12:01:01', '2019-06-27 16:01:01'), format="%Y-%m-%d %H:%M:%OS"), 
       wtr_1 = runif(31, 10, 20), 
       wtr_2 = runif(31, 10, 20), 
       wtr_3 = runif(31, 10, 20)) %>% 
    gather(y, value, -timestamp) %>% 
    mutate(y = parse_number(y))

wtr
#> # A tibble: 93 x 3
#>    timestamp               y value
#>    <dttm>              <dbl> <dbl>
#>  1 2019-06-22 16:01:01     1  19.8
#>  2 2019-06-22 20:01:01     1  13.7
#>  3 2019-06-23 00:01:01     1  17.6
#>  4 2019-06-23 04:01:01     1  18.2
#>  5 2019-06-23 08:01:01     1  15.7
#>  6 2019-06-23 12:01:01     1  16.9
#>  7 2019-06-23 16:01:01     1  13.9
#>  8 2019-06-23 20:01:01     1  14.7
#>  9 2019-06-24 00:01:01     1  15.4
#> 10 2019-06-24 04:01:01     1  19.2
#> # … with 83 more rows

ggplot(wtr, aes(timestamp, y, z = value, fill = stat(level))) + 
    metR::geom_contour_fill(bins = 100, show.legend = FALSE) + 
    scale_fill_gradientn(colours = rainbow(101)) + 
    scale_y_continuous(NULL, breaks = NULL, expand = c(0, 0)) + 
    labs(x = NULL) + 
    theme(panel.grid = element_blank(), 
          panel.background = element_blank(),
          plot.margin = margin(20, 100, 200, 10))

ggplot 图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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