programing

gem_point에 점 레이블 지정

oldcodes 2023. 7. 18. 21:57
반응형

gem_point에 점 레이블 지정

재생 중인 데이터는 아래 나열된 인터넷 소스에서 가져옵니다.

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()에서 기능.library(calibrate)둘 다 ggplot과 함께 작동하지 않습니다.

이 점에 이름 레이블을 추가하려면 어떻게 해야 합니까?

사용하다geom_text,와 함께aes레이블. 가지고 놀 수 있습니다.hjust, vjust텍스트 위치를 조정합니다.

ggplot(nba, aes(x= MIN, y= PTS, colour="green", label=Name))+
  geom_point() +geom_text(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

이 패키지는 서로 중복되는 텍스트 레이블을 제거하는 데 매우 유용합니다.다음 중 하나를 사용할 수 있습니다.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)

편집: 사용하기ggrepel선으로, 이것과 이것을 보세요.

reprex 패키지(v0.2.0)에 의해 2019-05-01에 생성되었습니다.

위의 예와 같이 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))

언급URL : https://stackoverflow.com/questions/15624656/label-points-in-geom-point

반응형