2017-05-16 16 views
0

각 요소 수준에 대해 산점도를 나란히 놓으려고합니다. 산점도 그러나각 패싯에서 여러 값 - GGplot

library(ggplot2) 
library(plotly) 
dput(df) 
structure(list(business_date = structure(c(17245, 17245, 17245, 
17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 
17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 
17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 
17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 
17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 
17245, 17245), class = "Date"), token_id = c(1.85075e+19, 1.85075e+19, 
1.70088e+19, 1.44736e+19, 1.70234e+19, 1.70234e+19, 1.28442e+19, 
1.21512e+19, 1.21512e+19, 1.21512e+19, 1.50179e+19, 1.50179e+19, 
1.50179e+19, 1.24174e+19, 1.24174e+19, 1.2993e+19, 1.2993e+19, 
1.2993e+19, 1.2993e+19, 1.98549e+19, 1.98549e+19, 1.98549e+19, 
1.98549e+19, 1.98549e+19, 1.98549e+19, 1.98549e+19, 1.98549e+19, 
1.18468e+19, 1.18468e+19, 1.18468e+19, 1.18468e+19, 1.18468e+19, 
1.18468e+19, 1.66233e+19, 1.21387e+19, 1.21387e+19, 1.9162e+19, 
1.40385e+19, 1.40385e+19, 1.40385e+19, 1.40385e+19, 1.69103e+19, 
1.57387e+19, 1.57387e+19, 1.57387e+19, 1.57387e+19, 1.40552e+19, 
1.43084e+19, 1.15233e+19, 1.15233e+19), entry_sp_id = structure(c(4L, 
8L, 8L, 6L, 8L, 8L, 4L, 8L, 8L, 6L, 8L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 4L, 4L, 4L, 4L, 4L, 4L, 
6L, 4L, 6L, 6L, 4L, 6L, 8L, 4L, 4L, 8L, 8L, 6L, 6L, 1L, 4L, 4L, 
4L), .Label = c("GA", "LT", "NA", "SB", "ST", "SM", "SMN", "TT" 
), class = "factor"), exit_sp_id = structure(c(4L, 8L, 8L, 6L, 
8L, 8L, 4L, 8L, 8L, 6L, 8L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 
4L, 4L, 4L, 4L, 4L, 4L, 6L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 4L, 6L, 
6L, 4L, 6L, 8L, 4L, 4L, 8L, 8L, 6L, 6L, 1L, 4L, 4L, 4L), .Label = c("GA", 
"LT", "NA", "SB", "ST", "SM", 
"SMN", "TT"), class = "factor"), dist = c(1.5, 
1.9, 5.1, 0.5, 1.1, 1.6, 1.9, 2, 1.9, 0.9, 0.6, 0.9, 1.6, 4.1, 
4.2, 1.1, 0.5, 0.8, 3.9, 3.2, 0.9, 10.1, 9.1, 1.2, 7.4, 0.4, 
0.9, 1.7, 2.5, 5.6, 2, 1.2, 1.7, 1.2, 1.8, 9.2, 0.8, 2.4, 7.8, 
2.5, 11, 1.2, 3.1, 5.5, 9.2, 3.5, 2.2, 0.6, 2.9, 1.1), Travel_Time = c(5, 
8, 23, 2, 5, 8, 9, 7, 9, 3, 1, 2, 5, 13, 12, 3, 1, 2, 21, 12, 
3, 28, 34, 3, 18, 2, 6, 5, 10, 27, 9, 8, 5, 7, 6, 26, 2, 3, 18, 
14, 35, 3, 14, 21, 44, 18, 10, 2, 18, 8)), .Names = c("business_date", 
"token_id", "entry_sp_id", "exit_sp_id", "dist", "Travel_Time" 
), row.names = c(10813L, 10816L, 10821L, 10830L, 10839L, 10842L, 
10846L, 10848L, 10849L, 10851L, 10856L, 10857L, 10858L, 10860L, 
10862L, 10863L, 10866L, 10867L, 10868L, 10870L, 10871L, 10873L, 
10875L, 10876L, 10877L, 10878L, 10879L, 10885L, 10887L, 10888L, 
10889L, 10890L, 10892L, 10893L, 10898L, 10899L, 10901L, 10904L, 
10905L, 10907L, 10909L, 10914L, 10916L, 10917L, 10918L, 10919L, 
10922L, 10927L, 10930L, 10931L), class = "data.frame") 

> p5 <- ggplot(df, aes(x=df$dist, y=df$Travel_Time)) + 
+ geom_point(aes(colour=df$entry_sp_id)) + 
+ geom_smooth(method="lm") + facet_grid(. ~ df$entry_sp_id) 
> ggplotly(p5) 

- 각면 내에서 값이 다른 요소 레벨의 값을 나타낸다는 (- 하나의 컬러 및 각각의 파셋 미만 형상 각면 아래 하나의 요소 레벨 값이 있어야한다). enter image description here

+2

는 사용자 코드에서 모든'안양 $을'제거합니다. 문제를 해결해야합니다. – Roland

+0

왜 그런지 설명해 주시겠습니까? – RUser

+0

gesplot을 aes 내부에서'$'과 함께 사용하고 예기치 않은 출력을 얻은 다음'$'없이 "올바른 방법으로"사용하고 올바른 출력을 얻고 왜 작동하는지 묻습니다. – zx8754

답변

1

이 경우 중복되므로 $를 제거해야합니다. ggplot에 이미 data=df의 소스가 정의되어 있으므로 $로 열을 호출 할 필요가 없습니다. 더구나 $로 호출하기 때문에 ggplot을 벡터로 사용하고 값이 entry_sp_id에 속하는 정보를 잃어 버려 문제가 발생합니다.

p5 <- ggplot(df, aes(x=dist, y=Travel_Time)) + 
geom_point(aes(colour=entry_sp_id)) + 
geom_smooth(method="lm") +facet_grid(~entry_sp_id) 
ggplotly(p5) 

편집 : 맞춤법 :