我've got some data that'分为三个子类别(A,B和C) . 我通过这些子类别绘制它的功能,到目前为止一切都很好 .

但是,每个 A 的子类别不重叠 . 当使用不同的线条类型或颜色绘制不同的类别时,很难找出它们之间的区别(特别是示例中的B6和B8) .

代码我到目前为止,(原谅半obfuscatd名称,它是半敏感的)基本加载:

library('ggplot2')
library('extrafonts')
loadfonts()
all_d <- read.table(url('http://pastebin.com/raw.php?i=WArS8GZM'), header=TRUE)

所以这就是我对平面情节的看法:

ggplot(all_d, aes(x=Q, y=T, linetype=B)) +
  stat_summary(fun.y=median, geom="line") +
  facet_wrap(A~M, scales='free', ncol=2)

这导致下面的情节,因为你可以看到线条没有真正区分太好 .
enter image description here

为了解决这个问题,我发现的唯一解决方案是将它们单独绘制,因为ggplot不处理重新启动的行类型(?)

有趣的来了 . 轴具有不同的刻度,并且图例具有不同的长度,因此排列图像成为至少可以说的问题 . 经过一番修剪和整理:

g_legend <- function(a.gplot){ 
  tmp <- ggplot_gtable(ggplot_build(a.gplot)) 
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 
  return(tmp$grobs[[leg]])
}    

get_plot <- function(data, C) {
  data <- data[which(data$M == C),]
  p <- ggplot(data, aes(x=Q, y=T, linetype=B)) + 
    theme(legend.position='bottom') +
    stat_summary(fun.y=median, geom="line") +
    labs(x='Q', y='T')
  p
}

a1 <- all_d[which(all_d$A == 'A1'),]
a2 <- all_d[which(all_d$A == 'A2'),]
a3 <- all_d[which(all_d$A == 'A3'),]

plots <- list(get_plot(a1, 'C1'), get_plot(a1, 'C2'), 
          get_plot(a2, 'C1'), get_plot(a2, 'C2'),
          get_plot(a3, 'C1'), get_plot(a3, 'C2'))
grobs <- list()
widths <- list()
legend <- list()

for (i in 1:length(plots)){
  legend[[i]] <- g_legend(plots[[i]])
  grobs[[i]] <- ggplotGrob(plots[[i]] + theme(legend.position='none'))
  widths[[i]] <- grobs[[i]]$widths[2:5]
}
maxwidth <- do.call(grid::unit.pmax, widths)
for (i in 1:length(grobs)){
  grobs[[i]]$widths[2:5] <- as.list(maxwidth)
}
t <- gtable(widths=unit(c(1,1), "null"), heights=unit(rep(c(.5,6,.5), 3), "null"))
t <- gtable_add_grob(t, textGrob("A1"), 1, 1, 1, 2)
t <- gtable_add_grob(t, grobs[[1]], 2, 1)
t <- gtable_add_grob(t, grobs[[2]], 2, 2)
t <- gtable_add_grob(t, legend[[1]], 3, 1, 3, 2)
t <- gtable_add_grob(t, textGrob("A2"), 4, 1, 4, 2)
t <- gtable_add_grob(t, grobs[[3]], 5, 1)
t <- gtable_add_grob(t, grobs[[4]], 5, 2)
t <- gtable_add_grob(t, legend[[3]], 6, 1, 6, 2)
t <- gtable_add_grob(t, textGrob("A3"), 7, 1, 7, 2)
t <- gtable_add_grob(t, grobs[[5]], 8, 1)
t <- gtable_add_grob(t, grobs[[6]], 8, 2)
t <- gtable_add_grob(t, legend[[5]], 9, 1, 9, 2)

grid.newpage()
grid.draw(t)

这导致下面的情节,一切都排成一行,有相同的情节区域,传说至少,不是问题在底部 .

enter image description here

However ,我想使用Computer Modern字体绘制这些图,以便它适合我的乳胶文件的其余部分 . 将 + theme_grey(base_family='CM Roman') 添加到单独的图表会导致:

Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
  polygon edge not found
In addition: Warning message:
In grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
  no font could be found for family "CM Roman"

但是从 extrafonts 执行 loadfonts() 表示已加载CM罗马字体 . 该字体是使用 font_install('fontcm') 安装的

添加指定字体的相同主题适用于第一个刻面图 .