gpt4 book ai didi

r - 如何创建具有特定于每个方面的标题和副标题的分面图?

转载 作者:行者123 更新时间:2023-12-04 11:12:44 25 4
gpt4 key购买 nike

生成一个带有标题和副标题的每列的单独绘图组合的绘图,以及每个绘图的垂直线:

我已经使用直方图为具有垂直线的列创建了。

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)

结果如下图:

enter image description here

我尝试使用 facet_wrap 创建单个图在创建长数据后,而不是组合多个单图。
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创建?

最佳答案

您可以定制两行标签:

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

关于r - 如何创建具有特定于每个方面的标题和副标题的分面图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47352923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com