'가로 세로 비율로 그래프 크기 조정'에 해당되는 글 1건

  1. 2017.09.09 [R] ggplot 그래프 크기 조정, 가로 세로 비율 조정 : coord_fixed(ratio = 2, 1, 0.5)

이번 포스팅에서는 R ggplot2 패키지의 coord_fixed(ratio = number) 옵션을 사용해서 그래프의 크기 (가로, 세로 비율)를 조정하는 방법을 소개하겠습니다. 


1 ~ 4의 정수 좌표를 가지는 X 축과 

1 ~ 4의 정수 좌표를 가지는 Y 축을 가지는 

가상의 데이터를 사용해서 예를 들어보겠습니다. 


X축이 Y축이 1:1 비율인 그래프가 되겠습니다. 



[ 가상 데이터셋 생성 ]



#==========================

# Rssizing the plot

#==========================


install.packages("ggplot2")

library(ggplot2)


# X, Y coordinate and segments(A, B category) data set

my.df <- data.frame(XCoord = c(1, 1, 1, 2, 2, 2, 3, 3, 4, 4), 

                    YCoord = c(1, 2, 4, 1, 3, 4, 2, 4, 1, 4), 

                    Seg = c("A", "A", "A", "A", "B", "A", "B", "B", "B", "B"))





X와 Y좌표별로 Seg. 변수의 "A"는 검정색, "B"는 흰색으로 색깔을 지정해서 Heatmap을 그려보겠습니다. 

X좌표와 Y좌표가 1~4 범위를 동일하게 가지고 있으므로 크기에 대한 설정없이 디폴트 세팅으로 그래프를 그리면 아래 처럼 정사각형 1:1 비율로 그래프가 그려집니다. 



[ 그림 1 ] 원본 그래프 (Original Plot) : 가로축과 세로축이 1:1



# Original plot

ggplot(my.df, aes(x=XCoord, y=YCoord, fill=Seg)) +

  geom_tile(colour="gray80") +

  scale_fill_manual(values = c("black", "white")) +

  ggtitle("Original Heatmap by X4*Y4 size")






[ 그림 2 ] 가로축과 세로축의 비율을 1:2 로 설정하기 : coord_fixed(ratio = 2)


[그림 1]에서 원본 이미지가 가로축과 세로축이 1:1 비율의 정사각형 그래프였는데요, 이를 가로:세로 비율을 1:2로 세로가 가로의 2배 비율인 그래프(가로:세로 = 1:2)로 바꾸어 주려면 coord_fixed(ratio = 2) 를 설정해주면 됩니다. 



# Resized plot using coord_fixed(ratio = number)

ggplot(my.df, aes(x=XCoord, y=YCoord, fill=Seg)) +

  geom_tile(colour="gray80") +

  scale_fill_manual(values = c("black", "white")) +

  coord_fixed(ratio = 2) +

  ggtitle("Heatmap : Resized X:Y axis size with 1:2 ratio")







[ 그림 3 ] 가로축과 세로축의 비율을 2:1 로 설정하기 : coord_fixed(ratio = 0.5)


가로와 세로축 비율이 1:1인 원본 이미지 [그림 1] 을 가로가 세로축의 2배인 그래프 (가로:세로 = 2:1)로 바꾸고 싶다면 coord_fixed(ratio = 0.5) 로 설정해주면 됩니다. 



# Resized plot using coord_fixed(ratio = number)

ggplot(my.df, aes(x=XCoord, y=YCoord, fill=Seg)) +

  geom_tile(colour="gray80") +

  scale_fill_manual(values = c("black", "white")) +

  coord_fixed(ratio = 0.5) +

  ggtitle("Heatmap : Resized X:Y axis size with 2:1 ratio")









아래는 EBImage 패키지의 resize() 함수를 사용해서 png 이미지 파일로 출력할 때 이미지 크기를 (a) 특정 가로, 세로 크기로 설정해주는 방법과 (b) 비율로 설정해주는 방법입니다. 

R code는 stackoverflow 의 답변 중에서 aoles 님께서 달아놓은 것인데요, 코드 그대로 인용해서 소개합니다. ( * R code 출처 : https://stackoverflow.com/questions/35786744/resizing-image-in-r )


#==============
# Image resize using EBImage package

# installing EBImage package
source("http://bioconductor.org/biocLite.R")
biocLite("EBImage")


# resizing image using EBImage package's resize() function

library("EBImage")

x <- readImage(system.file("images", "sample-color.png", package="EBImage"))


# width and height of the original image
dim(x)[1:2]


# scale to a specific width and height
y <- resize(x, w = 200, h = 100)


# scale by 50%; the height is determined automatically so that
# the aspect ratio is preserved
y <- resize(x, dim(x)[1]/2)


# show the scaled image
display(y)


# extract the pixel array
z <- imageData(y)


# or
z <- as.array(y)

 



많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾸욱 눌러주세요. 



728x90
반응형
Posted by Rfriend
,