데이터 프레임이 여러 개 있고 동일한 ggplot에 각 데이터 프레임의 2 열을 표시하려고합니다. 파란색과 빨간색으로 채색되어있는 다른 기능의 줄거리가 이미 있습니다. 새 기능을 추가하려고합니다. 내가 콘솔에서 작동하려고 노력하고 있지만, 나는 함수를 저장하고, 호출하고, 작동시킬 수는 없다. 내가 오류는 다음과 같습니다함수에서 for 루프를 사용하여 동일한 ggplot에 데이터 프레임을 그립니다.
Discrete value supplied to continuous scale.
그래서, dataframes 내 환경에 있고 BEFMORN9에 BEFMORN1 이름. 초기 플롯은 test_plot입니다.
나에게 test_plot을 제공하는 첫 번째 부분이 작동합니다. BEFMORN는 [1] R 온되도록
test_plot<-ggplot()+geom_point(data=yy4, aes(x=Time, y=Dist), colour="red")+geom_point(data=zz4, aes(x=Time, y=Dist), colour="blue")
test_plot<-test_plot+scale_x_continuous(name="Time (Seconds from the beginning)")
test_plot<-test_plot+scale_y_continuous(name="Distance (Metres from the beginning)")
번째 부분은
plot_all_runs<-function(r,test_plot) {
for (i in 1:(length(r[[1]]))) {
z<-as.data.frame(mget(ls(pattern=paste0("BEFMORN",i))))
test_plot2<-test_plot+geom_point(data=z, aes_string(x=names(z)[12], y=names(z)[17]))
}print(test_plot2)
}
R 다른 dataframes 6 개리스트 목록은 새 기능 할 것이다. BEFNOON은 r [2] 등에서 나올 것입니다. 그래서 내 계획은 paste0에 다른 인수를 갖는 6 개의 동일한 함수를 갖는 것입니다.
데이터 프레임 z는 각 반복마다 서로 다른 열 이름을 가지므로 aes_string(x=names(z)[12]
을 사용하고 있습니다.
누군가 내가 왜 오류가 발생하는지 이해하고 있습니까? 나는 저울을 가지고 놀았지만 (초기 플롯에서 제거하거나 다음 플롯에서 다시 추가), 개선하지는 않았습니다.
편집 : 플롯 할 모든 열이 숫자로 변환되었습니다. 다른 요소와 정수가 있습니다.
예
BEFMORN1<-data.frame(BEFMORN1.Time=seq(0:10, 0.5), BEFMORN1.Dist=1:20)
BEFMORN2<-data.frame(BEFMORN2.Time=seq(0:13, 0.5), BEFMORN2.Dist=c(1:8,8,8,9,10,13,13,13,13.5,14,14,14 14:20))
yy4<-data.frame(Time=seq(0:10, 0.5). Dist=c(1:8,8,8,9,10,13,14:20))
ZZ4<-data.frame(Time=seq(0:12, 0.5). Dist=c(1:8,8,8,9,9.5,10,10.5,12,12.5,13,14:20))
test_plot<-ggplot()+geom_point(data=yy4, aes(x=Time, y=Dist), colour="red")+geom_point(data=zz4, aes(x=Time, y=Dist), colour="blue")
plot_all_runs<-function(test_plot) {
for (i in 1:9) {
z<-as.data.frame(mget(ls(pattern=paste0("BEFMORN",i))))
test_plot2<-test_plot+geom_point(data=z, aes_string(x=names(z)[12], y=names(z)[17]))
}print(test_plot2)
}
오류의 원인은 일반적으로 데이터가 긴 형식이 아니라 넓은 형식이며 ggplot이이 형식을 개별 변수로 오해하기 때문입니다. 그것 이외의 재현 할 수없는 예제 (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) 없이는 도움이 꽤 어렵습니다. 단일 플롯에 여러 곡선을 추가하거나 여러 플롯을 인쇄 하시겠습니까? – biomiha
하나의 플롯에 여러 개의 커브 – Nikos
이 경우 데이터 프레임을 하나로 병합 (루프가 필요 없음 - 더 나은 옵션이 많음), 커브를 정의하는 변수에 관계없이 데이터를 긴 형식과 색상으로 변환하십시오. – biomiha