![](/img/trans.png)
[英]Sort matrix of points in R to get a sequential vector for plotting the line on x-y plot
[英]x-y scatter-plot in r with labels on points
我正在嘗試制作 xy 散點圖。 我不介意它是在 plot 還是 ggplot2 中。 我對每一個都不太了解,但如果你不介意的話,我想在這兩個方面舉個例子。 我想要一個 label 就點。
以下是代碼和輸入:
tickers <- rownames(x2)
library(zoo)
plot(x2,
main= "Vol vs Div",
xlab= "Vol (in %)",
ylab= "Div",
col= "blue", pch = 19, cex = 1, lty = "solid", lwd = 2)
text(x=x2$Volatility101,y=x2$`12m yield`, labels=tickers,cex= 0.7, pos= 3)
x2:
structure(list(Volatility101 = c(25.25353177644, 42.1628734949414,
28.527736824123), `12m yield` = c("3.08", "7.07", "4.72")), class = "data.frame", row.names = c("EUN",
"HRUB", "HUKX"))
這是一個tidyverse
的解決方案。
library(ggplot2)
library(tidyr)
library(dplyr)
library(ggrepel)
x2 %>%
rownames_to_column(var = "tickers") %>%
ggplot(aes(x = Volatility101, y = `12m yield`)) +
geom_point(color = "blue") +
geom_text_repel(aes(label = tickers)) +
ggtitle("Vol vs Div") +
xlab("Vol (in %)") +
ylab("Div") +
theme_classic()
我很驚訝plot
function 完全有效。 Y 值是字符值。 在文本調用中修復該問題會導致文本被放置在預期的位置
text(x=x2$Volatility101,y=as.numeric(x2$`12m yield`)+.1, labels=tickers,
cex= 0.7, col='black')
關於問題介紹的幾點說明:尚不清楚(且具有誤導性)為什么 ggplot2 是一個標簽。 plot
function 是通用的,在這種情況下,它使用基本圖形而不是ggplot2
或更普遍的grid
圖形。 我也認為library(zoo)
調用可能是不必要的。 有一個plot.zoo
function,但在這種情況下不會被調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.