Ggplot2의 geom_vline에 라벨을 추가하는 방법


다음 기본 구문을 사용하여 ggplot2의 수직선에 레이블을 추가할 수 있습니다.

 + annotate(" text ", x= 9 , y= 20 , label=" Here is my text ", angle= 90 )

다음 예에서는 이 구문을 실제로 사용하는 방법을 보여줍니다.

예시 1: geom_vline에 라벨 추가

다음 코드는 ggplot2의 수직선에 레이블을 추가하는 방법을 보여줍니다.

 library (ggplot2)

#create data frame 
df <- data. frame (x=c(1, 3, 3, 4, 5, 5, 6, 9, 12, 15),
                 y=c(13, 14, 14, 12, 17, 21, 22, 28, 30, 31))

#create scatterplot with vertical line at x=10
ggplot(df, aes(x=x, y=y)) +
geom_point() +
  geom_vline(xintercept= 10 ) +
  annotate("text", x= 9.7 , y= 20 , label=" Some text ", angle= 90 ) 

예 2: geom_vline에 맞춤 라벨 추가

다음 코드는 크기색상 인수를 사용하여 ggplot2의 수직선에 사용자 정의 크기 및 색상이 있는 레이블을 추가하는 방법을 보여줍니다.

 library (ggplot2)

#create data frame 
df <- data. frame (x=c(1, 3, 3, 4, 5, 5, 6, 9, 12, 15),
                 y=c(13, 14, 14, 12, 17, 21, 22, 28, 30, 31))

#create scatterplot with vertical line at x=10
ggplot(df, aes(x=x, y=y)) +
geom_point() +
  geom_vline(xintercept= 10 ) +
  annotate("text", x= 9 , y= 20 , label=" Some text ", angle= 90 , size= 15 , color=" blue ") 

예시 3: geom_vline에 여러 라벨 추가

다음 코드는 annotate() 함수를 여러 번 사용하여 ggplot2의 수직선에 여러 레이블을 추가하는 방법을 보여줍니다.

 library (ggplot2)

#create data frame 
df <- data. frame (x=c(1, 3, 3, 4, 5, 5, 6, 9, 12, 15),
                 y=c(13, 14, 14, 12, 17, 21, 22, 28, 30, 31))

#create scatterplot with vertical line at x=10
ggplot(df, aes(x=x, y=y)) +
geom_point() +
  geom_vline(xintercept= 10 ) +
  annotate("text", x= 9 , y= 20 , label=" Some text ", angle= 90 , size= 15 , color=" blue ") +
  annotate("text", x= 11 , y= 20 , label=" More text ", angle= 90 , size= 13 , color=" red ") 

플롯에 원하는 만큼 많은 레이블을 추가하려는 만큼 annotate() 함수를 자유롭게 사용하십시오.

추가 리소스

다음 튜토리얼에서는 ggplot2에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.

ggplot2에서 선형 회귀선을 그리는 방법
ggplot2에서 축 제한을 설정하는 방법
ggplot2에서 병렬 플롯을 만드는 방법

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다