How to Remove Na from Facet_Wrap in Ggplot2

Suppress NA column when faceting

It's hard to do this exactly without your data, but one option would be to convert to a gtable object and then just trim off that extra column you don't want.

My sample code will use R's built in mtcars.

library(ggplot2)
library(gtable)
p<-ggplot(mtcars, aes(wt, hp)) + geom_point()+ facet_grid(cyl ~ carb, scales = "free")
gt<-ggplot_gtable(ggplot_build(p))

Without Trim

grid.draw(gt)

Sample Image

With Trim

gt2<-gt[,-14]
grid.draw(gt2)

Sample Image

You can see that with trimming we simply took off the last column that we don't want (column 14, which had the 8 cyl plot in it). To figure out which column you want to trim you can do:

gtable_show_layout(gt)

Using this you can match the specific facets of your plot with the columns in gtable that you want to remove.
Sample Image

Is there a way to omit variables with NA values from facet wrap plots?

You just need to free the x axis:

plot + facet_wrap(~operon, ncol=2, scales = "free_x")

Without specifying scales = "free_x", ggplot defaults to identical axes with the same limits and breaks.

remove legend entries and facets when all data are NA

With the updated example, you can use either na.omit or complete.cases to get the desired result. With:

ggplot(df[complete.cases(df[,c('w','y')]),], aes(w, y)) +
geom_point(aes(color = group)) +
facet_grid(.~group, drop = TRUE) +
theme_bw()

or:

ggplot(na.omit(df[,c('group','w','y')]), aes(w, y)) +
geom_point(aes(color = group)) +
facet_grid(.~group, drop = TRUE) +
theme_bw()

you get:

Sample Image


Old answer: Use na.omit(df) instead of df in you ggplot call:

ggplot(na.omit(df), aes(x, y)) +
geom_point(aes(color = group)) +
facet_grid(. ~ group) +
theme_bw()

will give you the desired result:

Sample Image

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

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.

How to Delete Facets from ggplot2 facet_wrap?

You can manually remove the last facet using the gtable() package as follows

plt <- plot(stacked) + ggplot2::facet_wrap(~ contest_no, nrow = 1L)

library(ggplot2)
library(grid)
library(gtable)

# create gtable object
gt = ggplot_gtable(ggplot_build(plt))
print(gt)

#> TableGrob (15 x 29) "layout": 46 grobs
#> z cells name
#> 1 0 ( 1-15, 1-29) background
#> 2 1 ( 8- 8, 5- 5) panel-1-1
#> 3 1 ( 8- 8, 9- 9) panel-2-1
#> 4 1 ( 8- 8,13-13) panel-3-1
#> 5 1 ( 8- 8,17-17) panel-4-1
#> 6 1 ( 8- 8,21-21) panel-5-1
#> 7 1 ( 8- 8,25-25) panel-6-1
#> 8 3 ( 6- 6, 5- 5) axis-t-1-1
#> 9 3 ( 6- 6, 9- 9) axis-t-2-1
#> 10 3 ( 6- 6,13-13) axis-t-3-1
#> 11 3 ( 6- 6,17-17) axis-t-4-1
#> 12 3 ( 6- 6,21-21) axis-t-5-1
#> 13 3 ( 6- 6,25-25) axis-t-6-1
#> 14 3 ( 9- 9, 5- 5) axis-b-1-1
#> 15 3 ( 9- 9, 9- 9) axis-b-2-1
#> 16 3 ( 9- 9,13-13) axis-b-3-1
#> 17 3 ( 9- 9,17-17) axis-b-4-1
#> 18 3 ( 9- 9,21-21) axis-b-5-1
#> 19 3 ( 9- 9,25-25) axis-b-6-1
#> 20 3 ( 8- 8,24-24) axis-l-1-6
#> 21 3 ( 8- 8,20-20) axis-l-1-5
#> 22 3 ( 8- 8,16-16) axis-l-1-4
#> 23 3 ( 8- 8,12-12) axis-l-1-3
#> 24 3 ( 8- 8, 8- 8) axis-l-1-2
#> 25 3 ( 8- 8, 4- 4) axis-l-1-1
#> 26 3 ( 8- 8,26-26) axis-r-1-6
#> 27 3 ( 8- 8,22-22) axis-r-1-5
#> 28 3 ( 8- 8,18-18) axis-r-1-4
#> 29 3 ( 8- 8,14-14) axis-r-1-3
#> 30 3 ( 8- 8,10-10) axis-r-1-2
#> 31 3 ( 8- 8, 6- 6) axis-r-1-1
#> 32 2 ( 7- 7, 5- 5) strip-t-1-1
#> 33 2 ( 7- 7, 9- 9) strip-t-2-1
#> 34 2 ( 7- 7,13-13) strip-t-3-1
#> 35 2 ( 7- 7,17-17) strip-t-4-1
#> 36 2 ( 7- 7,21-21) strip-t-5-1
#> 37 2 ( 7- 7,25-25) strip-t-6-1
#> 38 4 ( 5- 5, 5-25) xlab-t
#> 39 5 (10-10, 5-25) xlab-b
#> 40 6 ( 8- 8, 3- 3) ylab-l
#> 41 7 ( 8- 8,27-27) ylab-r
#> 42 8 (12-12, 5-25) guide-box
#> 43 9 ( 4- 4, 5-25) subtitle
#> 44 10 ( 3- 3, 5-25) title
#> 45 11 (13-13, 5-25) caption
#> 46 12 ( 2- 2, 2- 2) tag
#> grob
#> 1 rect[plot.background..rect.379]
#> 2 gTree[panel-1.gTree.42]
#> 3 gTree[panel-2.gTree.53]
#> 4 gTree[panel-3.gTree.64]
#> 5 gTree[panel-4.gTree.75]
#> 6 gTree[panel-5.gTree.86]
#> 7 gTree[panel-6.gTree.97]
#> 8 zeroGrob[NULL]
#> 9 zeroGrob[NULL]
#> 10 zeroGrob[NULL]
#> 11 zeroGrob[NULL]
#> 12 zeroGrob[NULL]
#> 13 zeroGrob[NULL]
#> 14 absoluteGrob[GRID.absoluteGrob.104]
#> 15 absoluteGrob[GRID.absoluteGrob.104]
#> 16 absoluteGrob[GRID.absoluteGrob.104]
#> 17 absoluteGrob[GRID.absoluteGrob.104]
#> 18 absoluteGrob[GRID.absoluteGrob.104]
#> 19 absoluteGrob[GRID.absoluteGrob.104]
#> 20 zeroGrob[NULL]
#> 21 zeroGrob[NULL]
#> 22 zeroGrob[NULL]
#> 23 zeroGrob[NULL]
#> 24 zeroGrob[NULL]
#> 25 absoluteGrob[GRID.absoluteGrob.146]
#> 26 zeroGrob[NULL]
#> 27 zeroGrob[NULL]
#> 28 zeroGrob[NULL]
#> 29 zeroGrob[NULL]
#> 30 zeroGrob[NULL]
#> 31 zeroGrob[NULL]
#> 32 gtable[strip]
#> 33 gtable[strip]
#> 34 gtable[strip]
#> 35 gtable[strip]
#> 36 gtable[strip]
#> 37 gtable[strip]
#> 38 zeroGrob[NULL]
#> 39 titleGrob[axis.title.x.bottom..titleGrob.292]
#> 40 titleGrob[axis.title.y.left..titleGrob.295]
#> 41 zeroGrob[NULL]
#> 42 gtable[guide-box]
#> 43 zeroGrob[plot.subtitle..zeroGrob.375]
#> 44 zeroGrob[plot.title..zeroGrob.374]
#> 45 zeroGrob[plot.caption..zeroGrob.377]
#> 46 zeroGrob[plot.tag..zeroGrob.376]

