当因素不同时,geom_smooth不能使用facet_wrap吗?

时间:2018-03-17 00:58:59

标签: r ggplot2

Hi Stack Overflow社区,

这是我第一次发帖,所以我为任何奇怪的格式问题道歉,尤其是我的R代码。

首先,我试图在下面的代码再现的图的每个面板中的两条曲线周围获得95%置信区间曲线。看起来当我有ggplot(看,aes(x =治疗,y =健康,颜色= 生成))和facet_wrap(〜人口)时,我可以&#39 ;获得95%置信区间以使用geom_smooth(未包含在下面的代码中,因为它没有做任何事情)。 geom_smooth仅在我有ggplot(看起来,aes(x =治疗,y =适合,颜色= 人口))和facet_wrap(〜人口)时才有效。

如果有任何需要澄清的话,请告诉我!

这是我得到的视觉输出。我试图让geom_smooth为每条曲线添加置信区间。

ggplot output

这些是我尝试添加的时间间隔类型: geom_smooth example

library(ggplot2)


germData <- read.csv("https://dl.dropbox.com/s/h75rzzavpoxb8q1/processed_germData%20-%20no%20GA%20-%20continuous.csv")

totalViable    <- germData$totalGoodSeeds   
totalGerm      <- germData$totalGerm        

y <- cbind(totalGerm, totalViable-totalGerm)

germData$germRate <- germData$totalGerm/germData$totalGoodSeeds

q1  <- glm(y ~ population + generation + treatment + population:treatment + population:generation + generation:treatment + population:generation:treatment, germData, family=quasibinomial)

xv <- seq(0, 20, .2)
pfl <- length(xv)

look <- data.frame(population = factor(rep(levels(germData$population), each = 2*pfl)), generation = factor(rep(levels(germData$generation), each = pfl)), treatment = xv)

pred <- predict(q1, newdata = look, type= "response", se.fit = TRUE)
look$fit <- pred$fit
look$se.fit <- pred$se.fit

GAintercepts <- read.csv("https://dl.dropbox.com/s/jeei9yst7h4l37q/predict_germData.csv")

GAinterceptpoints <- GAintercepts[GAintercepts$treatment=="GA3",]
GAinterceptpoints$intercept <- "0" 
GAinterceptpoints$intercept <- as.numeric(GAinterceptpoints$intercept)

bigPlot <- ggplot(look, aes(x=treatment, y=fit, color=generation)) + 
    geom_line() + 
    geom_jitter(width=0.02, inherit.aes = FALSE, data=germData, 
    aes(x=treatment, y=germRate, color=generation), show.legend = FALSE) 

bigPlotPlus <- bigPlot + 
    geom_smooth() + 
    scale_color_manual(name="generation", values=c("#0f71bc", "#a3e1ff")) + 
    geom_pointrange(inherit.aes=FALSE, data=GAinterceptpoints, 
    aes(x=intercept, y=fit.germRate, ymin=fit.germRate-se, 
    ymax=fit.germRate+se, color=generation)) + 
    scale_color_manual(name="generation", values=c("#0f71bc", "#a3e1ff")) + 
    facet_wrap(~population) 

bigPlotPlus

1 个答案:

答案 0 :(得分:1)

如果没有看到所需输出的草图,我可能会误解你的目标。如果这些不匹配,那么请勾勒出你的目标。 我认为问题是你需要明确地将library(magrittr) library(ggplot2) germDataOriginal <- read.csv("https://dl.dropbox.com/s/h75rzzavpoxb8q1/processed_germData%20-%20no%20GA%20-%20continuous.csv") germData <- germDataOriginal %>% dplyr::select( population, generation, treatment, totalGoodSeeds, totalGerm, totalColdGerm ) %>% dplyr::mutate( difference = totalViable - totalGerm, germRate = totalGerm / totalGoodSeeds ) q1 <- glm( cbind(germData$totalGerm, germData$difference) ~ population + generation + treatment + population*generation*treatment, germData, family=quasibinomial ) look <- tidyr::crossing( population = germData$population, generation = germData$generation, treatment = seq(0, 20, by=5) ) %>% dplyr::mutate( pop_gen = paste0(population, "-", generation) ) pred <- predict(q1, newdata = look, type= "response", se.fit = TRUE) look$fit <- pred$fit look$se.fit <- pred$se.fit GAinterceptpoints <- read.csv("https://dl.dropbox.com/s/jeei9yst7h4l37q/predict_germData.csv") %>% dplyr::filter(treatment=="GA3") %>% dplyr::mutate( intercept = 0 ) bigPlot <- ggplot(look, aes(x=treatment, y=fit, color=generation)) + geom_line(aes(group=pop_gen)) + geom_point(aes(x=treatment, y=germRate, colour=generation), data=germData, inherit.aes = FALSE, shape=1) bigPlot bigPlot + geom_pointrange( aes(x=intercept,y=fit.germRate, ymin=fit.germRate-se, ymax=fit.germRate+se, color=generation), inherit.aes=FALSE, data=GAinterceptpoints ) 参数定义为人口和世代的独特组合。

last_plot() + 
  facet_wrap(~population) 

enter image description here

bigPlot

enter image description here

作为参考,您的原始germRate看起来像

enter image description here

注意我删除了更多的化妆品。我喜欢你最好的东西,但他们可能会分散你的基本问题。另外,我将关于色标的问题分开,因为它看起来大多不相关。此外,将来,将传入的数据集简化为最小的行数和列数。为了解决您的基本问题,我们无需在问题代码中计算effectView;它令人分心。