简体   繁体   中英

R ggplot facet_wrap y ticks on different sides

For some reason, I have to make a plot that looks more or less like this: 在此处输入图片说明

For this I use the following code:

library(ggplot2)
library(tidyverse)
set.seed(10)
df<-data.frame(Meas = runif(1000,0,10),
               Prop1 = sample(x = LETTERS[1:3],1000,replace=TRUE),
               Prop2 = sample(x = letters[1:5],1000,replace=TRUE),
               Prop3 = sample(x=c("monkey","donkey","flipper"),1000,replace=TRUE))%>%
  gather(Prop,Propvalue,-Meas)

ggplot(df,aes(x = Propvalue,y=Meas))+
  geom_boxplot()+
  facet_wrap(~Prop,ncol=2,scales="free_y")+
  coord_flip()

I believe this would look better if the y-ticks on the right graph would appear on the right (for the graphs on the left, the y-ticks should remain where they are, but flipper and donkey should appear on the right side to avoid the gap between the left and right panels), but I can't find a way to do this.

I believe this will do the trick.

library(ggplot2)
library(tidyverse)
library(tidyr)

set.seed(10)
df <-data.frame(Meas = runif(1000,0,10),
               Prop1 = sample(x = LETTERS[1:3],1000,replace=TRUE),
               Prop2 = sample(x = letters[1:5],1000,replace=TRUE),
               Prop3 = sample(x=c("monkey","donkey","flipper"),1000,replace=TRUE))%>%
  gather(Prop,Propvalue,-Meas)

ggplot(df,aes(x = Propvalue,y=Meas))+
  geom_boxplot()+
  facet_wrap(~Prop,ncol=2,scales="free_y")+
  coord_flip()


  p.list = lapply(sort(unique(df$Prop)), function(i) { # i <- "Prop1"
  ggplot(df[df$Prop==i,],aes(x = Propvalue, y=Meas))+
    geom_boxplot()+
    facet_wrap(~Prop,scales="free_y")+
    coord_flip()
})


  p.list[[2]] <- p.list[[2]]   + scale_x_discrete(position = "top")


library(gridExtra)
do.call(grid.arrange, c(p.list, nrow=2))

在此处输入图片说明

Here's a hack that utilises ggplot's sec.axis argument, which creates a secondary axis opposite the primary axis & has to be a one-to-one mapping of it. I call this a hack, because this works only for continuous axis, so we need to map the categorical Propvalue to numeric values.

Note: I assumed in this example that you want all odd numbered PropX facets' labels on the left, & even numbered PropX facets' labels on the right. You can also tweak the options for other variations.

library(ggplot2)
library(tidyverse)

# generate data
set.seed(10)
df<-data.frame(Meas = runif(1000,0,10),
               Prop1 = sample(x = LETTERS[1:3],1000,replace=TRUE),
               Prop2 = sample(x=c("monkey","donkey","flipper"),1000,replace=TRUE),
               Prop3 = sample(x = letters[1:5],1000,replace=TRUE))%>%
  gather(Prop,Propvalue,-Meas)

# map Propvalue to integers, primary axis contents, & secondary axis contents.
df2 <- df %>%
  mutate(Propvalue.int = as.integer(factor(Propvalue,
                                           levels = df %>% select(Prop, Propvalue) %>% 
                                             arrange(Prop, Propvalue) %>% unique() %>% 
                                             select(Propvalue) %>% unlist())),
         facet.column = ifelse(as.integer(str_extract(Prop, "[0-9]")) %% 2 == 0, 2, 1),
         Propvalue.left = ifelse(facet.column == 1, Propvalue, ""),
         Propvalue.right = ifelse(facet.column == 2, Propvalue, ""))

# create mapping table
integer2factor <- df2 %>% 
  select(Propvalue.int, Propvalue.left, Propvalue.right) %>% 
  unique() %>% arrange(Propvalue.int)

ggplot(df2,aes(x = Propvalue.int, y=Meas, 
               group = Propvalue.int))+
  geom_boxplot() +
  scale_x_continuous(breaks = integer2factor$Propvalue.int,
                     labels = integer2factor$Propvalue.left,
                     name = "Propvalue",
                     sec.axis = dup_axis(breaks = integer2factor$Propvalue.int,
                                         labels = integer2factor$Propvalue.right,
                                         name = "")) +
  facet_wrap(~Prop,ncol=2,scales="free")+
  coord_flip() +
  theme(axis.ticks.y = element_blank())

分面包装图

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