R의 scale_x_discrete 함수

Jinku Hu 2023년1월30일
  1. scale_x_discrete를 사용하여 R 플롯의x축에서 항목 순서를 반대로 바꿉니다
  2. scale_x_discrete를 사용하여 R의x축에 항목의 하위 집합을 표시합니다
  3. scale_x_discrete를 사용하여 R의x축에서 항목 레이블 이름을 바꿉니다
  4. scale_x_discrete를 사용하여 R에서x축 이름 수정
R의 scale_x_discrete 함수

이 기사에서는 R의scale_x_discrete함수를 소개합니다.

scale_x_discrete를 사용하여 R 플롯의x축에서 항목 순서를 반대로 바꿉니다

scale_x_discrete는 개별 x 축 스케일 미학에 대한 값을 설정하는 데 사용됩니다. 이 기사에서는ggplotgeom_boxplot 함수를 사용하여 여러 상자 그림 그래프를 구성하여scale_x_discrete에서 다양한 매개 변수의 효과를 보여줍니다. 일반적으로 축의 요소 순서를 반대로 해야 하는데 이를 달성하는 가장 쉬운 방법은 scale_x_discrete 매개변수의 limits 매개변수를 rev(levels(dataset_name$X_axis_items))로 설정하는 것입니다. 데이터 세트 이름이 먼저 나오고 그 뒤에 ‘$‘기호가 온 다음 반전해야하는 x 축 데이터를 지정합니다.

library(ggplot2)
library(gridExtra)

p1 <- ggplot(Loblolly, aes(x = Seed, y = height)) +
  geom_boxplot(fill = "cyan")

p2 <- ggplot(Loblolly, aes(x = Seed, y = height)) +
  geom_boxplot(fill = "orange") +
  scale_x_discrete(limits = rev(levels(Loblolly$Seed)))

grid.arrange(p1, p2, ncol = 2, nrow =2)

r 1의 scale_x_discrete

scale_x_discrete를 사용하여 R의x축에 항목의 하위 집합을 표시합니다

scale_x_discrete함수의 또 다른 유용한 기능은 x 축에서 일부 항목을 제거하고 일부 항목 만 그리는 것입니다. 이 경우 세 가지 범주의 그룹이 나열된PlantGrowth데이터 세트를 사용합니다. 따라서 다음 코드 스 니펫과 같이trt2trt1그룹 만있는 상자 그림을 출력 할 수 있습니다.

library(ggplot2)
library(gridExtra)

p1 <- ggplot(Loblolly, aes(x = Seed, y = height)) +
  geom_boxplot(fill = "cyan")

p2 <- ggplot(Loblolly, aes(x = Seed, y = height)) +
  geom_boxplot(fill = "orange") +
  scale_x_discrete(limits = rev(levels(Loblolly$Seed)))

p3 <- ggplot(PlantGrowth, aes(x = group, y = weight)) +
  geom_boxplot(fill = "pink")

p4 <- ggplot(PlantGrowth, aes(x = group, y = weight)) +
  geom_boxplot(fill = "green") +
  scale_x_discrete(limits = c("trt2", "trt1"))

grid.arrange(p1, p2, p3, p4, ncol = 2, nrow =2)

r 2의 scale_x_discrete

scale_x_discrete를 사용하여 R의x축에서 항목 레이블 이름을 바꿉니다

scale_x_discrete함수를 사용하여 x 축을 따라 항목 레이블의 이름을 바꿀 수도 있습니다. scale_x_discrete함수의labels매개 변수에 할당 된 벡터와 함께 새 레이블 값을 제공 할 수 있습니다.

library(ggplot2)
library(gridExtra)

p1 <- ggplot(PlantGrowth, aes(x = group, y = weight)) +
  geom_boxplot(fill = "cyan")

p2 <- ggplot(PlantGrowth, aes(x = group, y = weight)) +
  geom_boxplot(fill = "pink") +
  scale_x_discrete(
    labels = c("Control", "Treatment 1", "Treatment 2")
  )

grid.arrange(p1, p2, ncol = 2, nrow =2)

r 3의 scale_x_discrete

scale_x_discrete를 사용하여 R에서x축 이름 수정

각 이전 방법을 결합하여 원하는 그래프 구조를 출력 할 수 있습니다. 예를 들어, 다음 예제 코드는p4를 그려 항목의 하위 집합 만 표시하고 이러한 레이블의 이름을 제공된 값으로 바꿉니다. 또한scale_x_discrete를 활용하여name매개 변수를 사용하여 x 축을 따라 이름을 수정합니다.

library(ggplot2)
library(gridExtra)

p1 <- ggplot(PlantGrowth, aes(x = group, y = weight)) +
  geom_boxplot(fill = "cyan")

p2 <- ggplot(PlantGrowth, aes(x = group, y = weight)) +
  geom_boxplot(fill = "pink") +
  scale_x_discrete(
    labels = c("Control", "Treatment 1", "Treatment 2")
  )

p3 <- ggplot(OrchardSprays, aes(x = treatment, y = decrease)) +
  geom_boxplot(fill = "orange")

p4 <- ggplot(OrchardSprays, aes(x = treatment, y = decrease)) +
  geom_boxplot(fill = "green") +
  scale_x_discrete(
    limits = c("A", "B"),
    labels = c("Alice", "Bob"),
    name = "Treatment"
  )

grid.arrange(p1, p2, p3, p4, ncol = 2, nrow =2)

r 4의 scale_x_discrete

작가: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

관련 문장 - R Plot