R ggplot2:如何根据facet_wrap变量的值命名y轴?


10

我将为您提供有关数据的想法,然后我认为应该更容易理解我要实现的目标。

Repex:

ID <- c(1, 1, 2, 3, 3, 3)
cat <- c("Others", "Others", "Population", "Percentage", "Percentage", "Percentage")
logT <- c(2.7, 2.9, 1.5, 4.3, 3.7, 3.3)
m <- c(1.7, 1.9, 1.1, 4.8, 3.2, 3.5)
aggr <- c("median", "median", "geometric mean", "mean", "mean", "mean")
over.under <- c("overestimation", "overestimation", "underestimation", "underestimation", "underestimation", "underestimation")
data <- cbind(ID, cat, logT, m, aggr, over.under)
data <- data.frame(data)
data$ID <- as.numeric(data$ID)
data$logT<- as.numeric(data$logT)
data$m<- as.numeric(data$m)

码:

Fig <- data %>% ggplot(aes(x = logT, y = m, color = over.under)) + 
  facet_wrap(~ ID) +
  geom_point() +
  scale_x_continuous(name = "log (True value)", limits=c(1, 7)) +
  scale_y_continuous(name = NULL, limits=c(1, 7)) +
  geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
  theme_bw() +
  theme(legend.position='none')
Fig

我想用的值标记每个图的y轴aggr。因此,对于ID 1,应该说中位数,对于ID 2的几何平均值和ID 3的平均值。

我尝试了多种方法:

mtext(data1$aggr, side = 2, cex=1) #or
ylab(data1$aggr) #or
strip.position = "left"

但这是行不通的。

我也在尝试cat在图表的左上角添加。因此,对于ID 1“其他”,ID 2“填充”和ID 3“百分比”。我曾尝试与之合作,legend()但我也无法解决该问题。

Answers:


9

mtext是为plot()。ggplot是另一个绘图系统,因此它将不起作用。不幸的是,没有很多选择,一种方法是删除xlab,并将条带用作y轴:

LAB =tapply(as.character(data$aggr),data$ID,unique)

Fig <- data %>% ggplot(aes(x = logT, y = m, color = over.under)) + 
  geom_point() +
  scale_x_continuous(name = "log (True value)", limits=c(1, 7)) +
  scale_y_continuous(name = NULL, limits=c(1, 7)) +
  geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
  theme_bw() +
  theme(legend.position='none') +
  facet_wrap(~ID, scales = "free_y",strip.position = "left", 
  labeller = as_labeller(LAB ))  +
  ylab(NULL) +
  theme(strip.background = element_blank(),strip.placement = "outside")

在此处输入图片说明

另一种方法是组合图:

library(gridExtra) 

plts = by(data,data$ID,function(i){
ggplot(i,aes(x=logT,y=m,color=over.under)) + 
geom_point() + 
scale_x_continuous(name = "log (True value)", limits=c(1, 7)) +
scale_y_continuous(name = unique(i$agg), limits=c(1, 7)) +
geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
theme_bw() + 
scale_color_manual(values=c("overestimation"="turquoise","underestimation"="orange"))+
theme(legend.position='none') 
})

grid.arrange(grobs=plts,ncol=3)

在此处输入图片说明


2

如果我们关心ID刻面标签,这会变得更加复杂,并受此答案的启发。

首先,我们需要制作该图的两个副本,一个带有重命名的小条,一个带有原始的小条。

然后,我们将小平面条手动添加到另一个。

library(gtable)
library(grid)
plot1 <- data %>% ggplot(aes(x = logT, y = m, color = over.under)) + 
  facet_wrap(~ ID, scales = "free_y",strip.position = "left",  labeller = as_labeller(c(`1`="median",`2`="geometric mean",`3`="mean"))) +
  geom_point() +
  scale_x_continuous(name = "log (True value)", limits=c(1, 7)) +
  scale_y_continuous(name = NULL, limits=c(1, 7)) +
  geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
  theme_bw() +  
  theme(legend.position='none',strip.background = element_blank(),strip.placement = "outside")

plot2 <- data %>% ggplot(aes(x = logT, y = m, color = over.under)) + 
  facet_grid(~ ID) +
  geom_point() +
  scale_x_continuous(name = "log (True value)", limits=c(1, 7)) +
  scale_y_continuous(name = NULL, limits=c(1, 7)) +
  geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
  theme_bw() +  
  theme(legend.position='none')

gt1 = ggplot_gtable(ggplot_build(plot1))
gt2 = ggplot_gtable(ggplot_build(plot2))
strip1 <- gtable_filter(gt2, 'strip-t-1')
strip2 <- gtable_filter(gt2, 'strip-t-2')
strip3 <- gtable_filter(gt2, 'strip-t-3')
gt1 = gtable_add_rows(gt1, heights=strip1$heights[1], pos = 0)
panel_id <- gt1$layout[grep('panel-.+1$', gt1$layout$name),]
gt1 = gtable_add_grob(gt1, strip1, t = 1, l = panel_id$l[1])
gt1 = gtable_add_grob(gt1, strip2, t = 1, l = panel_id$l[2])
gt1 = gtable_add_grob(gt1, strip3, t = 1, l = panel_id$l[3])
gt1 = gtable_add_grob(gt1, zeroGrob(), t = 1, l = 1)
gt1 = gtable_add_rows(gt1, heights=gt2$heights[1], pos = 0)
grid.newpage()
grid.draw(gt1)

在此处输入图片说明

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.