1

我想用图形输出绘制powerofF-testggplot2MWE 在下面:

library(ggplot2)
df1 <- 3
df2 <- 8
Alpha <- 0.05

df <- data.frame(X = seq(from = 0, to = 10, length = 500))

p <- ggplot(data=df, mapping=aes(x = X, y = df(x = X, df1 = df1, df2 = df2, ncp=0)))+
        geom_area(color="black", fill="white")
p <- p + geom_area(aes(x=X, y=df(x = X, df1 = df1, df2 = df2, ncp=2)), color="blue", fill="blue", alpha = 1/3)
p <- p + scale_y_continuous(expand = c(0, 0)) + scale_x_continuous(breaks=seq(0, 10, 1))
p <- p + geom_area(data = subset(df, X > qf(p = 1-Alpha, df1 = df1, df2 = df2, ncp=0)), fill = "red")
p <- p + geom_area(data = subset(df, X > qf(p = 1-Alpha, df1 = df1, df2 = df2, ncp=0)),
          aes(x=X, y=df(x = X, df1 = df1, df2 = df2, ncp=2)), fill = "green", alpha=1/3)
p

在此处输入图像描述

我想知道如何使它更透明。不同地区不是很清楚。

4

2 回答 2

2

Transparency is controlled via the alpha graphics parameter. Just set this to a lower value for your layer. Right now you already have it set to 1/3, try setting it to 1/6

library(ggplot2)
df1 <- 3
df2 <- 8
Alpha <- 0.05

df <- data.frame(X = seq(from = 0, to = 10, length = 500))

p <- ggplot(data=df, mapping=aes(x = X, y = df(x = X, df1 = df1, df2 = df2, ncp=0)))+
  geom_area(color="black", fill="white")
p <- p + geom_area(aes(x=X, y=df(x = X, df1 = df1, df2 = df2, ncp=2)), color="blue", fill="blue", alpha = 1/6) ## this is the only line I've changed.
p <- p + scale_y_continuous(expand = c(0, 0)) + scale_x_continuous(breaks=seq(0, 10, 1))
p <- p + geom_area(data = subset(df, X > qf(p = 1-Alpha, df1 = df1, df2 = df2, ncp=0)), fill = "red")
p <- p + geom_area(data = subset(df, X > qf(p = 1-Alpha, df1 = df1, df2 = df2, ncp=0)),
                   aes(x=X, y=df(x = X, df1 = df1, df2 = df2, ncp=2)), fill = "green", alpha=1/3)
p
于 2013-07-26T15:34:45.857 回答
2

在我看来,您应该先定义要绘制的整个数据集,然后才开始绘制。这就是我要做的。

# load ggplot
require(ggplot2)
# define data
df1 <- 3
df2 <- 8
Alpha <- 0.05
X=seq(from = 0, to = 10, length = 500)
# Define the data outside ggplot
dd <- data.frame(x=c(X,X), 
                 y=c(df(X, df1, df2, ncp=0), df(X, df1, df2, ncp=2)),
                 npc=factor(rep(c(0,2), each=length(X))), 
                 quantile=c(X>qf(p = 1-Alpha, df1 = df1, df2 = df2, ncp=0),
                            # should this be qf(..., npc=0) or qf(..., npc=2)?
                            X>qf(p = 1-Alpha, df1 = df1, df2 = df2, ncp=2))) 
# Plot
pp <- ggplot(data=dd, aes(x=x, ymin=0, ymax=y, fill=npc, color=npc, alpha=quantile)) 
pp <- pp + geom_ribbon()
# Play with different alpha-ranges until you are satisfied
pp + scale_alpha_discrete(range=c(.2, .5), guide="none")
于 2013-07-30T07:49:58.317 回答