在geom_point中标记点 [英] Label points in geom_point

查看:791
本文介绍了在geom_point中标记点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的数据来自以下列出的互联网来源:

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

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

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

以下内容:



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

我也尝试了 text()函数和 textxy()库(校准)中的$ c>函数,它们都不会与ggplot一起使用。

如何为这些点添加名称标签? 使用 geom_text aes / code>标签。您可以使用 hjust,vjust 来调整文本位置。

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


编辑:只标记高于某个阈值的值:



<$ p $ (),
geom_point()+
geom_text(aes(aes(x = MIN,y = PTS,color =green,label = Name))+
ggplot label = ifelse(PTS> 24,as.character(Name),'')),hjust = 0,vjust = 0)


The data I'm playing with comes from the internet source listed below

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

What I want to do, is create a 2D points graph comparing two metrics from this table, with each player representing a dot 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:

What I want is a label of player's name right next to the dots. I thought the label function in ggplot's aesthetics would do this for me, but it didn't.

I also tried text() function and the textxy() function from library(calibrate), neither of which appears to work with ggplot.

How can I add name labels to these points?

解决方案

Use geom_text , with aes label. You can play with hjust, vjust to adjust text position.

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

EDIT: Label 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)

这篇关于在geom_point中标记点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