Label points in geom_point

The data I play with is taken from the Internet source indicated below

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

What I want to do is create a two-dimensional graph of points, comparing the two metrics from this table, where each player represents a point on the graph. I have the following code:

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

This gives me the following:

Nba plot

What I want is a shortcut with the name of the player next to the dots. I thought the label function in ggplot aesthetics would do this for me, but it is not.

I also tried the text() function of the textxy() text() function from library(calibrate) , none of which work with ggplot.

How can I add shortcuts to these points?

+138
r plot ggplot2 ggrepel
Mar 25 '13 at 21:00
source share
3 answers

Use geom_text , labeled aes . You can play with hjust, vjust to adjust the position of the text.

 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

EDIT: mark only values ​​above a certain threshold:

  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

+223
Mar 25 '13 at 21:30
source share

The ggrepel package for pushing overlapping text labels from each other. You can use the geom_label_repel() function (draws rectangles around the text) or the geom_text_repel() function.

 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) 

Created on 2019-05-01 by view package (v0.2.0).

+46
Feb 13 '18 at 8:26
source share

Instead of using ifelse, as in the above example, you can also pre-filter the data before marking based on some threshold values, which saves a lot of work for the build device:

 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)) 
+6
Jul 15 '16 at 19:31
source share



All Articles