How to remove extra column in facet_wrap plot with ggplot2?

Viewed 227

I am trying to generate a facet plot with facet_wrap with an unbalanced grouped data, and it provided a plot with extra blank axis column.

Like the paragraph showed, I want to generate a plot without the rightmost axis column. enter image description here

Here is an example code:

library(ggplot2)
name <- c(factor(letters[1:4]),factor(LETTERS[1:3]))
room <- rep(c('A','B'),c(4,3))
goal <- c(rnorm(7,mean=60,sd=10))
test <- data.frame(name,goal,room) 
test %>% ggplot(aes(name, goal))+
  facet_wrap(~factor(room))+
  geom_bar(stat = "identity")

'scales="free"' way: automatic set, can it be set manually? enter image description here

facetted_pos_scales in ggh4x developed by @teunbrand sovled the problem, thnaks! Here is the supplementary code:

library(ggh4x)

scales <- list(
  scale_y_continuous(limits = c(0, 100)),
  scale_y_continuous(limits = c(0, 80))
)

test %>% ggplot(aes(name, goal))+
  facet_wrap(~factor(room), scales="free")+
  geom_bar(stat = "identity")+
  facetted_pos_scales(y=scales)
1 Answers

Update on comment of op: Does this help: You can use coord_cartesian(ylim = c(0, 90)) to set the ylim:

test %>% ggplot(aes(name, goal))+
  geom_bar(stat = "identity")+
  coord_cartesian(ylim = c(0, 100)) +
  facet_wrap(~factor(room), scales="free")

Use scales="free" instead of scales="free_x"

library(ggplot2)
name <- c(factor(letters[1:4]),factor(LETTERS[1:3]))
room <- rep(c('A','B'),c(4,3))
goal <- c(rnorm(7,mean=60,sd=10))
test <- data.frame(name,goal,room) 
test %>% ggplot(aes(name, goal))+
  facet_wrap(~factor(room), scales="free")+
  geom_bar(stat = "identity")

enter image description here

Related