简体   繁体   中英

How to remove extra column in facet_wrap plot with ggplot2?

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. 在此处输入图片说明

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? 在此处输入图片说明

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)

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")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM