A: 경로에 원을 그리는 방법: 예제 포함
다음 방법을 사용하여 R의 경로에 원을 그릴 수 있습니다.
방법 1: Base R을 사용하여 원 그리기
library (plotrix)
#create scatterplot
plot(x, y)
#add circle at specific (x, y) coordinates with specific radius
draw.draw. circle (x=3, y=8, radius=.5)
방법 2: ggplot2를 사용하여 원 그리기
library (ggplot2) library (ggforce) #create scatter plot with circle at specific location with specific radius ggplot(data = df, aes(x, y)) + geom_point() + geom_circle(aes(x0=3, y0=8, r=1), inherit. aes = FALSE ) + coordinate_fixed()
다음 예에서는 각 방법을 실제로 사용하는 방법을 보여줍니다.
예 1: Base R을 사용하여 원 그리기
기본 R 플롯에 원을 그리려면 먼저 플롯릭스 패키지를 설치하고 로드해야 합니다.
install. packages (' plotrix ')
library (plotrix)
다음으로, 플롯릭스 패키지의 draw.circle() 함수를 사용하여 기본 R 포인트 클라우드에 원을 추가할 수 있습니다.
#create data frame
df <- data. frame (x=c(1, 2, 2, 3, 3, 4, 8),
y=c(2, 4, 5, 4, 7, 9, 10))
#create scatterplot
plot(df$x, df$y)
#add circle
draw.draw. circle (x=3, y=8, radius=.5)
draw.circle() 함수를 여러 번 사용하여 동일한 경로에 여러 개의 원을 그릴 수도 있습니다.
#create data frame
df <- data. frame (x=c(1, 2, 2, 3, 3, 4, 8),
y=c(2, 4, 5, 4, 7, 9, 10))
#create scatterplot
plot(df$x, df$y)
#add multiple circles to plot
draw.draw. circle (x=3, y=8, radius=.5)
draw.draw. circle (x=4, y=5, radius=.5, border=' red ', col=' lightblue ', lwd=5, lty=' dashed ')
우리가 지정한 (x, y) 좌표의 플롯에 여러 개의 원이 추가되었습니다.
예 2: ggplot2를 사용하여 원 그리기
ggplot2의 플롯에 원을 그리려면 먼저 ggplot2 및 ggforce 패키지를 설치하고 로드해야 합니다.
install. packages (' ggplot2 ')
install. packages (' ggforce ')
library (ggplot2)
library (ggforce)
다음으로 ggplot2의 산점도에 원을 추가하기 위해 ggforce 패키지의 geom_circle() 함수를 사용할 수 있습니다.
#create data frame
df <- data. frame (x=c(1, 2, 2, 3, 3, 4, 8),
y=c(2, 4, 5, 4, 7, 9, 10))
#create scatter plot with circle
ggplot(data = df, aes(x, y)) +
geom_point() +
geom_circle(aes(x0=3, y0=8, r=1), linetype=' dashed ', color=' red ',
fill=' lightblue ', lwd=1.5, inherit. aes = FALSE ) +
coordinate_fixed()
원은 우리가 지정한 정확한 좌표(x, y)에 배치됩니다.
참고 : coord_fixed() 인수를 사용하지 않으면 원이 타원으로 나타날 수 있습니다.
추가 리소스
다음 튜토리얼에서는 R에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.
여러 변수를 사용하여 R에서 산점도를 만드는 방법
R에서 산점도의 점에 레이블을 지정하는 방법
R의 플롯에 회귀 방정식을 추가하는 방법