How to Remove Empty Factors from Ggplot2 Facets

How can I remove empty factors from ggplot2 facets?

EDIT Updated to ggplot2 0.9.3

Here's another solution. It uses facet_grid and space = "free"; also it uses geom_point() and geom_errorbarh(), and thus there is no need for coord.flip(). Also, the x-axis tick mark labels appear on the lower panel only. In the code below, the theme command is not essential - it is used to rotate the strip text to appear horizontally. Using the test dataframe from above, the following code should produce what you want:

library(ggplot2)

p <- ggplot(test, aes(y = characteristic, x = es, xmin = ci_low, xmax = ci_upp)) +
geom_point() +
geom_errorbarh(height = 0) +
facet_grid(set ~ ., scales = "free", space = "free") +
theme_bw() +
theme(strip.text.y = element_text(angle = 0))

p

The solution is based on the example on page 124 in Wickham's ggplot2 book.

ggplot: how to remove unused factor levels from a facet?

Setting scales = free in facet grid will do the trick:

facet_grid( ~ fac, scales = "free")

Remove empty factors from clustered bargraph in ggplot2 with multiple facets

To illustrate my comment:

a<-ggplot(foo,aes(x = factor(Treatment),y = Count))
a+ facet_wrap(~Origin, scales="free_x") +
geom_bar(stat="identity",aes(fill=factor(Type)),position="dodge") +
theme_bw() +
theme(axis.text.x=element_text(angle=60,hjust=1))

Sample Image

Note that if you add coord_flip and switch to free_y you get a specific error about coord_flip not working with some types of free scales, which is the source of you problem.

ggplot: how to remove empty groups from a facet_wrap?

Maybe this:

#Code
p %>%
mutate(nystudie=as.character(study),
best.resp =as.factor(response)) %>%
bind_rows(., mutate(., nystudie="All")) %>%
group_by(nystudie,best.resp) %>%
summarise(N=n(),Val=unique(treatment)) %>%
ggplot(aes(nystudie, N, color = best.resp, fill= best.resp)) +
geom_col(position = position_dodge2(preserve = "single", padding = 0.1)) +
facet_wrap(~Val,ncol = 2,scales='free')

Output:

Sample Image

ggplot2: Deleting facets of unused factor level combinations from a plot (facet_grid)

In ggplot2 2.2.0, the names of the grobs in a plot have changed.

library(ggplot2)
library(grid)
d <- data.frame('factor_1' = factor(c('a', 'a', 'b')),
'factor_2' = factor(c('1', '2', '1')),
x = 1:3, y = 1:3)

p = ggplot(data = d, mapping = aes(x = x, y = y)) +
geom_point() +
facet_grid(facets = factor_1 ~ factor_2, drop = TRUE)

# Get ggplot grob
g = ggplotGrob(p)

# Get the layout dataframe.
# Note the names.
# You want to remove "panel-2-2"
g$layout

# gtable::gtable_show_layout(g) # Might also be useful

# Remove the grobs
# The grob needs to be remove,
# and the relevant row in the layout data frame needs to be removed
pos <- grepl(pattern = "panel-2-2", g$layout$name)
g$grobs <- g$grobs[!pos]
g$layout <- g$layout[!pos, ]


# Alternatively, replace the grobs with the nullGrob
g = ggplotGrob(p)
pos <- grep(pattern = "panel-2-2", g$layout$name)
g$grobs[[pos]] <- nullGrob()

# If you want, move the axis
# g$layout[g$layout$name == "axis-b-2", c("t", "b")] = c(8, 8)

# Draw the plot
grid.newpage()
grid.draw(g)

Sample Image

The answer in your link would need to be modified something like this:

n <- 1000
df <- data.frame(x = runif(n), y=rnorm(n), label = sample(letters[1:7],
size = n, replace = TRUE), stringsAsFactors=TRUE)
df$label.new <- factor(df$label, levels=sort(c(""," ",levels(df$label))))


p <- ggplot(df, aes(x=x, y=y)) + geom_point() +
facet_wrap(~ label.new, ncol=3,drop=FALSE)

g = ggplotGrob(p)

g$layout # Note the names and their positions (t, b, l, r)
# gtable::gtable_show_layout(g) # Might also be useful

pos <- g$layout$name %in% c("panel-1-1", "panel-1-2", "strip-t-1-1", "strip-t-2-1")
g$grobs <- g$grobs[!pos]
g$layout <- g$layout[!pos, ]

# Or replace the grobs with the nullGrob
g = ggplotGrob(p)
pos <- g$layout$name %in% c("panel-1-1", "panel-1-2", "strip-t-1-1", "strip-t-2-1")
g$grobs[pos] <- list(nullGrob())

# Move the axis
g$layout[g$layout$name == "axis-l-1-1", c("l", "r")] = c(10,10)

grid.newpage()
grid.draw(g)

removing unused factors from facet_grid with row and column specified

There are no unused factors in your sample data and the arguments are not giving you what you desire. Possible startup workaround can be:

# Your script
p <- ggplot(test_data, aes(x = position, y = factor(name))) +
geom_tile(aes(fill = base)) +
scale_fill_viridis_d() +
theme_bw() +
theme(
axis.title.y=element_blank(),
axis.text.y=element_blank(),
legend.title=element_blank(),
axis.title.x=element_text(margin = margin(t = 15)),
panel.grid=element_blank()
)
p + coord_flip() + facet_wrap(ntile ~ sample, scales = "free")

output

unused_factor_updated

Removing Unused Factors from a Facet in ggplot2

I think all you need is scales = "free_y":

p0 = ggplot(mydf, aes(x = x, y = y)) +
geom_point() +
facet_wrap(~ cat, ncol = 1,scales = "free_y")

p0

Sample Image

ggplot faceting - remove empty x-axis labels

You need to add the scales argument to facet_wrap(). Try

# Faceting
ggplot(data, aes(y=value, x=specie, color=specie, fill=specie)) +
geom_bar( stat="identity") +
facet_wrap(~condition, scales = "free")

How to remove empty facet with ggplot?

I found a solution for my question and I wanted to share it with you :

p <- qplot(data=data, x=F1, y=F2)+facet_wrap( ~ s1,ncol = 2)
z <- ggplotGrob(p)
gtable_show_layout(z)
z <- gtable_add_cols(z, unit(0.08, 'null'), 11)
gtable_show_layout(z)
z <- gtable_add_grob(z,
list(rectGrob(gp = gpar(col=NA, fill = gray(0.85), size = 0.5,face="bold")),
textGrob("g1",
rot = -90, gp = gpar(col = gray(0),fontsize=12,fontface = 'bold'))),
7, 12,9,12, name = paste(runif(2)))
gtable_show_layout(z)
z <- gtable_add_grob(z,
list(rectGrob(gp = gpar(col=NA, fill = gray(0.85), size = 0.5,face="bold")),
textGrob("g2",
rot = -90, gp = gpar(col = gray(0),fontsize=12,fontface = 'bold'))),
12, 12,18,12, name = paste(runif(2)))

z <- gtable_add_cols(z, unit(1/6, "line"),11)
grid.newpage()
grid.draw(z)

That's the graph I wanted to get

I hope that my solution will be useful for other people !!



Related Topics



Leave a reply



Submit