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

zqdjd7g9  于 2023-05-04  发布在  其他
关注(0)|答案(5)|浏览(137)

我有这个 Dataframe :

x <- data.frame(
  Date = factor(rep(
    c("12/1/2011", "1/2/2012", "2/1/2012", "2/10/2012", "2/13/2012"),
    3
  )),
  Server = factor(rep(c("A", "B", "C"), each = 5L)),
  FileSystem = factor(c(
    "/", "/var", "tmp", "/db", "/app", "C:", "D:", "F:", "/restore",
    "G:", "/", "/tmp", "/data", "/Storage", "/database"
  )),
  PercentUsed = c(
    60L, 50L, 90L, 86L, 90L, 67L, 67L, 34L, 89L, 56L, 90L, 78L,
    67L, 34L, 12L
  )
)
x
#>         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

我想在每个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)
hec6srdp

hec6srdp1#

Meh,@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))

bd1hkmkf

bd1hkmkf2#

最好的方法是使用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)

ycggw6v2

ycggw6v23#

我们可以不使用面,而是为每个组创建一个图列表,然后使用 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))

正如@Axeman所建议的,我们可以使用facet_grid(~Server)而不是labels = levels(x$Server)来添加标签。

pvabu6sv

pvabu6sv4#

我喜欢@joran的回答,并提供了几个基于他们代码的选项作为起点。两个选项都解决了未对准面的问题。

面外图例

如果您为图例项选择等宽字体,则可以使用str_pad在所有图例项的右侧添加填充,以强制每个图例项的长度保持一致。
如果您愿意使用等宽字体,这是一个快速解决方案。

library(ggplot2)
library(dplyr)
library(gridExtra)
library(stringr)

l <- max(nchar(as.character(x$FileSystem)))
mylevels <- as.character(levels(x$FileSystem))
mylevels <- str_pad(mylevels, width = l, side = "right", pad = " ")
x <- mutate(x, FileSystem = factor(str_pad(FileSystem, width = l, side = "right", pad = " "),
            levels = mylevels))
windowsFonts("Lucida Sans Typewriter" = windowsFont("Lucida Sans Typewriter"))
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) +
  theme(legend.text = element_text(family = "Lucida Sans Typewriter"))

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

grid.arrange(p1,p2,p3)

facet内部图例

如果你不介意每个facet中的图例,你可以在scale调用中使用“expand”参数为每个facet添加额外的空间:

library(lubridate)
x <- mutate(x, Date = as.Date(as.character(Date), format = "%m/%d/%Y"))
xs <- split(x,f = x$Server)
p1 <- ggplot(xs$A,aes(x = Date,y = PercentUsed,group = 1,colour = FileSystem)) + 
  geom_jitter(size=0.5) + 
  scale_x_date(expand = expansion(add = c(5, 20)),
               date_labels = "%d-%m-%Y") +
  geom_smooth(method="loess", se=T) + 
  facet_wrap(~Server, ncol=1) +
  theme_bw() +
  theme(legend.position = c(0.9, 0.5))

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

grid.arrange(p1,p2,p3)

nmpmafwu

nmpmafwu5#

除了gridExtracowplot,现在游戏中还有patchwork。因此,您可以执行以下操作:

require(ggplot2)
require(patchwork)
# split
dfs = split(df, f = df$Server)
# apply ggplot function and write to list
gg_l = lapply(dfs, function(x) {
  ggplot(x, aes(x = Date,y = PercentUsed, group = 1, colour = FileSystem)) + 
    geom_jitter(size = 0.5) + 
    geom_smooth(method = "loess", se = TRUE) + 
    facet_wrap(~ Server, ncol = 1)
})
# patchwork
wrap_plots(gg_l, ncol = 1)

您也可以手动合并图,看看here。我使用了df的OP数据。

相关问题