r - 如何用R标记散点图上的点?

标签 r scatter-plot

我是 R 新手,想知道如何在散点图上标记数据点。我尝试了以下代码,但出现错误。

x = c(102856,17906,89697,74384,91081,52457,73749,29910,75604,28267,122136,
      54210,48925,58937,76281,67789,69138,18026,90806,44893)
y = c(2818, 234, 2728, 2393, 2893, 1015, 1403, 791, 2243, 596, 2468, 1495,
      1232, 1746, 2410, 1791, 1706, 259, 1982, 836)

plot(x, y, main="Scatterplot ", xlab="xaxis ", ylab="yaxis ", pch=19)

names = c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R",
          "T","W","V","Y")

library(calibrate)
textxy(x, y, labs=names, cx = 0.5, dcol = "black", m = c(0, 0))

Error in text.default(X[posXposY], Y[posXposY], labs[posXposY], adj = c(-0.3,  :
plot.new has not been called yet

我不明白这个错误。请帮我

最佳答案

您可以在 ggplot2 中完成:

require(ggplot2)
d <- data.frame(x = c(102856,17906,89697,74384,91081,52457,73749,29910,75604,28267,122136, 54210,48925,58937,76281,67789,69138,18026,90806,44893), y = c(2818, 234, 2728, 2393, 2893, 1015, 1403, 791, 2243, 596, 2468, 1495, 1232, 1746, 2410, 1791, 1706, 259, 1982, 836), names = c("A","C","E","D","G","F","I","H","K","M","L","N","Q","P","S","R","T","W","V","Y"))
ggplot(d, aes(x,y)) + geom_point() + geom_text(aes(label=names))

您可能不希望文本标签直接位于点的顶部,这可以通过使用 hjust 来实现。或 vjust geom_text 中的参数部分。

关于r - 如何用R标记散点图上的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9500066/

相关文章:

r - 计算和显示 ggplot2::geom_密度() 对象峰值的最佳方法是什么?

r - 从原点到某个点的绘图箭头

r - 在绘图框中使用相同的坐标和相同的位置绘制两个 igraph 网络

matplotlib:重绘前清除散点数据

python - 散点图python双边缘线

python - matplotlib - 散点图,为 y 元组选择颜色

r - 如何在 R 中将文本拆分为两个有意义的词

python - Seaborn Jointplot 为每个类添加颜色

matlab - plot 和 scatter matlab 的区别

r - 从 R 中的第一次击键开始计时用户输入