두 치료 그룹의 다른 개인에 대한 몇 가지 측정 값을 플로팅하고 싶습니다. 첫 번째 치료를위한 열과 두 번째 치료를위한 두 개의 열을 보여 드리고 싶습니다. 각 열에는 해당 그룹의 각 개인에 대한 플롯이 있습니다. 이 같은:
require(ggplot2)
t <- seq(from=0, to=10, length.out=100)
ids <- c(1, 17, 22, 4, 55, 74, 88)
treatment <- c('A', 'A', 'A', 'B', 'B', 'B', 'B')
df <- NULL
for (i in 1:length(ids))
df <- rbind(df, data.frame(time=t, treatment=treatment[i], id=ids[i], value=rnorm(length(t))))
ggplot(df, aes(y=value, x=time)) +
geom_line() +
facet_grid(id ~ treatment, scale='free_y')
생산하는
ID는 거의 확실하게 정렬되지 않으므로 중간에 여러 개의 빈 그래프가 표시됩니다. ID는 어쨌든 동일한 개인에 해당하지 않으므로 정렬 할 필요가 없습니다. 두 열에서 ID가 독립적이기를 원합니다. 지나치게 해키에 의지하지 않고 ggplot에서 이것을 수행하는 편리한 방법이 있습니까? 아니면 다른 플롯을 나란히 보면서 붙어 있습니까? 나는 그들에게 간격을 제거하지만 행 레이블의 유용성을 제거하는 "의사"ID를 줄 수 있습니다.
이것은 단지 머리 해키,하지만 당신은 다음 그룹과 ID를 모두 새 변수를 생성 할 수 있습니다 facet_wrap
대신의 사용에 facet_grid
. 유일한 해키 부분은 각 그룹에 동일한 수의 개인이 있는지 확인하는 것입니다 (여기서는 빈 사용자에 대한 자리 표시 자 추가).
# Generate a label with the individual and id
df$label <-
paste(df$treatment
, df$id
, sep = ": ")
# Count the number of individuals in each treatment
counts <-
by(df$id, df$treatment, function(x){length(unique(x))})
# For each group, check how many there are
# If it is less than the max, add a dummy row as a placeholder
for(i in names(counts)){
if(counts[i] < max(counts)){
df <- rbind(df,data.frame(time=0, treatment=i, id=NA, value=0, label= paste(i, "holder", 1:(max(counts) - counts[i]))))
}
}
# Plot the result
ggplot(df, aes(y=value, x=time)) +
geom_line() +
# Facet on the contstructed label
facet_wrap(~label
, scale='free_y'
# Make sure that you put them in columns, not rows
, dir = "v"
# Set the number of columns to be the number of groups
, ncol = length(unique(df$treatment)))
레이블을 측면에 유지하려면 각 그룹의 개인을 나타내는 공동 레이블을 구성 할 수 있습니다. 라벨 구성은 약간 엉망이지만 유연해야한다고 생각합니다. 위에서 생성 한 추가 행을 포함시킨 후에는 이것을 실행하고 싶지 않을 것입니다.
theIndOrders <-
split(df$id,df$treatment) %>%
lapply(function(x){
1:(length(unique(x))) %>%
setNames(sort(unique(x)))
}) %>%
unlist
myLabels <-
split(names(theIndOrders), theIndOrders) %>%
sapply(paste, collapse = "; ")
df$group <-
theIndOrders[paste(df$treatment,df$id, sep = ".")]
df$myLab <-
myLabels[df$group]
ggplot(df, aes(y=value, x=time)) +
geom_line() +
# Facet on the contstructed label
facet_grid(myLab ~ treatment)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다