0
votes

I'd like to create a figure in ggplot that emphasizes a subset of the points using the alpha parameter. Each point represents a combination of five variables:

  • x axis variable
  • y axis variable
  • x-faceted variable
  • y-faceted variable
  • color variable

Because many points share partial or complete overlap with each other, I wanted to jitter the points using a position = position_jitterdodge argument to (1) separate the values into groups within their x-axis variable, and (2) apply the color to each of those discrete groups.

My problem arises when I try to apply that alpha parameter to a subset of those points. In my attempt to generate this plot, my points don't line up in those groups within their respective x-axis positions. This occurs because (in this example) one of those groups is entirely missing data in that subset. Thus the plot automatically squeezes these remaining points closer together for one half of the plot, but not for the other half where all groups have some data to plot. By mistake, I've effectively created two different widths between the grouped points along the x-axis.

I'd like to force it to stop doing that, and would greatly appreciate any help someone can offer as to how do so.

Here is a token example of my problem:

xval <- rep(c(rep("a", 160), rep("b", 160)),2)
yval <- c(runif(320, min = 0.51, max = 0.99), runif(310, min = 0.01, max = 0.49), rep(NA, 10))
yfacet <- rep(c(rep("night", 10), rep("day", 10)),32)
xfacet <- rep(c(rep("apples", 20), rep("oranges", 20)), 16)
colorgroup <- rep(c(rep("first", 40), rep("second", 40), rep("third", 40), rep("whoops", 40)),4)

df <- data.frame(xval, yval, xfacet, yfacet, colorgroup)
df$yfacet <- factor(df$yfacet, levels = c("night", "day"))

If I try to plot these data without any subsetting, all my points line up as expected into those jittered and dodged x-axis groups. Note the warning from ggplot indicating that there are 10 missing values - these are missing from the most bottom right of all panels in the plot... the blue "whoops" colorgroup points in the oranges ~ day facet, in group b of the x-axis:

ggplot() +
  geom_point(data = df,
             aes(x=xval, y=yval, color=colorgroup),
             position = position_jitterdodge(jitter.width = 0.1)) +
  facet_grid(xfacet ~ yfacet) +
  scale_color_manual(values = c("red", "black", "orange", "blue"))

enter image description here

My intention is to take this same plot, but use an alpha parameter only to those points that have a yval more than 0.5. This was the argument I used, which produced a problem in that same bottom right panel - note how in the day ~ oranges facet, in the x-axis b group, the points no longer line up? I'm wondering what I might change to fix this alignment issue:

ggplot() +
  geom_point(data = df %>% filter(yval > 0.5),
             aes(x=xval, y=yval, color=colorgroup),
             position = position_jitterdodge(jitter.width = 0.1),
             alpha = 0.1) +
  geom_point(data = df %>% filter(yval <= 0.5),
             aes(x=xval, y=yval, color=colorgroup),
             position = position_jitterdodge(jitter.width = 0.1)) +
  facet_grid(xfacet ~ yfacet) +
  scale_color_manual(values = c("red", "black", "orange", "blue"))

enter image description here

Thank you for your consideration and assistance

1

1 Answers

3
votes

Try this approach. You can conditionate the alpha parameter acording to y values to assign the transparent level. In your approach you use filters. It is better try to set all aesthetic elements in ggplot2 in order to avoid issues such as not aligning. Here the code:

library(ggplot2)
#Plot
ggplot() +
  geom_point(data = df,
             aes(x=xval, y=yval,
                 color=colorgroup,alpha=ifelse(yval>0.5,0.5,1)),
             position = position_jitterdodge(jitter.width = 0.1)) +
  facet_grid(xfacet ~ yfacet) +
  scale_color_manual(values = c("red", "black", "orange", "blue"))+
  scale_alpha(guide = 'none')

Output:

enter image description here