본문 바로가기
R

R ) 독학 :: data 시각화 ggplot2 히스토그램 (histogram), 박스 플랏(boxplot) in r -2 플랏 겹치기 플랏 옵션 multi plot, plot option

by C.Mond 2022. 5. 5.
728x90
728x90

시각화 공부를 하다가 참고 할만한걸 찾아서 올려놓아보려고 합니다.

 

my_df <- structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100), value= c(18, 9, 3, 4, 3, 13, 12, 5, 8, 37, 64, 107, 11, 11, 8, 18, 5, 13, 13, 14, 11, 11, 9, 14, 11, 14, 12, 10, 11, 10, 5, 3, 8, 11, 12, 11, 7, 6, 6, 4, 11, 8, 14, 13, 14, 15, 10, 2, 4, 4, 8, 15, 21, 9, 5, 7, 11, 6, 11, 2, 6, 16, 5, 11, 21, 33, 12, 10, 13, 33, 35, 7, 7, 9, 2, 21, 32, 19, 9, 8, 3, 26, 37, 5, 6, 10, 18, 5, 70, 48, 30, 10, 15, 18, 7, 4, 19, 10, 4, 32)), row.names = c(NA, 100L), class = "data.frame", .Names = c("id", "value") ) my_df require(dplyr) require(ggplot2) my_df %>% select(value) %>% ggplot(aes(x="", y = value)) + geom_boxplot(fill = "lightblue", color = "black") + coord_flip() + theme_classic() + xlab("") + theme(axis.text.y=element_blank(), axis.ticks.y=element_blank())
my_df %>% select(id, value) %>% ggplot() + geom_histogram(aes(x = value, y = (..count..)/sum(..count..)), position = "identity", binwidth = 1, fill = "lightblue", color = "black") + ylab("Relative Frequency") + theme_classic()

이 코드는 구글링 하다가 시각화 공부할때 도움이 될까 싶어 찾아서 올려봅니다,

박스플랏과 히스토그램의 다양한 옵션들을 줄 수 있는 코드가 있습니다.

 

저 2개의 플랏을 합치는 방법도 있어 올려 봅니다.

#install.packages("egg", dependencies = TRUE)
egg::ggarrange(plt2, plt1, heights = 2:1)
cowplot
# install.packages("cowplot", dependencies = TRUE)
cowplot::plot_grid(plt2, plt1, ncol = 1, rel_heights = c(2, 1), align = 'v', axis = 'lr')
patchwork
# install.packages("devtools", dependencies = TRUE)
# devtools::install_github("thomasp85/patchwork")
library(patchwork)
plt2 + plt1 + plot_layout(nrow = 2, heights = c(2, 1))

위의 코드를 사용하면 될듯한데 마지막은 깃허브서 다른사람이 만든거 불러오는건가..?

 

위코드를 공부하면 플랏의 옵션, 여러개의 플랏 합치기를 활용할 수 있을것 같다.

 

728x90
728x90

댓글