R의 플롯에 위 첨자와 아래 첨자를 추가하는 방법
다음 기본 구문을 사용하여 R의 플롯에 위 첨자 또는 아래 첨자를 추가할 수 있습니다.
#define expression with superscript x_expression <- expression(x^ 3 ~ variable ~ label) #define expression with subscript y_expression <- expression(y[ 3 ] ~ variable ~ label) #add expressions to axis labels plot(x, y, xlab = x_expression, ylab = y_expression)
다음 예에서는 이 구문을 실제로 사용하는 방법을 보여줍니다.
예 1: 축 레이블에 지수 추가
다음 코드는 R에서 플롯의 축 레이블에 지수를 추가하는 방법을 보여줍니다.
#define data x <- c(1, 2, 3, 4, 5, 6, 7, 8) y <- c(9, 12, 14, 16, 15, 19, 26, 29) #define x and y-axis labels with superscripts x_expression <- expression(x^3 ~ variable ~ label) y_expression <- expression(y^3 ~ variable ~ label) #createplot plot(x, y, xlab = x_expression, ylab = y_expression)
X축과 Y축 모두 레이블에 지수가 있습니다.
y축 지수는 플롯에서 약간 잘립니다. 레이블을 플롯의 축에 더 가깝게 가져오려면 R의 par() 함수를 사용할 수 있습니다.
#adjust by values (default is (3, 0, 0)) by(mgp=c(2.5, 1, 0)) #createplot plot(x, y, xlab = x_expression, ylab = y_expression)
참고 : 지수로 배치할 임의의 값으로 “3”을 선택했습니다. 숫자 값이나 문자를 위 첨자로 자유롭게 배치하세요.
예 2: 축 레이블에 아래 첨자 추가
다음 코드는 R에서 플롯의 축 레이블에 인덱스를 추가하는 방법을 보여줍니다.
#define data x <- c(1, 2, 3, 4, 5, 6, 7, 8) y <- c(9, 12, 14, 16, 15, 19, 26, 29) #define x and y-axis labels with superscripts x_expression <- expression(x[3] ~ variable ~ label) y_expression <- expression(y[3] ~ variable ~ label) #createplot plot(x, y, xlab = x_expression, ylab = y_expression)
예제 3: 플롯 내부에 위 첨자와 아래 첨자 추가
다음 코드는 경로 내의 텍스트 요소에 위 첨자를 추가하는 방법을 보여줍니다.
#define data x <- c(1, 2, 3, 4, 5, 6, 7, 8) y <- c(9, 12, 14, 16, 15, 19, 26, 29) #createplot plot(x, y) #define label with superscript to add to plot R2_expression <- expression(paste(" ", R^2 , "= ", .905)) #add text to plot text(x = 2, y = 25, label = R2_expression)
추가 리소스
여러 변수를 사용하여 R에서 산점도를 만드는 방법
R에서 병렬 상자 플롯을 만드는 방법
R에서 플롯을 오버레이하는 방법