我需要将 ggplot 图的 x 轴更改为立方根比例,而不转换原始数据。我下面的代码一直有效,但是随着新的 R 更新,我收到错误,
if (zero_range(as.numeric(transformation$transform(limits)))) { 中的错误: 需要 TRUE/FALSE 时缺少值
我想知道是否有人知道为什么现在会发生此错误和/或解决方案/解决方法来在立方根刻度上绘制我的图形。谢谢!
前几行数据:
structure(list(site = c("US", "US", "US", "US", "US", "US"),
Transect = c(1L, 1L, 1L, 1L, 1L, 1L), Plot = c(100L, 103L,
106L, 109L, 112L, 115L), Year = c(2020L, 2020L, 2020L, 2020L,
2020L, 2020L), SYear = c("2020-2021", "2020-2021", "2020-2021",
"2020-2021", "2020-2021", "2020-2021"), E.Delta = c(-0.0380000000000109,
-0.0930000000000746, -0.0950000000000273, -0.0709999999999127,
-0.104000000000042, -0.0989999999999327), I = c(1, 1, 1,
1, 1, 1), AllRtedPctCov = c(0, 0, 0, 0, 0, 0), NoRtedVeg = c(1,
1, 1, 1, 1, 1), PerCov.15 = structure(c(1L, 1L, 1L, 1L, 1L,
1L), levels = c("0", "1"), class = "factor")), row.names = c(NA,
6L), class = "data.frame")
**我的代码:**
transform_cuberoot = function(){
trans_new('cuberoot',
transform = function(x) x^(1/3),
inverse = function(x) x^3,
breaks = c(0, 0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0),
domain = c(-Inf, Inf))
}
ggplot(df, aes(x = I, y = E.Delta, color = PerCov.15)) +
geom_point(size = 1.5) +
facet_wrap(~site + SYear, scales = "free") +
theme_classic() +
geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
labs(x = "Inundation-Duration", y = "Change in Elevation", color =" Percent Rooted Cover") +
scale_colour_brewer(labels= c("<15%", ">15%"), type = "qual", palette = 3) + # qual 3 or seq 1
theme(axis.title.x = element_text(size = 16, vjust = -2),
axis.title.y = element_text(size = 16, vjust = 5),
legend.title = element_text(size = 16),
legend.text = element_text(size = 16),
strip.background = element_blank(),
strip.text = element_text(size = 18),
axis.text = element_text(size = 14),
plot.margin = unit(c(2,2,2,2), "cm"),
panel.spacing = unit(3, "lines"))+
scale_x_continuous(trans = transform_cuberoot(), breaks = c(0, 0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0),labels=c(0, 0.01, 0.1, " ", 0.3, " ", " ", 0.6, " ", " ", " ", 1.0))
我也尝试过使用解决方法,
coord_trans(x = transform_cuberoot())
而不是
trans = transform_cuberoot()
中的 scale_x_continuous
参数。这可以工作,但会删除其他重要的绘图功能,例如 y = 0 处的虚线和 y 轴线。它还会产生 18 条警告:
在transform(..., self = self)中: 变换在 x 轴上引入了无限值
请参阅下面的示例,了解我正在寻找的图形与我通过解决方法能够生成的图形。
我想要/尝试重新创建(在 0 处水平线并为每个站点包裹):
我目前的解决方法:
您可以通过将立方根变换的域限制为非负值来解决此问题,即使用
domain=c(0, Inf)
。
注意:正如 @BenBolker 已经指出的那样,使用示例数据无法重现该问题,因此我将其更改为包含零值。经过一些实验后,仅当映射到
x
的变量包含一些小的、接近零的值时,才会出现问题。由此我可以猜测这与比例的(默认)扩展有关,即使用 expand = c(0, 0)
也可以正常工作。
library(ggplot2)
library(scales)
df$I <- c(0.0, 0.01, 0.1, 0.8, 0.9, 1.0)
transform_cuberoot <- function() {
trans_new("cuberoot",
transform = function(x) x^(1 / 3),
inverse = function(x) x^3,
domain = c(0, Inf)
)
}
ggplot(df, aes(x = I, y = E.Delta, color = PerCov.15)) +
geom_point(size = 1.5) +
facet_wrap(~ site + SYear, scales = "free") +
theme_classic() +
geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
labs(
x = "Inundation-Duration", y = "Change in Elevation",
color = " Percent Rooted Cover"
) +
scale_colour_brewer(
labels = c("<15%", ">15%"),
type = "qual", palette = 3
) + # qual 3 or seq 1
scale_x_continuous(
transform = transform_cuberoot(),
breaks = c(0, 0.01, 0.1, 0.3, 0.6, 1.0)
)
仅供参考,这里我使用
domain = c(-Inf, Inf)
重现该问题:
transform_cuberoot <- function() {
trans_new("cuberoot",
transform = function(x) x^(1 / 3),
inverse = function(x) x^3,
domain = c(-Inf, Inf)
)
}
ggplot(df, aes(x = I, y = E.Delta, color = PerCov.15)) +
geom_point(size = 1.5) +
facet_wrap(~ site + SYear, scales = "free") +
theme_classic() +
geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
labs(
x = "Inundation-Duration", y = "Change in Elevation",
color = " Percent Rooted Cover"
) +
scale_colour_brewer(
labels = c("<15%", ">15%"),
type = "qual", palette = 3
) + # qual 3 or seq 1
scale_x_continuous(
transform = transform_cuberoot(),
breaks = c(0, 0.01, 0.1, 0.3, 0.6, 1.0)
)
#> Error in if (zero_range(as.numeric(transformation$transform(limits)))) {: missing value where TRUE/FALSE needed