可重現的 R 代碼
library(ggplot2)
library(metR)
v <- reshape2::melt(volcano)
g <- ggplot(v, aes(Var1, Var2))
geom_contour(aes(z = value), color = "white", size = 1.25,
breaks = c(100, 110, 120, 130, 140, 150, 160, 170, 180, 190))
geom_contour(aes(z = value, linetype = ifelse(value <= 130, "solid", "dash")),
color = "black", size = 0.45,
breaks = c(100, 110, 120, 130, 140, 150, 160, 170, 180, 190),
show.legend = F)
geom_text_contour(aes(z = value))
g
當我geom_contour用白色背景和另一個根據值覆寫不同線型geom_contour的ifelse函式時,繪圖程序顯示斷線,接近指定的 ifelse 值 130,如下圖所示。

有人可以推薦一個解決方案嗎?我懷疑這與插值方法有關。
再現的代碼而不在metR庫(用于繪制輪廓的標簽上面的曲線)
library(ggplot2)
v <- reshape2::melt(volcano)
g <- ggplot(v, aes(Var1, Var2))
geom_contour(aes(z = value), color = "white", size = 1.25,
breaks = c(100, 110, 120, 130, 140, 150, 160, 170, 180, 190))
geom_contour(aes(z = value, linetype = ifelse(value <= 130, "solid", "dash")),
color = "black", size = 0.45,
breaks = c(100, 110, 120, 130, 140, 150, 160, 170, 180, 190),
show.legend = F)
g
uj5u.com熱心網友回復:
總之,沒有辦法用函式ifelse成功使用geom.contour。正確的用法是分別繪制兩個輪廓。請注意,該錯誤純粹是由您的ifelse函式引起的,而不是由您在標題中假設的兩個圖重疊引起的。
library(reshape2)
library(ggplot2)
v <- reshape2::melt(volcano)
g <- ggplot(v, aes(Var1, Var2))
geom_contour(aes(z = value, linetype = "solid"),
color = "black", size = 0.45,
breaks = c(140, 150, 160, 170, 180, 190),
show.legend = F)
geom_contour(aes(z = value, linetype = "dashed"),
color = "black", size = 0.45,
breaks = c(100, 110, 120, 130),
show.legend = F)
g

但是,我仍然想知道是否有辦法通過該ifelse功能成功地做到這一點?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315895.html
