关于r:ggplot2在geom_raster中结合连续变量和离散变量 | 珊瑚贝

ggplot2 combine continous variable and discrete variable in geom_raster


我有一个包含两种元素的 100 x 100 矩阵。第一种是介于 0 和 100 之间的连续变量(实际上在程序中是离散的,但它们代表的是连续的,因此应该具有连续缩放的图例),另一种类型是具有三个可能值(-1、-2、 -3)。我在这个问题中使用了这个矩阵。

目标是制作连续变量的热图,同时区分具有离散负值的区域。目前,我正在使用带有 geom_raster 的 ggplot(请参阅此问题底部的代码片段)来绘制以下热图。

Current

但是,顶部和右侧的均匀灰色区域由负离散值组成,并且应该具有与图表其他部分不同的颜色/图案。例如,这些区域应该是白色的,并带有一个指示值的标签(参见第二张图片)。有没有办法用ggplot做到这一点?在理想情况下,该图将有一个用于连续范围的图例和一个用于三个离散值的指南。

额外问题:是否可以在边界处画一条线,即,如果矩阵中的下一个元素具有不同的值,则画一条线。现在我通过仅绘制许多段来手动执行此操作(参见第二个代码片段),但这不是要走的路(而且我没有成功将它与 ggplot 热图结合起来)。

enter

第一个代码片段(制作热图)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  minRate = 0;
  maxRate = 100;

  colnames(df) = NULL
  df = melt(df)
  colnames(df) = c(“col”,”row”,”value”)

  # geom_raster takes the center as input argument
  df[,”col”] = df[,”col”] – 0.5
  df[,”row”] = df[,”row”] – 0.5

  # Without labels
  ggplot(df, aes(x = col, y = row, fill = value)) +
    geom_raster() +
    theme_bw() +
    labs(fill=”Rate (%)”) +
    theme(plot.margin=unit(c(3,3,3,2),”mm”), legend.position =”right”) +
    scale_fill_gradient(low=”black”, high=”white”, limits=c(minRate, maxRate)) +
    scale_x_continuous(“state 1”, expand = c(0,0), limits=c(0,100)) +
    scale_y_continuous(“state 2”, expand = c(0,0), limits=c(0,100))

第二个代码片段(仅绘制边界):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
printPolicy <- function(df, title)
{
  n = nrow(df)

  plot(NA, xlim=c(0, n), ylim=c(0, n),
       xlab=”Machine 0″, ylab=”Machine 1″, main=title,
       las=1, yaxs=’i’, xaxs=’i’)

  for (x0 in 1:(n-1))
  {
    for (x1 in 1:(n-1))
    {
      # Horizontal lines
      if (df[x0, x1] != df[x0+1, x1])
        segments(x1-1, x0, x1, x0)

      # Vertical lines
      if (df[x0, x1] != df[x0, x1+1])
        segments(x1, x0-1, x1, x0)
    }
  }
}

  • 提供最小数据集怎么样?
  • 没有提供的实际矩阵很难提供帮助。但是由于您的所有数据都是数字,您可能可以使用 scale_fill_gradientn 手动定义停止和颜色值。
  • @jdobres 我添加了矩阵:)
  • 在您的栅格顶部添加 geom_contour()(指定 breaks)与解决您的奖励问题有多接近?


使用 ggnewscale 包相对容易做到这一点,请参见下面的示例。假设 dat 是 read.csv(the_data_you_posted).

的输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
library(ggplot2)
library(ggnewscale)
dat <- as.matrix(dat)
dimnames(dat) <- NULL

mdat <- reshape2::melt(dat)

conti <- mdat[mdat$value >= 0,]
discr <- mdat[mdat$value < 0,]

ggplot(mapping = aes(Var1, Var2)) +
  geom_raster(data = conti, aes(fill = value),
              hjust = 0, vjust = 0) +
  scale_fill_continuous() + # scale for continuous values need to be
                            # defined before call to  new_scale_fill()
  new_scale_fill() +
  geom_raster(data = discr, aes(fill = as.factor(value)),
              hjust = 0, vjust = 0)

enter

按照你认为合适的方式装饰情节。

很遗憾,我不知道您的额外问题的答案,但我很想知道是否有人有针对该问题的自动解决方案。

  • 如果我按原样运行您的代码段,我会收到以下错误:”isFALSE(scale$guide)中的错误:找不到函数”isFALSE”任何线索如何解决这个问题?
  • 这是一个已知问题:github.com/eliocamp/ggnewscale/issues/12。您可以下载该软件包的最新 github 版本,他们在其中反向移植了这些功能:github.com/eliocamp/ggnewscale/blob/master/R/utils.R。


来源:https://www.codenong.com/57376574/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8888.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?