我有以下資料集
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 將其解釋為運算式,以便文本看起來像
uj5u.com熱心網友回復:
對于這種型別的繪圖注釋,您應該使用annotate(geom="text"...)而不是geom_text(). 關于如何生成運算式,您可以parse=TRUE使用annotate().
我認為我們缺少您所有的繪圖資料,所以這里有一個示例,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"...),當您不想將任何美學映射到資料框時使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427656.html
上一篇:在時間序列圖上繪制PNG
