The following data frame is represented in a tile plot. Group B data has a different scale and therefore the Y-axis must be free. The plot is separated by facets according to the group. However, the group B tile plot appears as thin bands rather than looking like the group A plot. How can I make it so that, despite the Y-axis being free, the plot of group B fills all the white space as in group A?
library(ggplot2)
X <- 1:3
Y1 <- 1:3
Y2 <- seq(10, 30, 10)
Y <- c(rep(Y1,3), rep(Y2,3))
Grid <- 1:3
Group <- c("A", "B")
DF <- expand.grid(Grid = Grid,
X = X,
Group = Group)
DF$Y <- Y
DF$Grid <- NULL
DF$Z <- 1:18
ggplot(data = DF,
aes(x = X,
y = Y,
fill = Z))
geom_tile()
facet_wrap(~ Group,
scales = "free_y")
CodePudding user response:
One option to achieve your desired result would be to convert Y
to a factor
:
library(ggplot2)
ggplot(data = DF,
aes(x = X,
y = factor(Y),
fill = Z))
geom_tile()
facet_wrap(~ Group,
scales = "free_y")
CodePudding user response:
library(tidyverse)
DF %>%
complete(X = 1:3,Y = 1:30,Group = c("A","B"),fill = list(Z = NA_real_)) %>%
filter(!(Group == "A" & is.na(Z))) %>%
ggplot(aes(x = X,
y = Y,
fill = Z))
geom_tile(col = "white")
facet_wrap(~ Group,
scales = "free_y")