我有一個具有多個值的單列資料框。我使用的是基本的 rplot 函式,如 plot() 和 points()。我成功繪制了線圖,但無法將資料框中的點值寫入繪圖欄位。無論如何要在圖上添加資料值嗎?
下面是測驗代碼
> x = data.frame(A = rnorm(10))
> plot(x$A, type = "o", pch = 20)**
抱歉,我進行了編輯以使我的問題更清楚。
下面是 10 個亂數的示例圖

uj5u.com熱心網友回復:
可能這超出了您的要求,但是您可以使用 ggplot 將標簽添加到線圖中的值:
library(ggplot2)
x = data.frame(A = rnorm(10),
pos = runif(10, 0.1, 0.7))
ggplot(x)
geom_point(aes(x = A),
y = 0)
geom_line(aes(x = A),
y = 0)
geom_segment(aes(x = A,
xend = A,
y = 0,
yend = pos),
linetype = 2)
geom_label(aes(x = A,
y = pos,
label = round(A, 2)),
size = 3)
scale_y_continuous(name = "",
limits = c(0, 0.8))
guides(y = "none")
theme_bw()

uj5u.com熱心網友回復:
將y設定為零,然后在plot之后使用文本:
#data
set.seed(1); x = data.frame(A = rnorm(10), y = 0)
#base plot
plot(x, type = "o", pch = 20, ylim = c(0, 1))
text(x = x$A, y = 0.2, labels = round(x$A, 2), srt = 90)

或者使用ggplot和ggrepel來制作漂亮的標簽:
#ggplot
library(ggplot2)
library(ggrepel) # pretty labels, avoid overlap:
ggplot(x, aes(A, y, label = round(A, 2)))
geom_point()
geom_label_repel()
#geom_text_repel()

uj5u.com熱心網友回復:
你可以制作一個基本的R“type b”等價物
library(ggh4x)
#> Loading required package: ggplot2
set.seed(1)
x = data.frame(A = rnorm(10))
ggplot(x, aes(1:10, A))
geom_pointpath(shape = NA)
geom_text(aes(label = round(A,2)))
labs(x= "Index")

由reprex 包于 2022-05-27 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482751.html
