programing

오리진이 0에서 시작하도록 강제 적용

lastcode 2023. 7. 16. 13:33
반응형

오리진이 0에서 시작하도록 강제 적용

ggplot2에서 y축과 x축의 원점/절편을 설정하려면 어떻게 해야 합니까?

x축의 선은 정확하게 다음과 같아야 합니다.y=Z.

와 함께Z=0또는 다른 주어진 값.

xlim그리고.ylim여기서 자르지 마세요.사용해야 합니다.expand_limits,scale_x_continuous,그리고.scale_y_continuous시도:

df <- data.frame(x = 1:5, y = 1:5)
p <- ggplot(df, aes(x, y)) + geom_point()
p <- p + expand_limits(x = 0, y = 0)
p # not what you are looking for

enter image description here

p + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0))

enter image description here

점이 잘리지 않도록 약간의 조정이 필요할 수 있습니다(예를 들어, 다음 점 참조).x = 5그리고.y = 5.

gg 그림에 다음을 추가하기만 하면 됩니다.

+ scale_x_continuous(expand = c(0, 0), limits = c(0, NA)) + 
  scale_y_continuous(expand = c(0, 0), limits = c(0, NA))

df <- data.frame(x = 1:5, y = 1:5)
p <- ggplot(df, aes(x, y)) + geom_point()
p <- p + expand_limits(x = 0, y = 0)
p # not what you are looking for


p + scale_x_continuous(expand = c(0, 0), limits = c(0,NA)) + 
  scale_y_continuous(expand = c(0, 0), limits = c(0, NA))

enter image description here

마지막으로 의도치 않게 차트에서 데이터를 제외하지 않도록 주의해야 합니다.예를 들어, aposition = 'dodge'막대가 차트에서 완전히 벗어날 수 있습니다(예: 막대 값이 0이고 축이 0에서 시작하는 경우). 따라서 막대가 보이지 않고 막대가 있는지도 모를 수 있습니다.플롯의 미관을 개선하려면 먼저 전체 플롯 데이터를 확인한 다음 위의 팁을 사용하는 것이 좋습니다.

ggplot2의 최신 버전에서는 이 작업이 더 쉬울 수 있습니다.

p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point()
p+ geom_point() + scale_x_continuous(expand = expansion(mult = c(0, 0))) + scale_y_continuous(expand = expansion(mult = c(0, 0)))

enter image description here

?expansion()자세한 내용은

다른 옵션은 다음과 함께 사용합니다.expand = FALSE한계는 데이터에서 추출하거나 한계를 기반으로 합니다.다음은 재현 가능한 예입니다.

df <- data.frame(x = 1:5, y = 1:5)

library(ggplot2)
p <- ggplot(df, aes(x, y)) + geom_point()
p <- p + expand_limits(x = 0, y = 0)
p + coord_cartesian(expand = FALSE)

repref v2.0.2를 사용하여 2022-11-26에 생성됨

다음에서 한계를 지정할 수도 있습니다.coord_cartesian이렇게 직접적으로:

df <- data.frame(x = 1:5, y = 1:5)

library(ggplot2)
p <- ggplot(df, aes(x, y)) + geom_point()
p + coord_cartesian(expand = FALSE, xlim = c(0, NA), ylim = c(0, NA))

repref v2.0.2를 사용하여 2022-11-26에 생성됨

언급URL : https://stackoverflow.com/questions/13701347/force-the-origin-to-start-at-0

반응형