R:在 ggplot2 中将 guide/legend 的顺序从排序字符串更改为排序数字

R: Changing order of guide/legend from sorting strings to sorting numeric in ggplot2

我正在尝试使用 ggplot2 创建等值线图。我成功地使用 cut() 函数分块填充了 geom_tile() 函数的方块。使用这种方法,中断将被解释为字符串,而不是值,这给我设置指南的顺序带来了麻烦。这样,例如值 20 将被解释为大于 180 的数字,从而导致图中出现致命的误导性颜色,例如这个故意丑陋的颜色:

为了创建这个丑陋的图像,我使用了以下代码:

plot.isoline <- function(data, dbr=20, n_interp=200){

  library(akima)
  library(ggplot2)
  library(fields)

  data <- read.table(file = "http://s000.tinyupload.com/download.php?file_id=90687695741432763217&t=9068769574143276321730276", header = T)

  dint <- interp(x = data$x, y = data$y, z = data$z,
                 xo = seq(min(data$x), max(data$x), length = n_interp),
                 yo = seq(min(data$y), max(data$y), length = n_interp))

  dat_interp <- data.frame(expand.grid(x=dint$x,y=dint$y), z=c(dint$z))

  br <- seq(floor(min(dat_interp$z)/dbr)*dbr, ceiling(max(dat_interp$z)/dbr)*dbr, dbr)

  breaks <- cut(dat_interp$z, breaks = br)
  breaks <- gsub(","," - ",breaks,fixed=TRUE)

  dat_interp$breaks <- sapply(breaks, function(x){strsplit(x, "[( ]")[[1]][[2]]})

  ret <- ggplot(dat_interp) + 
    aes(x=x, y=y, z=z) +
    geom_tile(aes(fill=breaks)) +
    scale_fill_manual("TEMP", values=tim.colors(length(br)), guide = guide_legend(reverse=TRUE)) +
    stat_contour(breaks=br) +
    scale_x_continuous(expand=c(0,0)) +
    scale_y_continuous(expand=c(0,0))

  return(ret)        
}

我已经尝试通过以下方式将中断字符串设置为值:

dat_interp$breaks <- as.numeric(sapply(breaks, function(x){strsplit(x, "[( ]")[[1]][[2]]}))

这种方法只会导致错误 "Error: Continuous value supplied to discrete scale"。我确信必须有一种方法可以以数字方式订购比例尺,如果有人可以帮助我解决这个问题,我将非常高兴。

ggplot2character 值排序为字符串,factor 值按级别排序。在您的代码中,

breaks <- gsub(","," - ",breaks,fixed=TRUE)

cut 返回的因子转换为字符。尝试省略它。

如果您需要更改显示,请使用类似

的内容
levels(breaks) <- gsub(",", " - ", levels(breaks), fixed=TRUE)

在@krlmlr 的帮助下,我设法解决了这个问题。正如他所说,关键部分是编辑 levels()。对于任何感兴趣的人,这是重要的部分:

  breaks <- cut(dat_interp$z, breaks = br)
  levels(breaks) <- gsub(","," - ", levels(breaks),fixed=TRUE)
  dat_interp$breaks <- breaks
  levels(dat_interp$breaks) <- as.numeric(sapply(levels(breaks), function(x){strsplit(x, "[( ]")[[1]][[2]]}))

通过这种方式,我得到了以下正确排序的结果:

完整代码:

plot.isoline <- function(data, dbr=20, n_interp=200){

  library(akima)
  library(ggplot2)
  library(fields)

  data <- read.table(file = "http://s000.tinyupload.com/download.php?file_id=90687695741432763217&t=9068769574143276321730276", header = T)

  dint <- interp(x = data$x, y = data$y, z = data$z,
                 xo = seq(min(data$x), max(data$x), length = n_interp),
                 yo = seq(min(data$y), max(data$y), length = n_interp))

  dat_interp <- data.frame(expand.grid(x=dint$x,y=dint$y), z=c(dint$z))

  br <- seq(floor(min(dat_interp$z)/dbr)*dbr, ceiling(max(dat_interp$z)/dbr)*dbr, dbr)

  breaks <- cut(dat_interp$z, breaks = br)
  levels(breaks) <- gsub(","," - ", levels(breaks),fixed=TRUE)
  dat_interp$breaks <- breaks
  levels(dat_interp$breaks) <- as.numeric(sapply(levels(breaks), function(x){strsplit(x, "[( ]")[[1]][[2]]}))

  ret <- ggplot(dat_interp) + 
    aes(x=x, y=y, z=z) +
    geom_tile(aes(fill=breaks)) +
    scale_fill_manual("TEMP", values=tim.colors(length(br)), guide = guide_legend(reverse=TRUE)) +
    stat_contour(breaks=br) +
    scale_x_continuous(expand=c(0,0)) +
    scale_y_continuous(expand=c(0,0))

  return(ret)        
}