我有一組產品分數,我想分散顯示。我想要做的是讓標簽拉到圖表的四個獨立角落。實際上,我有 30-40 的空間,這就是我使用 ggrepel 作為標簽的原因。
`
library(ggplot2)
peer_score <- c(3.5, 3.5, 2, 2 )
mkt_score <- c(3.5, 2, 3.5, 2)
revenue <- sample(1:75, 4, replace = T)
prod_name <- c("Product 1", "Product 2", "Product 3", "Product 4")
df <- data.frame(prod_name, revenue, peer_score, mkt_score)
head(df)
ggplot(df, aes(peer_score, mkt_score))
geom_point(color = "blue", alpha=0.5)
geom_label_repel(aes(label=prod_name))
geom_vline(xintercept=3, linetype="dashed", alpha = 0.9, colour = "dark grey")
geom_hline(yintercept=3, linetype="dashed", alpha = 0.9, colour = "dark grey")
xlim(1,5)
ylim(1,5)
`
所以我希望將“產品 1”拉到右上角,將“產品 2”拉到右下角,等等

我曾嘗試使用 nudge_x 或 nudge_y 但這會向同一方向發送標簽。任何建議都非常感謝!
uj5u.com熱心網友回復:
不確定這是否適用于您的實際情況。但至少對于您的示例資料,一個選項是使用例如一個geom_label_repel用于四個象限中的每一個,這將允許單獨設定輕推和對齊。
library(ggplot2)
library(ggrepel)
nudge <- .1
ggplot(df, aes(peer_score, mkt_score))
geom_point(color = "blue", alpha = 0.5)
geom_label_repel(data = subset(df, peer_score < 3 & mkt_score < 3), aes(label = prod_name), nudge_x = -nudge, nudge_y = -nudge, hjust = 1, vjust = 1)
geom_label_repel(data = subset(df, peer_score < 3 & !mkt_score < 3), aes(label = prod_name), nudge_x = -nudge, nudge_y = nudge, hjust = 1, vjust = 0)
geom_label_repel(data = subset(df, !peer_score < 3 & mkt_score < 3), aes(label = prod_name), nudge_x = nudge, nudge_y = -nudge, hjust = 0, vjust = 1)
geom_label_repel(data = subset(df, !peer_score < 3 & !mkt_score < 3), aes(label = prod_name), nudge_x = nudge, nudge_y = nudge, hjust = 0, vjust = 0)
geom_vline(xintercept = 3, linetype = "dashed", alpha = 0.9, colour = "dark grey")
geom_hline(yintercept = 3, linetype = "dashed", alpha = 0.9, colour = "dark grey")
xlim(1, 5)
ylim(1, 5)

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/520910.html
標籤:rggplot2几何文本
