998이라는 번호로 인덱스되어 있습니다.Sessioninfo()를 명시할 예정입니다.Macbook Pro에서 이루어졌습니다.load_libraries를 명시하면 마크다운 문서 모든 영역에서 패키지를 적용할 수 있습니다.library("dplyr")
library("ggplot2")
library("ggpubr")
library("haven")
# COP 원시 파일이 존재하는 경로
data_path <- c("/Users/tigris/Dropbox/DellWorks/Doc/Rprojects/BPPV gait visualization/data/cop coordinates data")
기본적인 작업폴더는 .Rmd 파일이 있는 폴더입니다.
작업폴더를 새롭게 지정하기 위해서는 setwd("C:/Users/jangg/Dropbox/DellWorks/Doc/Rprojects/BPPV Gait visualization/) 이렇게 적용하면 됩니다.
경로 설정 변경
윈도우 컴퓨터 폴더 경로
data_path <- c("C:/Users/jangg/Dropbox/DellWorks/Doc/Rprojects/BPPV Gait visualization/data/cop coordinates data")맥북 경로
data_path <- c("/Users/tigris/Dropbox/DellWorks/Doc/Rprojects/BPPV gait visualization/data/cop coordinates data")~/BPPV gait visualization/data/cop coordinates data는 미리 만들어져 있어야 하고, 데이터가 존재해야 합니다.
wd <- getwd()
ydtm <- format(Sys.time(), "%y%m%d%H%M") # current year, month, day, time, minute
dir.create(paste(wd, "/", "outputs", "_", ydtm, sep="")) # 결과 폴더 만들기
out.path <- paste(wd, "/", "outputs", "_", ydtm, sep="") # 결과 폴더의 경로 저장
Sys.time()은 마지막에 콜론이 있어 폴더 만들기에 오류가 나게 됩니다. 예) “2020-11-18 13:00:28 KST”format()을 이용하여 저장하면 됩니다. ydtm <- format(Sys.time(), "%y%m%d%H%M")COPdata.csv들을 모두 수집합니다.# 데이터가 있는 상위 폴더가 작업폴더로 설정되어야 함.
listCOP <- list.files(recursive=TRUE, pattern="COPdata.csv")
for(i in 1:length(listCOP)){
COP <- read.csv(listCOP[i], sep=",", header=TRUE)
in.data <- COP
write.table(in.data, paste(out.path,"/", "COP.txt", sep=""),sep=",", row.names=FALSE, col.names = FALSE, append = TRUE)
}
sa <- read.table(paste(out.path, "COP.txt", sep="/"), sep=",", header=FALSE)
write.csv(sa, paste(out.path, "COP.csv", sep="/")) # 반복측정에서 treatment(2) -> trial(2) 순서로 입력
junk <- paste(out.path,"/", "COP.txt", sep="") #삭제할 경로와 파일
file.remove(junk) # 파일삭제
## [1] TRUE
listCOP <- list.files(recursive=TRUE, pattern="COPdata.csv") 이 명령문이 제대로 작동하기 위해서는 Knit에서 Knit Directory의 Current Working Directory로 체크되어 있어야 합니다.# left to right direction walking (before-treatment)
fig_1 <- ggplot(sa, aes(x = V3, y = V4)) +
geom_point(na.rm = FALSE, show.legend = NA, alpha=0.1) +
xlim(0, 400) +
ylim(0, 40)
# left to right direction walking (post-treatment)
fig_2 <- ggplot(sa, aes(x = V7, y = V8)) +
geom_point(na.rm = FALSE, show.legend = NA, alpha=0.1) +
xlim(0, 400) +
ylim(0, 40)
# right to left direction walking (before-treatment)
fig_3 <- ggplot(sa, aes(x = V5, y = V6)) +
geom_point(na.rm = FALSE, show.legend = NA, alpha=0.1) +
xlim(0, 400) +
ylim(0, 40)
# right to left direction walking (post-treatment)
fig_4 <- ggplot(sa, aes(x = V9, y = V10)) +
geom_point(na.rm = FALSE, show.legend = NA, alpha=0.1) +
xlim(0, 400) +
ylim(0, 40)
ggarrange(fig_1, fig_2, fig_3, fig_4, labels = c("A","B","C","D"), ncol = 2, nrow = 2)
+
ggarrange함수는 ggpubr패키지에서 불러왔습니다.
# You can also call the palette using a name.
x_1 <- ggplot(sa, aes(x=V3, y=V4) ) +
stat_density_2d(aes(fill = ..density..),geom = "raster", contour = FALSE) +
scale_fill_distiller(palette= "Spectral", direction=-1) +
xlim(-50, 450) +
ylim(-5, 45) +
geom_smooth(span = 0.72) +
theme(legend.position='none') +
theme_minimal() # x와 y축이 선으로 확장되어 보입니다.
x_2 <- ggplot(sa, aes(x=V7, y=V8) ) +
stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
scale_fill_distiller(palette= "Spectral", direction=-1) +
xlim(-50, 450) +
ylim(-5, 45) +
geom_smooth(span = 0.72) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme(legend.position='none')
# You can also call the palette using a name.
x_3 <- ggplot(sa, aes(x=V5, y=V6) ) +
stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
scale_fill_distiller(palette= "Spectral", direction=-1) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
xlim(-50, 450) +
ylim(-5, 45) +
geom_smooth(span = 0.72) +
theme(legend.position='none')
x_4 <- ggplot(sa, aes(x=V9, y=V10) ) +
stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
scale_fill_distiller(palette= "Spectral", direction=-1) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
xlim(-50, 450) +
ylim(-5, 45) +
geom_smooth(span = 0.72) +
theme(legend.position='none')
COPraster <- ggarrange(x_1, x_2, x_3, x_4, labels = c("A", "B", "C", "D"), ncol = 2, nrow = 2)
COPraster
ggsave("COPrasterdensity.png", COPraster, dpi=300, width = 30, height = 20, units = "cm", scale = 0.8, path=out.path)
geom = "raster", contour = FALSE에서 raster를 썼다면 contour=TRUE로는 쓸 수 없습니다.theme_minimal()은 x와 y축이 선으로 보이게 합니다. 이게 없을 경우, 회색 배경의 상자가 보입니다.scale_x_continuous(expand = c(0, 0))와 scale_y_continuous(expand = c(0, 0))은 설정한 축(xlim or ylim)만큼 그림의 틀을 확장하는 효과를 나타냅니다.m <- ggplot(sa, aes(x=V3, y=V4))
m + geom_density_2d()
m + geom_density_2d_filled(alpha = 0.5)
m + geom_density_2d_filled(alpha = 0.5) +
geom_density_2d(size = 0.25, colour = "black")
m + geom_density_2d_filled(contour_var = "count")
m + stat_density_2d(contour_var = "ndensity") +
geom_density_2d_filled(contour_var = "ndensity")
contour_var = 은 3가지 method가 있습니다(count/density/ndensity).ndensity 방식을 써야할 것 같습니다.# You can also call the palette using a name.
pp_1 <- ggplot(sa, aes(x=V3, y=V4) ) +
stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
stat_density_2d(geom = "density_2d", position = "identity", contour = TRUE, contour_var = "density")+
scale_fill_distiller(palette= "Spectral", direction=-1) +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
pp_2 <- ggplot(sa, aes(x=V7, y=V8) ) +
stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
stat_density_2d(geom = "density_2d", position = "identity", contour = TRUE, contour_var = "density")+
scale_fill_distiller(palette= "Spectral", direction=-1) +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
pp_3 <- ggplot(sa, aes(x=V5, y=V6) ) +
stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
stat_density_2d(geom = "density_2d", position = "identity", contour = TRUE, contour_var = "density")+
scale_fill_distiller(palette= "Spectral", direction=-1) +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
pp_4 <- ggplot(sa, aes(x=V9, y=V10) ) +
stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) +
stat_density_2d(geom = "density_2d", position = "identity", contour = TRUE, contour_var = "density")+
scale_fill_distiller(palette= "Spectral", direction=-1) +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
ContOnden <- ggarrange(pp_1, pp_2, pp_3, pp_4, labels=c("A", "B", "C", "D"), ncol = 2, nrow = 2)
ContOnden
ggsave("COPcontourOndensity.png", ContOnden, dpi=300, width = 30, height = 20, units = "cm", scale = 0.8, path=out.path)
countmethod 사용하여 분석하고 그 밀도 그림 위에 데이터를 점으로 표현합니다.fig_1 <- ggplot(sa, aes(x = V3, y = V4)) +
geom_point() +
xlim(0, 400) +
ylim(0, 40)
fig_2 <- ggplot(sa, aes(x = V7, y = V8)) +
geom_point() +
xlim(0, 400) +
ylim(0, 40)
fig_3 <- ggplot(sa, aes(x = V5, y = V6)) +
geom_point() +
xlim(0, 400) +
ylim(0, 40)
fig_4 <- ggplot(sa, aes(x = V9, y = V10)) +
geom_point() +
xlim(0, 400) +
ylim(0, 40)
f1 <- fig_1 +
geom_density_2d_filled(contour_var = "count") +
geom_point(alpha=0.10, colour = "white")
f2 <- fig_2 +
geom_density_2d_filled(contour_var = "count") +
geom_point(alpha=0.10, colour = "white")
f3 <- fig_3 +
geom_density_2d_filled(contour_var = "count") +
geom_point(alpha=0.10, colour = "white")
f4 <- fig_4 +
geom_density_2d_filled(contour_var = "count") +
geom_point(alpha=0.10, colour = "white")
# geom_density_2d_filled(contour_var = "ndensity")
# geom_density_2d_filled(contour_var = "density")
# geom_density_2d_filled(contour_var = "ndensity")
figarrange <- ggarrange(f1, f2, f3, f4,labels = c("A","B","C","D"), ncol = 2, nrow = 2, common.legend = TRUE)
figarrange
ggsave("COPpointOndensity.png", figarrange, dpi=300, width = 30, height = 20, units = "cm", scale = 0.8, path=out.path)
fig_1 <- ggplot(sa, aes(x = V3, y = V4)) +
geom_point(alpha=0.1) +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
fig_2 <- ggplot(sa, aes(x = V7, y = V8)) +
geom_point(alpha=0.1) +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
fig_3 <- ggplot(sa, aes(x = V5, y = V6)) +
geom_point(alpha=0.1) +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
fig_4 <- ggplot(sa, aes(x = V9, y = V10)) +
geom_point(alpha=0.1) +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
pointarrange <- ggarrange(fig_1, fig_2, fig_3, fig_4,labels = c("A","B","C","D"), ncol = 2, nrow = 2,common.legend = TRUE, legend = "right")
pointarrange
ggsave("COP_point.png", pointarrange, dpi=300, width = 30, height = 20, units = "cm", scale = 0.8, path=out.path)
countmethod 사용하여 분석p_1 <- ggplot(sa, aes(x = V3, y = V4)) +
geom_point() +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
p_2 <- ggplot(sa, aes(x = V7, y = V8)) +
geom_point() +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
p_3 <- ggplot(sa, aes(x = V5, y = V6)) +
geom_point() +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
p_4 <- ggplot(sa, aes(x = V9, y = V10)) +
geom_point() +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
p1 <- p_1 +
geom_density_2d_filled(contour_var = "count")+
labs(x = "AP direction(Left to Right, cm)", y = "ML direction, cm")
p2 <- p_2 +
geom_density_2d_filled(contour_var = "count")+
labs(x = "AP direction(Left to Right, cm)", y = "ML direction, cm")
p3 <- p_3 +
geom_density_2d_filled(contour_var = "count")+
labs(x = "AP direction(Right to Left, cm)", y = "ML direction, cm")
p4 <- p_4 +
geom_density_2d_filled(contour_var = "count")+
labs(x = "AP direction(Right to Left, cm)", y = "ML direction, cm")
picarrange <- ggarrange(p1, p2, p3, p4,labels = c("A","B","C","D"), ncol = 2, nrow = 2,common.legend = TRUE, legend = "right")
picarrange
ggsave("COP_count.png", picarrange, dpi=300, width = 30, height = 20, units = "cm", scale = 0.8, path=out.path)
ndensity method 사용하여 분석하고 논문에 삽입합니다.fig_1 <- ggplot(sa, aes(x = V3, y = V4)) +
geom_point(alpha=0.1) +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
fig_2 <- ggplot(sa, aes(x = V7, y = V8)) +
geom_point(alpha=0.1) +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
fig_3 <- ggplot(sa, aes(x = V5, y = V6)) +
geom_point(alpha=0.1) +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
fig_4 <- ggplot(sa, aes(x = V9, y = V10)) +
geom_point(alpha=0.1) +
xlim(-50, 450) +
ylim(-5, 45) +
theme(legend.position='none')+
theme_minimal()
f1 <- fig_1 +
geom_density_2d_filled(contour_var = "ndensity")+
labs(x = "", y = "")
# geom_point(alpha=0.10, colour = "white")
# geom_density_2d_filled(contour_var = "ndensity")
# geom_density_2d_filled(contour_var = "density")
# geom_density_2d_filled(contour_var = "ndensity")
f2 <- fig_2 +
geom_density_2d_filled(contour_var = "ndensity")+
labs(x = "", y = "")
f3 <- fig_3 +
geom_density_2d_filled(contour_var = "ndensity")+
labs(x = "", y = "")
f4 <- fig_4 +
geom_density_2d_filled(contour_var = "ndensity")+
labs(x = "", y = "")
figarrange <- ggarrange(f1, f2, f3, f4,labels = c("A","B","C","D"), ncol = 2, nrow = 2, common.legend = TRUE, legend = "right")
figarrange
ggsave("COP_ndensity.png", figarrange, dpi=300, width = 30, height = 20, units = "cm", scale = 0.8, path=out.path)
# 윈도우용 경로: 만약 맥북에서 분석한다면 이 부분을 바꿔줘야 합니다.
basic_gait <- read_sav("C:/Users/jangg/Dropbox/DellWorks/Doc/Rprojects/BPPV gait visualization/data/BPPV_basicGait_n33-ver2.sav")
# 두 발을 평균
pre_ST_CV <- data.frame((basic_gait$CV_ST_left+basic_gait$CV_ST_right)/2)
pre_SL_CV <- data.frame((basic_gait$CV_SL_left+basic_gait$CV_SL_right)/2)
pos_ST_CV <- data.frame((basic_gait$CV_ST_left_a+basic_gait$CV_ST_right_a)/2)
pos_SL_CV <- data.frame((basic_gait$CV_SL_left_a+basic_gait$CV_SL_right_a)/2)
# 변수에 이름 붙이기
names(pre_ST_CV) <- "Before"
names(pos_ST_CV) <- "After"
names(pre_SL_CV) <- "Before"
names(pos_SL_CV) <- "After"
df1 <- cbind(pre_ST_CV, pos_ST_CV)
df2 <- cbind(pre_SL_CV, pos_SL_CV)
# stride length:왼발의 치료 전후 비교
cv_1 <- ggpaired(df1, cond1="Before", cond2 = "After", width=0.3, fill = "condition", palette = "d3", xlab = "", ylab = "Stride Time CV (%)")
cv_2 <- ggpaired(df2, cond1="Before", cond2 = "After", width=0.3, fill = "condition", palette = "d3", xlab = "", ylab = "Stride Length CV (%)")
# cv_1 + theme(legend.position = "none")
# cv_2 + theme(legend.position = "none")
pic_CV <- ggarrange(cv_1, cv_2, labels = c("A","B"), ncol = 2, nrow = 1, common.legend = FALSE, legend = "none")
pic_CV
ggsave("gaitCV.png", pic_CV, dpi=300, width = 30, height = 20, units = "cm", scale = 0.8, path=out.path)
# 윈도우용 경로: 만약 맥북에서 분석한다면 이 부분을 바꿔줘야 합니다.
basicGait_2 <- read_sav("C:/Users/jangg/Dropbox/DellWorks/Doc/Rprojects/BPPV gait visualization/data/BPPV_basicGait_n33.sav")
# velocity
vel1 <- basicGait_2[10] #pre left foot
vel2 <- basicGait_2[92] #pre left foot
names(vel1) <- "velocity"
names(vel2) <- "velocity"
vel <- data.frame(rbind(vel1, vel2)) # colume name 일치해야 함
vel$treatment <- rep(c("Before", "After"), c(33, 33))
# cadence
cad1 <- basicGait_2[11] #pre left foot
cad2 <- basicGait_2[93] #pre left foot
names(cad1) <- "cadence"
names(cad2) <- "cadence"
cad <- data.frame(rbind(cad1, cad2)) # colume name 일치해야 함
cad$treatment <- rep(c("Before", "After"), c(33, 33))
# FAP
fap1 <- basicGait_2[16] #pre left foot
fap2 <- basicGait_2[98] #pre left foot
names(fap1) <- "fap"
names(fap2) <- "fap"
fap <- data.frame(rbind(fap1, fap2)) # colume name 일치해야 함
fap$treatment <- rep(c("Before", "After"), c(33, 33))
theme_set(theme_classic2())
# velocity
g <- ggplot(vel, aes(treatment, velocity))
ph1 <- g + geom_boxplot(width=0.3) +
geom_dotplot(binaxis='y', stackdir='center',
dotsize = .6,
fill="red") +
scale_x_discrete(limits = c("Before", "After"))
# cadence
c <- ggplot(cad, aes(treatment, cadence))
ph2 <- c + geom_boxplot(width=0.3) +
geom_dotplot(binaxis='y', stackdir='center',
dotsize = .6,
fill="red") +
scale_x_discrete(limits = c("Before", "After"))
# fap
c <- ggplot(fap, aes(treatment, fap))
ph3 <- c + geom_boxplot(width=0.3) +
geom_dotplot(binaxis='y', stackdir='center',
dotsize = .6,
fill="red") +
scale_x_discrete(limits = c("Before", "After"))
nagait <- ggarrange(ph1, ph2, ph3, labels = c("A","B", "C"), ncol = 3, nrow = 1, common.legend = FALSE, legend = "none")
nagait
ggsave("basicgait.png", nagait, dpi=300, width = 25, height = 10, units = "cm", scale = 0.8, path=out.path)
끝. 2020-11-18일 15시 49분. 임용현.
sessionInfo()
## R version 3.6.3 (2020-02-29)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19041)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Korean_Korea.949 LC_CTYPE=Korean_Korea.949
## [3] LC_MONETARY=Korean_Korea.949 LC_NUMERIC=C
## [5] LC_TIME=Korean_Korea.949
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] haven_2.2.0 ggpubr_0.2.5 magrittr_1.5 ggplot2_3.3.2 dplyr_1.0.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.4.6 RColorBrewer_1.1-2 pillar_1.4.4 compiler_3.6.3
## [5] forcats_0.5.0 tools_3.6.3 digest_0.6.25 viridisLite_0.3.0
## [9] lattice_0.20-41 nlme_3.1-147 evaluate_0.14 lifecycle_0.2.0
## [13] tibble_3.0.1 gtable_0.3.0 mgcv_1.8-31 pkgconfig_2.0.3
## [17] rlang_0.4.6 Matrix_1.2-18 ggsci_2.9 yaml_2.2.1
## [21] xfun_0.13 gridExtra_2.3 withr_2.2.0 stringr_1.4.0
## [25] knitr_1.28 generics_0.0.2 vctrs_0.3.1 hms_0.5.3
## [29] isoband_0.2.1 grid_3.6.3 tidyselect_1.1.0 cowplot_1.0.0
## [33] glue_1.4.1 R6_2.4.1 rmarkdown_2.1 tidyr_1.1.0
## [37] readr_1.3.1 purrr_0.3.4 farver_2.0.3 splines_3.6.3
## [41] MASS_7.3-51.5 scales_1.1.1 ellipsis_0.3.0 htmltools_0.4.0
## [45] colorspace_1.4-1 ggsignif_0.6.0 labeling_0.3 stringi_1.4.6
## [49] munsell_0.5.0 crayon_1.3.4