在geom_point中标记点

时间:2013-03-25 21:00:05

标签: r plot ggplot2 labeling ggrepel

我正在玩的数据来自下面列出的互联网资源

nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv", sep=",")

我想要做的是创建一个2D点图,比较此表中的两个指标,每个玩家在图表上代表一个点。我有以下代码:

nbaplot <- ggplot(nba, aes(x= MIN, y= PTS, colour="green", label=Name)) + 
                  geom_point() 

这给了我以下内容:

NBA Plot

我想要的是一个玩家名字的标签就在点的旁边。我认为ggplot美学中的标签功能会为我做这个,但事实并非如此。

我还尝试了text()函数和来自textxy()的{​​{1}}函数,这些函数似乎都不适用于ggplot。

如何为这些点添加名称标签?

3 个答案:

答案 0 :(得分:224)

使用带有geom_text标签的aes。您可以使用hjust, vjust来调整文字位置。

ggplot(nba, aes(x= MIN, y= PTS, colour="green", label=Name))+
  geom_point() +geom_text(aes(label=Name),hjust=0, vjust=0)

enter image description here

编辑:仅标记高于特定阈值的值:

  ggplot(nba, aes(x= MIN, y= PTS, colour="green", label=Name))+
  geom_point() +
  geom_text(aes(label=ifelse(PTS>24,as.character(Name),'')),hjust=0,vjust=0)

chart with conditional labels

答案 1 :(得分:50)

ggrepel包非常适合排斥重叠的文本标签。您可以使用geom_label_repel()(在文本周围绘制矩形)或geom_text_repel()函数。

library(ggplot2)
library(ggrepel)

nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv", sep = ",")

nbaplot <- ggplot(nba, aes(x= MIN, y = PTS)) + 
  geom_point(color = "blue", size = 3)

### geom_label_repel
nbaplot + 
  geom_label_repel(aes(label = Name),
                  box.padding   = 0.35, 
                  point.padding = 0.5,
                  segment.color = 'grey50') +
  theme_classic()

enter image description here

### geom_text_repel
# only label players with PTS > 25 or < 18
# align text vertically with nudge_y and allow the labels to 
# move horizontally with direction = "x"
ggplot(nba, aes(x= MIN, y = PTS, label = Name)) + 
  geom_point(color = dplyr::case_when(nba$PTS > 25 ~ "#1b9e77", 
                                      nba$PTS < 18 ~ "#d95f02",
                                      TRUE ~ "#7570b3"), 
             size = 3, alpha = 0.8) +
  geom_text_repel(data          = subset(nba, PTS > 25),
                  nudge_y       = 32 - subset(nba, PTS > 25)$PTS,
                  size          = 4,
                  box.padding   = 1.5,
                  point.padding = 0.5,
                  force         = 100,
                  segment.size  = 0.2,
                  segment.color = "grey50",
                  direction     = "x") +
  geom_label_repel(data         = subset(nba, PTS < 18),
                  nudge_y       = 16 - subset(nba, PTS < 18)$PTS,
                  size          = 4,
                  box.padding   = 0.5,
                  point.padding = 0.5,
                  force         = 100,
                  segment.size  = 0.2,
                  segment.color = "grey50",
                  direction     = "x") +
  scale_x_continuous(expand = expand_scale(mult = c(0.2, .2))) +
  scale_y_continuous(expand = expand_scale(mult = c(0.1, .1))) +
  theme_classic(base_size = 16)

reprex package(v0.2.0)于2019-05-01创建。

答案 2 :(得分:6)

除了使用上面示例中的ifelse之外,还可以在标记之前根据某些阈值预先过滤数据,这为绘图设备节省了大量工作:

xlimit <- 36
ylimit <- 24
ggplot(myData)+geom_point(aes(myX,myY))+
    geom_label(data=myData[myData$myX > xlimit & myData$myY> ylimit,], aes(myX,myY,myLabel))