2016-10-11 3 views
0

여러 변수의 분포 플롯을 단일 이미지 파일에 넣으 려합니다. 모든 분포 플롯 (서브 플롯)에는 유사한 그룹이 색상으로 구분되어 있습니다.동일한 플롯에서 패싯과 같은 다중 분포 플롯 (다른 변수)

현재 ggplot을 사용하여 각 변수를 개별적으로 플로팅하는 방법을 사용하고 있습니다. 그런 다음 grid.arrange를 사용하여 모든 하위 그림을 결합하여 모든 배포를 나타낼 수 있습니다.

(아래 예 코드)

각 부가 적 줄거리의 X 축에 사용되는 변수는 다른

알 수있는 바와 같이

#plot1 
plot_min_RTT <- ggplot(house_total_year, aes(x=min_RTT, colour = ISP)) + 
geom_density(adjust = 1/2,alpha=0.1, size = 2) 

#plot2 
plot_MaxMSS <- ggplot(house_total_year, aes(x=MaxMSS, colour = ISP)) + 
geom_density(adjust = 1/2,alpha=0.1, size = 2) 

#plot3 
plot_send_buffer_size <- ggplot(house_total_year, aes(x=send_buffer_size, colour = ISP)) + 
geom_density(adjust = 1/2,alpha=0.1, size = 2) 

#plot4 
plot_maxSpeed <- ggplot(house_total_year_filtered, aes(x=download_speed_max_month, colour = ISP)) + 
geom_density(adjust = 1/2,alpha=0.1, size = 2) 

#combine 
grid.arrange(plot_min_RTT,plot_MaxMSS,plot_send_buffer_size,plot_maxSpeed) 
이다. 그러나 모든 변수에는 비슷한 그룹 변수 (ISP)가 있습니다. 나는 아래 하나의 플롯으로 돌아가 셨습니다 :

What I currently have

그러나, 내가 실제로 원하는 것은 모든 줄거리를위한 단 하나의 전설 (ISP)를하는 것입니다. 나는 ggplot에서 facet_wrap 함수를 사용하려고 생각하고 있었지만 여전히 고민 중입니다. 도와주세요.

의견을 보내 주시면 감사하겠습니다.

감사합니다. :)

+1

데이터는 서로 다른 플롯을 만들기 위해 'facet_wrap'에서 사용될 수있는 가변 단일 dataframe이어야한다. – Haboryme

+1

이것을 읽은 다음 "여러 ggplot2 그래프에 대한 공통 범례 추가"장을 참조하십시오. http://www.sthda.com/english/wiki/ggplot2-easy-way-to-mix-multiple-graphs-on-the-same -page-r- 소프트웨어 및 데이터 시각화 –

+0

Thanks @J_F! 그 작품! –

답변

0

재현성있는 데이터를 제공하지 않으므로 ggplot 패키지와 함께 제공되는 data.frame mpg을 사용했습니다.

# Subset the data 
d <- mpg[, c(1, 3:5)] 
# your ISP == manufacturer 
# Than transform the data to long format as stated already in the comments using reshape function melt 
library(reshape2) 
d_long <- melt(d) 
head(d_long) 
    manufacturer variable value 
1   audi displ 1.8 
2   audi displ 1.8 
3   audi displ 2.0 
4   audi displ 2.0 
5   audi displ 2.8 
6   audi displ 2.8 

# Now plot the data using the variable column in facet_grid for the layout. 
# scales = "free_x" is used for vairable dependend x-axis scales. 
ggplot(d_long, aes(x = value, colour = manufacturer)) + 
    geom_density() + 
    facet_grid(~ variable, scales = "free_x") + 
    theme_bw() 
# Instead of using facet_grid you can try 
# facet_wrap(~ variable, scales = "free_x", ncol = 2) 
# to arrange the plots row- and colwise as your needs are. The scaling of both axis can also changed. 
ggplot(d_long, aes(x = value, colour = manufacturer)) + 
    geom_density() + 
    facet_wrap(~ variable, scales = "free", ncol = 2) + 
    theme_bw() 

enter image description here

+0

감사합니다 @ 짐보,이 솔루션을 사용하는 유일한 문제는 각 하위 플롯에 서로 다른 y 축 라벨을 사용할 수 없다는 것입니다. –

+0

@TaraSutjarittham이 내 편집 내용을 살펴 봤습니다. 밀도 플롯을 위해 왜 다른 y- 라벨이 필요한가? 그것은 각 플롯에 대해 항상 ""밀도 "이어야합니다. – Jimbou

+0

@Jumbou, 죄송합니다. x-aixs를 의미합니다. :) –