2017-11-17 1 views
2

제목과 부제가있는 각 열의 개별 플롯과 각 플롯의 세로선을 결합한 플롯 생성 :각면에 특정한 제목과 부제가있는면 처리 된 그림을 만드는 방법은 무엇입니까?

세로선이있는 열에 대해 히스토그램 플롯을 사용하여 생성했습니다.

library(ggplot2) 
library(gridExtra) 
library(tidyr) 

actualIris <- data.frame(Sepal.Length=6.1, Sepal.Width=3.1, Petal.Length=5.0, Petal.Width=1.7) 

# Sepal Length 
oneTailed <- sum(actualIris$Sepal.Length < iris$Sepal.Length)/nrow(iris) 

plot1SL <- ggplot(iris, aes(x=Sepal.Length)) + geom_histogram() + 
    geom_vline(xintercept = actualIris$Sepal.Length, col = "blue", lwd = 2) + 
    labs(title='Distribution of Sepal Length', x='Sepal Length', y='Frequency', 
     subtitle=paste('one-tailed test=', oneTailed, sep='')) + theme_bw() 

아래 코드는 다른 세 열의 반복입니다. (당신은 그것을 무시할 수 있습니다).

# Sepal Width 
oneTailed <- sum(actualIris$Sepal.Width < iris$Sepal.Width)/nrow(iris) 

plot1SW <- ggplot(iris, aes(x=Sepal.Width)) + geom_histogram() + 
    geom_vline(xintercept = actualIris$Sepal.Width, col = "blue", lwd = 2) + 
    labs(title='Distribution of Sepal Width', x='Sepal Width', y='Frequency', 
     subtitle=paste('one-tailed test=', oneTailed, sep='')) + theme_bw() 

# Petal Length 
oneTailed <- sum(actualIris$Petal.Length < iris$Petal.Length)/nrow(iris) 

plot1PL <- ggplot(iris, aes(x=Petal.Length)) + geom_histogram() + 
    geom_vline(xintercept = actualIris$Petal.Length, col = "blue", lwd = 2) + 
    labs(title='Distribution of Petal Length', x='Petal Length', y='Frequency', 
     subtitle=paste('one-tailed test=', oneTailed, sep='')) + theme_bw() 

# Petal Width 
oneTailed <- sum(actualIris$Petal.Width < iris$Petal.Width)/nrow(iris) 

plot1PW <- ggplot(iris, aes(x=Petal.Width)) + geom_histogram() + 
    geom_vline(xintercept = actualIris$Petal.Width, col = "blue", lwd = 2) + 
    labs(title='Distribution of Petal Width', x='Petal Width', y='Frequency', 
     subtitle=paste('one-tailed test=', oneTailed, sep='')) + theme_bw() 

# Combine the plots 
grid.arrange(plot1SL, plot1SW, plot1PL, plot1PW, nrow=1) 

는 아래의 플롯 결과 : 나는 facet_wrap를 사용하는 대신 긴 데이터를 생성 한 후, 여러 하나의 플롯을 결합하는 하나의 플롯을 만들려고 한

enter image description here

.

tmp <- iris[,-5] %>% gather(Type, value) 
#actualIris <- data.frame(Sepal.Length=6.1, Sepal.Width=3.1, Petal.Length=5.0, Petal.Width=1.7) 
actuals <- data.frame(col1=colnames(actualIris), col2=as.numeric(actualIris[1,])) 
tmp$Actual <- actuals$col2[match(tmp$Type, actuals$col1)] 
tmp$Type <- factor(tmp$Type, levels = c('Petal.Length', 'Petal.Width', 'Sepal.Length', 'Sepal.Width'), 
        labels = c('Petal Length', 'Petal Width', 'Sepal Length', 'Sepal Width')) 
ggplot(tmp, aes(value)) + facet_wrap(~Type, scales="free", nrow = 1) + geom_histogram() + 
    geom_vline(aes(xintercept=Actual), colour="blue", lwd=2) 

enter image description here

나는 labeller 옵션을 사용하여 패싯 레이블을 변경하는 시도했지만 작동하지 않았다. (단,이 주요 문제가 아닙니다.)

ggplot(tmp, aes(value)) + geom_histogram() + 
    facet_wrap(~Type, scales="free", nrow = 1, 
      labeller = as_labeller(paste('Distribution of ', levels(~Type), sep=''))) + 
    geom_vline(aes(xintercept=Actual), colour="blue", lwd=2) 

enter image description here

어떻게 tmp 만든 긴 데이터를 사용하여 첫 번째 플롯과 비슷한 플롯을 만들?

당신은 두 줄 레이블 맞춤형 할 수 있습니다

답변

2

:

labels <- c(paste('Petal Length\none-tailed test=', round(sum(actualIris$Sepal.Length < iris$Sepal.Length)/nrow(iris), 2)), 
      paste('Petal Width\none-tailed test=', round(sum(actualIris$Sepal.Width < iris$Sepal.Width)/nrow(iris), 2)), 
      paste('Sepal Length\none-tailed test=', round(sum(actualIris$Petal.Length < iris$Petal.Length)/nrow(iris), 2)), 
      paste('Sepal Width\none-tailed test=', round(sum(actualIris$Petal.Width < iris$Petal.Width)/nrow(iris), 2))) 

tmp <- iris[,-5] %>% gather(Type, value) 
actuals <- data.frame(col1=colnames(actualIris), col2=as.numeric(actualIris[1,])) 
tmp$Actual <- actuals$col2[match(tmp$Type, actuals$col1)] 
tmp$Type <- factor(tmp$Type, levels = c('Petal.Length', 'Petal.Width', 'Sepal.Length', 'Sepal.Width'), 
        labels = labels) 
ggplot(tmp, aes(value)) + facet_wrap(~Type, scales="free", nrow = 1) + geom_histogram() + 
    geom_vline(aes(xintercept=Actual), colour="blue", lwd=2) 

을 그리고이 얻을 :

enter image description here

관련 문제