다음과 같이 ggplot을 사용하여 선형 함수를 플로팅했습니다.
ggplot(data.frame(x=c(0,320)), aes(x)) +
stat_function(fun=function(x)60.762126*x-549.98, geom="line", colour="black") +
stat_function(fun=function(x)-0.431181333*x+2.378735e+02, geom="line", colour="black")+
ylim(-600,600)
그러나 첫 번째 함수는 0에서 12까지의 x에 대해 플로팅하고 두 번째 함수는 12에서 max (x)까지의 x에 대해 플로팅하기를 원합니다. 누구든지 그것을하는 방법을 알고 있습니까?
ggplot
먼저 통화 외부에서 필요한 데이터를 계산하는 것이 가장 쉽습니다 .
fun1 <- function(x) 60.762126 * x - 549.98
dat1 <- data.frame(x = c(0, 12), y = NA)
dat1$y <- fun1(dat1$x)
fun2 <- function(x) -0.431181333 * x + 2.378735e+02
dat2 <- data.frame(x = c(12, 320), y = NA)
dat2$y <- fun2(dat2$x)
ggplot(mapping = aes(x, y)) +
geom_line(data = dat1) +
geom_line(data = dat2)
또는 먼저 라인에 대한 데이터를 결합하여 (@Heroka가 제안한대로) 동일한 플롯을 생성 할 수 있습니다.
dat.com <- rbind(dat1, dat2)
dat.com$gr <- rep(1:2, c(nrow(dat1), nrow(dat2)))
ggplot(dat.com, aes(x, y, group = gr)) +
geom_line()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다