Show plot layout

library(lemon)
gtable_show_names(gt)

Sample Image

# we want to remove everything related to panel-6-
rm_grobs <- gt$layout$name %in% c("panel-6-1", "strip-t-6-1",
"axis-t-6-1", "axis-b-6-1",
"axis-l-1-6", "axis-r-1-6", "ylab-r")
# remove grobs
gt$grobs[rm_grobs] <- NULL
gt$layout <- gt$layout[!rm_grobs, ]

# check result
gtable_show_names(gt)

Sample Image

Next remove the space left over

names(gt)
#> [1] "grobs" "layout" "widths" "heights"
#> [5] "respect" "rownames" "colnames" "name"
#> [9] "gp" "vp" "children" "childrenOrder"

gt$widths
#> [1] 5.5pt 0cm 1grobwidth
#> [4] 3.93105593607306cm 1null 0cm
#> [7] 5.5pt 0cm 1null
#> [10] 0cm 5.5pt 0cm
#> [13] 1null 0cm 5.5pt
#> [16] 0cm 1null 0cm
#> [19] 5.5pt 0cm 1null
#> [22] 0cm 5.5pt 0cm
#> [25] 1null 0cm 0cm
#> [28] 0pt 5.5pt

gt$widths[25] = unit(0, "cm")
gt$widths[29] = unit(0, "cm")

# check result again
gtable_show_names(gt)

Sample Image

# final plot
grid.newpage()
grid.draw(gt)

Sample Image

Created on 2018-10-05 by the reprex package (v0.2.1.9000)

Is there a way to skip NA values for a line plot in ggplot2

The line geom will but cut if there are any NA values, so the simple solution is to remove the NA values from your data frame. You can use na.omit() to do this, but just pay attention to where you use it in your code. Your original dataset looks something like this:

df <- data.frame(pos=1:4, A=c(1.05, 2.3, 4.24, 3.89),
B=c(4.44, NA, 2.22, 3.33))
df

>
pos A B
1 1 1.05 4.44
2 2 2.30 NA
3 3 4.24 2.22
4 4 3.89 3.33

Plotting this after gathering you get:

df %>%
gather(key=type, value=value, -pos) %>%
ggplot(aes(x=pos, y=value)) +
geom_line(linetype=2, color='blue', size=0.7) +
geom_point(color='red', size=3) +
facet_wrap(~type)

Sample Image

If you used na.omit() on df, then it would remove the entire second row, which removes the second observation for column A as well. In this case, just make sure you use na.omit() after the gather() function to pivot the data frame longer:

df %>%
gather(key=type, value=value, -pos) %>%
na.omit() %>% # important this comes after the gather
ggplot(aes(x=pos, y=value)) +
geom_line(linetype=2, color='blue', size=0.7) +
geom_point(color='red', size=3) +
facet_wrap(~type)

Sample Image

In your case, the pseudo code below gives you an idea where to place na.omit() in your own code:

plots %>%
mutate(...) %>%
na.omit() %>%
ggplot(...) + ...


Related Topics



Leave a reply



Submit