将 geom_text 与 substitute 结合使用无效
Combining geom_text with substitute doesn't work
我有以下数据集
structure(list(X = c(9.8186734, 19.6373468, 29.4560202, 39.2746936,
49.093367, 58.9120404, 68.7307138, 78.5493872, 88.3680606, 98.186734
), Y = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), radii = c(530.595715856625,
530.595715856625, 524.270569515141, 520.785212389348, 524.423046929159,
524.777454042683, 523.089321742221, 522.852371975715, 523.124870390148,
522.612174462367), slope = c(-21.796356958782, -21.796356958782,
-21.796356958782, -21.796356958782, -21.796356958782, -21.796356958782,
-21.796356958782, -21.796356958782, -21.796356958782, -21.796356958782
)), row.names = c(NA, -10L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x7f989f011ce0>, sorted = "Y")
我只是想将 slope
作为文本打印到图中作为
str_slope <- c(substitute("Slope = "~sp~mu*"m/s", list(sp = sprintf("%2.1f", dt[!duplicated(slope), slope]))))
d_text <- data.table(x=2000, y=500, label = str_slope)
ggplot(dt, aes(x = X, y=radii)) +
geom_point()+
geom_smooth(method = "lm", level = 0.9999, se = TRUE)+
scale_colour_manual(values = getPalette) +
labs(x = "time (s)", y = expression("radius ["*mu*"m]"), color = "Speed [µm/s]") +
geom_text(data = d_text, aes(x = x, y = y, label = label))+
theme_default(legend.position = "none")
但我得到了这样的东西
为什么 str_slope
中的文本未作为表达式求值?我如何强制 ggplot 将其解释为表达式,以便文本看起来像
对于这种类型的绘图注释,您应该使用 annotate(geom="text"...)
而不是 geom_text()
。关于如何生成表达式,可以使用 annotate()
.
中的 parse=TRUE
参数
我认为我们缺少您所有的绘图数据,所以这里有一个 mtcars
的示例,其中包含您示例中 str_slope
的内容。
str_slope <- c(substitute("Slope = "~sp~mu*"m/s", list(sp = sprintf("%2.1f", dt[!duplicated(slope), slope]))))
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p + annotate(
"text", x=4, y=25,
label= str_slope, parse=TRUE)
供您参考,geom_text()
设计用于将一种或多种美学映射到数据框。如果您只想在图中显示一行文本,则应使用 annotate(geom="text"...)
,当您 不想 将任何美学映射到数据时使用帧.
我有以下数据集
structure(list(X = c(9.8186734, 19.6373468, 29.4560202, 39.2746936,
49.093367, 58.9120404, 68.7307138, 78.5493872, 88.3680606, 98.186734
), Y = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), radii = c(530.595715856625,
530.595715856625, 524.270569515141, 520.785212389348, 524.423046929159,
524.777454042683, 523.089321742221, 522.852371975715, 523.124870390148,
522.612174462367), slope = c(-21.796356958782, -21.796356958782,
-21.796356958782, -21.796356958782, -21.796356958782, -21.796356958782,
-21.796356958782, -21.796356958782, -21.796356958782, -21.796356958782
)), row.names = c(NA, -10L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x7f989f011ce0>, sorted = "Y")
我只是想将 slope
作为文本打印到图中作为
str_slope <- c(substitute("Slope = "~sp~mu*"m/s", list(sp = sprintf("%2.1f", dt[!duplicated(slope), slope]))))
d_text <- data.table(x=2000, y=500, label = str_slope)
ggplot(dt, aes(x = X, y=radii)) +
geom_point()+
geom_smooth(method = "lm", level = 0.9999, se = TRUE)+
scale_colour_manual(values = getPalette) +
labs(x = "time (s)", y = expression("radius ["*mu*"m]"), color = "Speed [µm/s]") +
geom_text(data = d_text, aes(x = x, y = y, label = label))+
theme_default(legend.position = "none")
但我得到了这样的东西
为什么 str_slope
中的文本未作为表达式求值?我如何强制 ggplot 将其解释为表达式,以便文本看起来像
对于这种类型的绘图注释,您应该使用 annotate(geom="text"...)
而不是 geom_text()
。关于如何生成表达式,可以使用 annotate()
.
parse=TRUE
参数
我认为我们缺少您所有的绘图数据,所以这里有一个 mtcars
的示例,其中包含您示例中 str_slope
的内容。
str_slope <- c(substitute("Slope = "~sp~mu*"m/s", list(sp = sprintf("%2.1f", dt[!duplicated(slope), slope]))))
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p + annotate(
"text", x=4, y=25,
label= str_slope, parse=TRUE)
供您参考,geom_text()
设计用于将一种或多种美学映射到数据框。如果您只想在图中显示一行文本,则应使用 annotate(geom="text"...)
,当您 不想 将任何美学映射到数据时使用帧.