2015-09-22 108 views
1

我正嘗試使用ggplot2創建等值線圖。我使用cut()函數成功地填充了組中的geom_tile()函數的切片。使用這種方法,中斷將被解釋爲字符串,但不是作爲值,給我指導設置的順序帶來麻煩。這樣,例如20的值將被解釋爲比180更大的數量,從而在圖中,致命的誤導性的顏色,如此特意難看之一: Isoline plotR:更改排序字符串中指導/圖例的順序以排序ggplot2中的數字

爲了創建這個難看圖像,我用下面的代碼:

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]]})) 

這種做法只會導致一個錯誤「錯誤:提供給離散規模連續值」。我確信必須有一種方法來以數字方式排列比例,如果有人能幫我解決這個問題,我會很高興。

回答

1

隨着@krlmlr的有益輸入我設法解決這個問題。正如他所說,關鍵部分是編輯關卡()。任何有興趣,這是重要的部分:

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]]})) 

這樣,我得到以下結果,正確排序: Working result

而且全碼:

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)   
} 
1

ggplot2將按值排序character值爲字符串,factor值按其級別排序。在您的代碼中,

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

將由cut返回的因子轉換爲字符。嘗試省略它。

如果需要更改顯示,使用類似

levels(breaks) <- gsub(",", " - ", levels(breaks), fixed=TRUE) 
+0

但使用 後'dat_interp $ break < - sapply(break,function(x){strsplit(x,「[(]」)[[1]] [[2]]}) 沒有附加層次,對不對?我轉換這些字符串使用as.numeric()的值,仍然存在此錯誤消息(錯誤:提供給離散比例的連續值) – oepix

+1

@oepix:哦,我錯過了那條線。嘗試在'dat_interp'數據框中使用由'cut'返回的斷點,並在必要時僅修改級別。 (我認爲'strsplit'也可以使用'gsub'來實現。) – krlmlr

+0

好吧,這幫我弄明白了,非常感謝你@krlmlr! – oepix

相關問題