R ggplot facet_wrap y在不同方面打勾

时间:2017-08-25 09:40:08

标签: r ggplot2 facet-wrap

出于某种原因,我必须制作一个看起来或多或少像这样的情节: enter image description here

为此,我使用以下代码:

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

我相信如果右边的图表上的y-ticks出现在右边会更好看(对于左边的图表,y-ticks应该保留在它们的位置,但是鳍状肢和驴子应该出现在右边一边避免左右面板之间的差距),但我找不到办法做到这一点。

2 个答案:

答案 0 :(得分:1)

我相信这会成功。

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

enter image description here

答案 1 :(得分:1)

这是一个利用ggplot的sec.axis参数的hack,它创建了一个与主轴相对的辅助轴&amp;必须是它的一对一映射。我称之为hack,因为这仅适用于连续轴,因此我们需要将分类Propvalue映射到数值。

注意:我在此示例中假设您希望左侧的所有奇数编号的PropX构面标签,&amp;偶数编号为PropX facets右侧的标签。您还可以调整其他变体的选项。

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

facet wrap plot