R ggplot facet_wrap y 在不同的边上打勾
R ggplot facet_wrap y ticks on different sides
出于某种原因,我不得不制作一个看起来或多或少像这样的情节:
为此,我使用以下代码:
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 刻度出现在右边会更好看(对于左边的图,y 刻度应该保持在原处,但是 flipper 和 donkey 应该出现在右侧以避免左右面板之间的间隙),但我找不到这样做的方法。
我相信这会成功。
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))
这是一个利用 ggplot 的 sec.axis
参数的 hack,它创建了一个与主轴相对的辅助轴,并且必须是它的一对一映射。我称之为 hack,因为这仅适用于连续轴,因此我们需要将分类 Propvalue 映射到数值。
注意:我假设在此示例中,您希望所有奇数编号的 PropX 刻面标签都在左侧,而偶数编号的 PropX 刻面标签在右侧。您还可以调整其他变体的选项。
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())
出于某种原因,我不得不制作一个看起来或多或少像这样的情节:
为此,我使用以下代码:
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 刻度出现在右边会更好看(对于左边的图,y 刻度应该保持在原处,但是 flipper 和 donkey 应该出现在右侧以避免左右面板之间的间隙),但我找不到这样做的方法。
我相信这会成功。
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))
这是一个利用 ggplot 的 sec.axis
参数的 hack,它创建了一个与主轴相对的辅助轴,并且必须是它的一对一映射。我称之为 hack,因为这仅适用于连续轴,因此我们需要将分类 Propvalue 映射到数值。
注意:我假设在此示例中,您希望所有奇数编号的 PropX 刻面标签都在左侧,而偶数编号的 PropX 刻面标签在右侧。您还可以调整其他变体的选项。
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())