在ggplot2中为每个facet_wrap网格放置一个图例

use*_*980 25 r legend facet ggplot2 facet-wrap

我有这个数据框:

        Date Server FileSystem PercentUsed
1  12/1/2011      A          /          60
2   1/2/2012      A       /var          50
3   2/1/2012      A        tmp          90
4  2/10/2012      A        /db          86
5  2/13/2012      A       /app          90
6  12/1/2011      B         C:          67
7   1/2/2012      B         D:          67
8   2/1/2012      B         F:          34
9  2/10/2012      B   /restore          89
10 2/13/2012      B         G:          56
11 12/1/2011      C          /          90
12  1/2/2012      C       /tmp          78
13  2/1/2012      C      /data          67
14 2/10/2012      C   /Storage          34
15 2/13/2012      C  /database          12

dput(x)
structure(list(Date = structure(c(2L, 1L, 3L, 4L, 5L, 2L, 1L, 
3L, 4L, 5L, 2L, 1L, 3L, 4L, 5L), .Label = c("1/2/2012", "12/1/2011", 
"2/1/2012", "2/10/2012", "2/13/2012"), class = "factor"), Server = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", 
"B", "C"), class = "factor"), FileSystem = structure(c(1L, 9L, 
14L, 5L, 2L, 10L, 11L, 12L, 6L, 13L, 1L, 8L, 3L, 7L, 4L), .Label = c("/", 
"/app", "/data", "/database", "/db", "/restore", "/Storage", 
"/tmp", "/var", "C:", "D:", "F:", "G:", "tmp"), class = "factor"), 
    PercentUsed = c(60L, 50L, 90L, 86L, 90L, 67L, 67L, 34L, 89L, 
    56L, 90L, 78L, 67L, 34L, 12L)), .Names = c("Date", "Server", 
"FileSystem", "PercentUsed"), class = "data.frame", row.names = c(NA, 
-15L))
Run Code Online (Sandbox Code Playgroud)

我想在每个facet_wrap网格旁边放一个图例,它自己FileSystem:

当我这样做时,它将图例放在情节的一侧FileSystem.是否可以将FileSystem属于每个网格旁边的每个服务器?

ggplot(x, aes(Date, PercentUsed, group=1, colour=FileSystem)) + 
     geom_jitter(size=0.5) + geom_smooth(method="loess", se=T) + 
     facet_wrap(~Server, ncol=1)
Run Code Online (Sandbox Code Playgroud)

jor*_*ran 27

最好的方法是使用gridExtra包:

library(gridExtra)

xs <- split(x,f = x$Server)
p1 <- ggplot(xs$A,aes(x = Date,y = PercentUsed,group = 1,colour = FileSystem)) + 
        geom_jitter(size=0.5) + 
        geom_smooth(method="loess", se=T) + 
        facet_wrap(~Server, ncol=1)

p2 <- p1 %+% xs$B
p3 <- p1 %+% xs$C

grid.arrange(p1,p2,p3)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 我对 `%+%` 运算符有点感兴趣。你能解释一下它的作用吗? (3认同)
  • @Legend这是一种使ggplot对象"模块化"的方法,你可以使用它来简单地放入一个新的数据框,但使用前一个绘图中的所有相同的geom规范.当然,只有当列名全部匹配时,并且如果您没有在其他层中使用任何_other_数据帧,它才会起作用. (2认同)

Rom*_*rik 26

嗯,@ joran打败了我(我gridExtra已经过时但是花了我10分钟才意识到这一点).这是一个类似的解决方案,但是这个通常会按照水平来划分猫Server.

library(gridExtra)
out <- by(data = x, INDICES = x$Server, FUN = function(m) {
      m <- droplevels(m)
      m <- ggplot(m, aes(Date, PercentUsed, group=1, colour = FileSystem)) + 
         geom_jitter(size=2) + geom_smooth(method="loess", se=T)
   })
do.call(grid.arrange, out)

# If you want to supply the parameters to grid.arrange
do.call(grid.arrange, c(out, ncol=3))
Run Code Online (Sandbox Code Playgroud)

图片


zx8*_*754 5

除了使用构面外,我们还可以列出每组的绘图列表,然后使用cowplot :: plot_grid进行绘图。每个人都有自己的传说:

# make list of plots
ggList <- lapply(split(x, x$Server), function(i) {
  ggplot(i, aes(Date, PercentUsed, group = 1, colour = FileSystem)) + 
    geom_jitter(size = 2) +
    geom_smooth(method = "loess", se = TRUE)})

# plot as grid in 1 columns
cowplot::plot_grid(plotlist = ggList, ncol = 1,
                   align = 'v', labels = levels(x$Server))
Run Code Online (Sandbox Code Playgroud)

如@Axeman所建议,我们可以使用facet_grid(~Server)代替来添加标签labels = levels(x$Server)

在此处输入图片说明