免责声明:R 新手。
你好!我正在尝试使用 ggtern 包绘制水化学 sample 的三元图。尝试运行以下代码会出现标题错误。
require(ggtern)
datos = read.csv("Li, B, Cl.csv", header = T, sep = ";", stringsAsFactors = FALSE)
names(datos) <- c("Muestra", "Li", "B", "Cl")
x11()
plot <- ggtern(data = datos, mapping = aes(x = as.numeric(datos[["Li"]]), y = as.numeric(datos[["Cl"]]), z = as.numeric(datos[["B"]]))) + geom_point()
plot
根据我的理解,有一些低级函数需要一个原子向量,但我给了 ggtern
作为输入,一个 data.frame (通过 str()
)。
一旦执行,脚本就不会绘制任何内容。我的数据格式如下,但以分号分隔:
Muestra Li B Cl
XYA3030 2.321334755 3.017842551 94.66082269
XEP3609 9.436334248 45.43581846 45.12784729
XEP3606_1 10.12604478 62.68726944 27.18668578
XEP3606_2 5.18367492 34.94305194 59.87327314
XEP3611 5.859786577 18.8098607 75.33035272
XEP3613 13.60173875 49.1191375 37.27912375
XEP3612 13.11960754 27.07316925 59.80722321
XEP3608 6.473636887 15.58523589 77.94112722
XEP3543 16.93515603 46.59573787 36.4691061
这是“dput(datos)”输出,如评论中所建议。
> dput(datos)
structure(list(Muestra = c("XYA3030", "XEP3609", "XEP3606_1",
"XEP3606_2", "XEP3611", "XEP3613", "XEP3612", "XEP3608", "XEP3543"
), Li = c(2.321334755, 9.436334248, 10.12604478, 5.18367492,
5.859786577, 13.60173875, 13.11960754, 6.473636887, 16.93515603
), B = c(3.017842551, 45.43581846, 62.68726944, 34.94305194,
18.8098607, 49.1191375, 27.07316925, 15.58523589, 46.59573787
), Cl = c(94.66082269, 45.12784729, 27.18668578, 59.87327314,
75.33035272, 37.27912375, 59.80722321, 77.94112722, 36.4691061
)), class = "data.frame", row.names = c(NA, -9L))
更新:尝试了我能想到的最简单的代码,但仍然没有成功。我错过了一些基本的东西吗?
library(ggtern)
datos = read.csv("Li_B_Cl.csv", header = T, sep = ";", stringsAsFactors = FALSE)
ggtern(data = datos, mapping = aes(x = Li, y = Cl, z = B)) + geom_point()
更新 2:清除 RStudio session 和软件包,重新安装 ggtern 和 ggplot2,然后运行简单的代码。没有运气。这是错误后的回溯:
> datos = read.csv("Li_B_Cl.csv", header = TRUE, sep = ";", stringsAsFactors = FALSE)
> ggtern(data = datos, mapping = aes(x = Li, y = Cl, z = B)) + geom_point()
Error: $ operator is invalid for atomic vectors
> traceback()
17: transform_position(data, panel_params$x$rescale, panel_params$y$rescale)
16: f(...)
15: self$super()$super()$transform(data, scale_details)
14: f(..., self = self)
13: self$transform(ex, scale_details)
12: .get.tern.extremes(self, list(x.range = self$limits$x, y.range = self$limits$y))
11: f(..., self = self)
10: self$coord$setup_panel_params(scale_x, scale_y, params = self$coord_params)
9: (function (scale_x, scale_y)
{
self$coord$setup_panel_params(scale_x, scale_y, params = self$coord_params)
})(dots[[1L]][[1L]], dots[[2L]][[1L]])
8: mapply(FUN = f, ..., SIMPLIFY = FALSE)
7: Map(setup_panel_params, scales_x, scales_y)
6: f(..., self = self)
5: layout$setup_panel_params()
4: ggplot_build.ggplot(x)
3: ggplot_build(x)
2: print.ggplot(x)
1: (function (x, ...)
UseMethod("print"))(x)
最佳答案
更改为aes
内的名称为我修复它:
library(ggtern)
ggtern(data = datos, mapping = aes(x = Li, y = Cl, z = B)) + geom_point()
一般来说,aes
使用名称(或符号),表示不带引号。有时这是不可取的,例如当您提前不知道名称或出于其他原因想要以编程方式执行此操作时。如果您有变量名称字符串,还可以这样做:
ggtern(data = datos, mapping = aes_string(x = "Li", y = "Cl", z = "B")) + geom_point()
(或 var1 <- "Li"
,然后 aes_string(x=var1, ...)
)。
还有其他rlang
-使用现状等做事的方式,不一定与您的问题相关。
要考虑的另一件事是,通常您引用的是变量而不是向量。在您的代码中,您有 x = as.numeric(datos[["Li"]])
它试图传递一个值向量。这看起来似乎很有道理,但实际上行不通。最好允许的原因之一ggplot2
(和 ggtern
,通过继承)管理数据就是做一些技巧,否则需要更多的外部/手动跟踪。例如,您可以让一个图层仅处理数据的一个子集:
ggtern(data = datos, mapping = aes(x = Li, y = Cl, z = B)) + geom_point() +
geom_point(color = "red", data = ~ subset(., grepl("XYA", Muestra)))
# the "." means "data as it exists so far" ^^^
(将一个点着色为“红色”,而无需自己处理子集化)。当您将数据传递到 ggplot(data=...)
时,这尤其有用或ggplot(data=...)
已预先过滤和/或位于管道末端,否则您必须在图层中重新创建数据。所以......使用名称/符号,不要尝试使用向量。
关于r - 错误: $ operator is invalid for atomic vectors - ggtern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60609292/