R中的image.plot不显示色标边缘的色阶值

时间:2017-02-10 01:20:13

标签: r plot

[Here top images is output from R 3.2.5, same code produces bottom image in R 2.15.2, notice the diff in color scale labels on the right side. I want to resolve the 'shift' of labels in the top plot. Sample code given in this query was used to generate both plots.

注意两个图像上的颜色比例标签中的差异。顶部图像是R 3.2.5的输出,相同的代码在R 2.15.2中生成底部图像。我想解决“转变”问题。顶部图中的标签与底部标签相匹配。此查询中给出的示例代码用于生成两个图。

我正在尝试使用image.plot绘制地图,但颜色刻度的最小值和最大值并未完全显示在提示处。我在R版本3.2.5(2016-04-14)中遇到了这个问题;平台:x86_64-w64-mingw32 / x64(64位)和库包'字段'垃圾邮件版本1.4-0(2016-08-29)

相比之下,相同的命令可以在R版本2.15.2(2012-10-26),平台:x86_64-w64-mingw32 / x64(64-)中正确显示色标边缘的最小值和最大值位)和'字段'包0.41-0(2014-02-26)。以下是示例代码:

library(fields)
temp <- matrix(data=rexp(200, rate=10), nrow=180, ncol=360)
min(temp)
max(temp)
color_plate <- c("#FF0000", "#FF4D00", "#FF7000", "#FF8A00", "#FFA800", "#FFBF00", "#FFF000", "#FFFF54", "#AAFFFF","#7FFFFF", "#55FFFF", "#2AFFFF", "#00CFFF", "#20BFFF", "#209FFF", "#2060FF")

zlim <- seq(0.08,0.40,by=0.04)
temp[temp<min(zlim)] <- min(zlim)
temp[temp>max(zlim)] <- max(zlim)

image.plot(temp,col=color_plate, 
           axis.args=list(cex.axis =1,at=zlim, labels=zlim,mgp=c(1, 0, 0),tck=0.1))

2 个答案:

答案 0 :(得分:6)

我能回答这个问题的最好方法是逃避image.plot()的限制并重新编码ggplot2中的热图。我写的代码应该将您的刻度线重新定位到适当的位置。请注意,"Var1"对象"Var2"中的ggplotp可以根据您希望数据显示的方式进行切换。我使用melt()转换temp对象,这意味着原始行/列指定丢失。我不确定在image.plot()函数的x / y轴上绘制了哪个,所以如果我选错了,请务必切换"Var1""Var2"

我希望这有帮助!

library(fields)
library(reshape2)
library(ggplot2)
library(grid)

temp <- matrix(data=rexp(200, rate=10), nrow=180, ncol=360)

color_palette <- c("#FF0000", "#FF4D00", "#FF7000", "#FF8A00",
        "#FFA800", "#FFBF00", "#FFF000", "#FFFF54", "#AAFFFF","#7FFFFF",
        "#55FFFF", "#2AFFFF", "#00CFFF", "#20BFFF", "#209FFF", "#2060FF")

zlim <- seq(0.08,0.40,by=0.04)
zlim2 <- seq(0.08,0.40,by=0.02)

temp[temp<min(zlim)] <- min(zlim)
temp[temp>max(zlim)] <- max(zlim)
rownames(temp) <- seq(0,1,1/(length(temp[,1])-1))
colnames(temp) <- seq(0,1,1/(length(temp[1,])-1))

tdm <- melt(temp)

tdm$val_for_color <- NA
##can change this as long as you end up with 17 classes (labeled 1-17) for color assignment
for(i in 1:(length(zlim2)-1)){
    tdm$val_for_color[which(tdm$value >= zlim2[[i]] & tdm$value <= zlim2[[i+1]] )] <- i
}

p <- ggplot(tdm, aes(x = Var1, y = Var2, fill = val_for_color)) +
        geom_raster() + scale_fill_gradientn(breaks=seq(1,length(zlim),1),colors=color_palette, labels=zlim)+ 
        scale_x_continuous(expand=c(0,0)) +
        scale_y_continuous(expand=c(0,0)) +
        guides(fill = guide_colorbar(draw.ulim = TRUE,draw.llim = FALSE,
                        barwidth = 0.7, barheight = 10, limits=c(min(zlim),max(zlim)), raster=FALSE,
                        ticks=FALSE,
                        title=NULL))+
        ylab(NULL)+
        xlab(NULL)+
        theme_bw()

g <- ggplotGrob(p)

#this shifts and spreads the labels
d <-g$grobs[[15]][[1]][[1]]$grobs[[3]]$y[[1]]
g$grobs[[15]][[1]][[1]]$grobs[[3]]$y[[1]] <- g$grobs[[15]][[1]][[1]]$grobs[[3]]$y[[1]]-d
for(i in 2:length(g$grobs[[15]][[1]][[1]]$grobs[[3]]$y)){
    g$grobs[[15]][[1]][[1]]$grobs[[3]]$y[[i]] <- d*5*(i-1)
}

grid.draw(g)

答案 1 :(得分:4)

我从字段包的包创建者那里得到了答案。将示例代码粘贴给其他人..

library(fields)

temp <- matrix( seq( 0,.5,,80), 8,10)

colTab <- c("#FF0000", "#FF4D00","#FF7000", "#FF8A00", "#FF7000")

N<- length( colTab)

breaks <- seq(0.08, 0.40,  length.out= N+1 )

image.plot(temp, col=colTab, breaks=breaks,
           axis.args=list(cex.axis =1, at=breaks, labels= breaks, mgp=c(1, 0, 0), tck=0.1)
     )