使用geom_contour_filled手动设置等高线图的比例
我想手动调整两个等高线图的比例,使每个等高线图具有相同的比例,即使它们在z方向上包含不同的值范围。 例如,假设我想绘制z1和z2的等高线图:使用geom_contour_filled手动设置等高线图的比例,r,ggplot2,scale,contour,R,Ggplot2,Scale,Contour,我想手动调整两个等高线图的比例,使每个等高线图具有相同的比例,即使它们在z方向上包含不同的值范围。 例如,假设我想绘制z1和z2的等高线图: x = 1:15 y = 1:15 z1 = x %*% t(y) z2 = 50+1.5*(x %*% t(y)) data <- data.frame( x = as.vector(col(z1)), y = as.vector(row(z1)), z1 = as.vector(z1), z2 = as.vector(z2) )
x = 1:15
y = 1:15
z1 = x %*% t(y)
z2 = 50+1.5*(x %*% t(y))
data <- data.frame(
x = as.vector(col(z1)),
y = as.vector(row(z1)),
z1 = as.vector(z1),
z2 = as.vector(z2)
)
ggplot(data, aes(x, y, z = z1)) +
geom_contour_filled(bins = 8)
ggplot(data, aes(x, y, z = z2)) +
geom_contour_filled(bins = 8)
是否有办法手动调整每个图的比例,使每个图包含相同数量的标高(在本例中,bins=8),最小值与最小值相同(在本例中,minz1),最大值与最大值相同(在两个maxz2) 可以手动定义所需断点的向量,然后将该向量传递给geom_contour_filled函数中的breaks选项 在下面的脚本中,查找数据集的最大最小值和最大值之间的8个中断间隔 此外,还定义了两个函数来创建图例的调色板和标签名称
#establish the min and max of scale
grandmin <- min(z1, z2)-1
grandmax <- max(z2, z2)
#define the number of breaks. In this case 8 +1
mybreaks <- seq(grandmin, ceiling(round(grandmax, 0)), length.out = 9)
#Function to return the dersired number of colors
mycolors<- function(x) {
colors<-colorRampPalette(c("darkblue", "yellow"))( 8 )
colors[1:x]
}
#Function to create labels for legend
breaklabel <- function(x){
labels<- paste0(mybreaks[1:8], "-", mybreaks[2:9])
labels[1:x]
}
ggplot(data, aes(x, y, z = z1)) +
geom_contour_filled(breaks= mybreaks, show.legend = TRUE) +
scale_fill_manual(palette=mycolors, values=breaklabel(8), name="Value", drop=FALSE) +
theme(legend.position = "right")
ggplot(data, aes(x, y, z = z2)) +
geom_contour_filled(breaks= mybreaks, show.legend = TRUE) +
scale_fill_manual(palette=mycolors, values=breaklabel(8), name="Value", drop=FALSE)
可以手动定义所需断点的向量,然后将该向量传递给geom_contour_filled函数中的breaks选项 在下面的脚本中,查找数据集的最大最小值和最大值之间的8个中断间隔 此外,还定义了两个函数来创建图例的调色板和标签名称
#establish the min and max of scale
grandmin <- min(z1, z2)-1
grandmax <- max(z2, z2)
#define the number of breaks. In this case 8 +1
mybreaks <- seq(grandmin, ceiling(round(grandmax, 0)), length.out = 9)
#Function to return the dersired number of colors
mycolors<- function(x) {
colors<-colorRampPalette(c("darkblue", "yellow"))( 8 )
colors[1:x]
}
#Function to create labels for legend
breaklabel <- function(x){
labels<- paste0(mybreaks[1:8], "-", mybreaks[2:9])
labels[1:x]
}
ggplot(data, aes(x, y, z = z1)) +
geom_contour_filled(breaks= mybreaks, show.legend = TRUE) +
scale_fill_manual(palette=mycolors, values=breaklabel(8), name="Value", drop=FALSE) +
theme(legend.position = "right")
ggplot(data, aes(x, y, z = z2)) +
geom_contour_filled(breaks= mybreaks, show.legend = TRUE) +
scale_fill_manual(palette=mycolors, values=breaklabel(8), name="Value", drop=FALSE)
任何绕过此问题的方法都会抛出警告:警告消息:1:In if n>lengthvalues{:条件的长度大于1,并且只使用第一个元素?可能不值得一个全新的问题,但是当使用原始海报生成的数据在上述代码中调用ggplot时,会出现警告。在ggplot2 v.3.3.2上,R版本4.0.2 2020-06-22@ltronneberg,则警告将从该行颜色[1:x]生成在mycolor函数中。对不起,我没有一种简单的方法来抑制警告。任何绕过此问题的方法都会抛出警告:警告消息:1:in if n>lengthvalues{:条件的长度大于1,并且只使用第一个元素?可能不值得一个全新的问题,但是当使用原始海报生成的数据在上述代码中调用ggplot时,会出现警告。在ggplot2 v.3.3.2上,R版本4.0.2 2020-06-22@ltronneberg,则警告将从该行颜色[1:x]生成在mycolor函数中。对不起,我没有一个简单的方法来抑制警告。