Reputation: 23938
I want to plot the power
of F-test
with ggplot2
and MWE is below with the graph output:
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
I wonder how to make it more transparent. Different regions are not very clear.
Upvotes: 1
Views: 890
Reputation: 22343
In my opinion you should define the entire dataset that you want to plot first and only then start plotting. Here is what I would do.
# 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")
Upvotes: 2
Reputation: 8558
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
Upvotes: 2