How to create a facetted plot with title and subtitle specific to each facet?

六月ゝ 毕业季﹏ 提交于 2020-02-22 23:23:11

问题


Generating a plot combined with separate plots for each column with title and subtitles, along with a vertical line for each plot:

I have created using the histogram plot for a column with a vertical line.

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()

The below code is just repetition of three other columns. (You can ignore it).

# 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)

It results in the below plot:

I have tried to create the single plot using facet_wrap instead of combining multiple single plots, after creating the long data.

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) 

I have tried to change the facet labels using the labeller option but it did not work. (However, this is not the main question.)

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) 

How to create the plot similar to the first plot using the long data tmp created?


回答1:


You can make tailored two lines labels:

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) 

And get this:



来源:https://stackoverflow.com/questions/47352923/how-to-create-a-facetted-plot-with-title-and-subtitle-specific-to-each-facet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!