r - 改变 ggplot2 中嵌套面的美学

标签 r plot ggplot2 facet

我想在 ggplot2 中使用嵌套面板但是两个面板的名称必须在情节的相对两侧。这是一个可重现的示例:

library(ggplot2)
library(data.table)

# data for reproducible example
dt <- data.table(
  value = c("East", "West","East", "West", "NY", "LA","NY", "LA"),
  year = c(2008, 2008, 2013, 2013, 2008, 2008, 2013, 2013),
  index = c(12, 10, 18, 15, 10, 8, 12 , 14),
  var = c("Region","Region","Region","Region", "Metro","Metro","Metro","Metro"))

# change order or plot facets
dt[, var := factor(var, levels=c( "Region", "Metro"))]

# plot
ggplot(data=dt) +
  geom_point( aes(x=index, y= factor(year), color=index)) +
  facet_grid(value + var ~., scales = "free_y", space="free") 

enter image description here

请注意,在此示例中,我使用列 value + var创建构面,但将两个面板的标题绘制在一起。

预期产量 : 我想要实现的是:

enter image description here

最佳答案

使用 labeller = label_bquote(rows = .(var1)) 的可能解决方案,两次调用 geom_text以及一些进一步的定制:

ggplot(dt, aes(x = index, y = factor(year), color = index)) +
  geom_point() +
  geom_text(aes(x = 6, y = 1.5, label = value), color = 'black', hjust = 0) +
  geom_text(aes(x = 7, label = year), color = 'black') +
  geom_segment(aes(x = 7.5, xend = 7.5, y = 0.7, yend = 2.3), color = 'black') +
  geom_segment(aes(x = 7.45, xend = 7.5, y = 1, yend = 1), color = 'black') +
  geom_segment(aes(x = 7.45, xend = 7.5, y = 2, yend = 2), color = 'black') +
  scale_x_continuous(breaks = seq(8,18,2)) +
  facet_grid(value + var1 ~., scales = "free_y", space="free", labeller = label_bquote(rows = .(var1))) +
  theme_minimal() +
  theme(axis.title = element_blank(),
        axis.text.y = element_blank(),
        strip.background = element_rect(color = 'darkgrey', fill = 'lightgrey'),
        panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank())

这使:

enter image description here

注意:我使用了 var1而不是 var因为后者也是一个函数名。

另一种可能性是使用 gridExtra包以创建附加标签并将它们放在 y 轴标签的前面,带有 grid.arrange :
# create the main plot
mainplot <- ggplot(dt, aes(x = index, y = factor(year), color = index)) +
  geom_point(size = 2) +
  scale_x_continuous(breaks = seq(8,18,2)) +
  facet_grid(value + var1 ~., scales = "free_y", space="free", labeller = label_bquote(rows = .(var1))) +
  theme_minimal() +
  theme(axis.title = element_blank(),
        strip.background = element_rect(color = 'darkgrey', fill = 'lightgrey'))

# create a 2nd plot with everything besides the labels set to blank or NA
lbls <- ggplot(dt, aes(x = 0, y = factor(year))) +
  geom_point(color = NA) +
  geom_text(aes(x = 0, y = 1.5, label = value), color = 'black') +
  scale_x_continuous(limits = c(0,0), breaks = 0) +
  facet_grid(value + var1 ~.) +
  theme_minimal() +
  theme(axis.title = element_blank(),
        axis.text.x = element_text(color = NA),
        axis.text.y = element_blank(),
        strip.background = element_blank(),
        strip.text = element_blank(),
        panel.grid = element_blank(),
        legend.position = 'none')

# plot with 'grid.arrange' and give the 'lbls'-plot a small width
library(gridExtra)
grid.arrange(lbls, mainplot, ncol = 2, widths = c(1,9))

这使:

enter image description here

关于r - 改变 ggplot2 中嵌套面的美学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38412474/

相关文章:

r - 绘图轴标题上的特殊字符和上标

r - 手动增加geom_tile中的图例范围

r - 将变量名称粘贴到 mutate(across()) 内

c++ - Rcpp - 从矩阵/数据帧列表中提取行

r - 多面板点阵图的不同 strip 高度

r - ggplot 线图 : is there a way to depict the data points under or over the line plot depending on what looks better?

r - 如何使长文本适合 Ggplot2 分面标题

r - geom_smooth() 中的 "prediction from a rank-deficient fit may be misleading"

python - 用 python 绘制数学数据

r - 在 R 中的轴标签中绘制表情符号/自定义图像