如何在 R 中 plot_model (ggplot) 制作的绘图中删除图例键周围的正方形

How to remove the square surrounding legend keys in a plot made by plot_model (ggplot) in R

提问人:Zaida 提问时间:9/7/2023 更新时间:9/7/2023 访问量:48

问:

我目前正在使用 R 中包中的函数创建一些复杂的可视化效果,具体来说,我正在处理线性模型的交互作用图,并且我面临着与自定义图例相关的问题。 当我绘制它时,图例的键出现在正方形内,我想摆脱这些正方形。我已经通读了文档,尝试了各种方法,并搜索了 Stack Overflow 上的现有问题,但我仍在努力实现这一目标。plot_modelsjPlot

我尝试了几种方法来从我的图例中删除正方形和边框。其中包括使用 and 函数使图例键透明并删除边框线,但不幸的是,这些解决方案没有产生预期的结果。他们要么完全删除图例键,要么保留带有黑色键的灰色背景。theme(legend.key = element_blank())theme(legend.background = element_rect(color = NA))

我也尝试过并控制传奇美学,但这些方法也没有解决它。guides(color = guide_legend(override.aes = list(fill = NULL, color = NULL)))guides(color = guide_legend(override.aes = list(fill = NULL, line = 0)))

我检查过的一些帖子是:https://stackoverflow.com/a/11270029/18255654https://stackoverflow.com/a/2249622/18255654

这是我目前获得的数字:Plot with squares

为了测试脚本并将其发布在此处,我使用了两个可用的数据集,并且我使用了以下脚本:

rm(list=ls())

library(nlme)
library(lme4)
library(ggplot2)
library(sjPlot)
library(ggthemes) # Library of themes for ggplot

# Load dataset for:
data=sleepstudy # Example 1
data(efc) # Example 2

# Example 1
group_colors <- c(old = "#3b71cd", young = "#c0393d")
threshold_subject_number <- 9
data$Subject <- as.numeric(data$Subject)
data$Group <- ifelse(data$Subject <= threshold_subject_number, "young", "old")

analysis_model = lme(Reaction ~ Days * Group,
                     data=data,
                     random=~1+Days|Subject, 
                     control = lmeControl(opt = "optim"), 
                     method = "REML")

line_plot = 
  
  plot_model(analysis_model, # Model to plot
             type = "int", # Type of plot: int = interaction
             title = "Title", # Add title
             colors = group_colors, # Define the colors
             axis.title = c("Day", "Predicted Value")) +
  theme_hc() +
  theme(plot.title = element_text(hjust = 0.5)) + # Center plot title
  theme(legend.position = "right") + # Move legend to right side
  guides(color = guide_legend(override.aes = list(fill = NA, # Make square transparent
                                                  size = 2), # Change size of square
                              reverse = TRUE)) # Reverse order of legend

# Example 2
m = glm(tot_sc_e ~ c161sex + c172code * neg_c_7,
        data = efc,
        family = poisson())

mp = plot_model(m, type = "int") +
     theme_hc() + # Change theme
     theme(plot.title = element_text(hjust = 0.5)) + # Center plot title
     theme(legend.position = "right") + # Move legend to right side
     guides(color = guide_legend(override.aes = list(fill = NA, # Make square transparent
                                                     size = 2), # Change size of square
                                 reverse = TRUE)) # Reverse order of legend

## Different things I've tried:

# From: https://stackoverflow.com/a/11270029/18255654
mp +
  theme(legend.key = element_blank()) # Does not work - nothing happens

# From: https://stackoverflow.com/a/2249622/18255654
mp +
  theme(legend.background = element_rect(color = NA)) # Does not work - nothing happens

mp +
  theme(legend.key = element_rect(colour = NA, fill = NA)) # Does not work - nothing happens

mp + 
  guides(color = guide_legend(override.aes = list(fill = NA, color = NA))) # Does not work - removes also the keys

mp + 
  guides(color = guide_legend(override.aes = list(fill = NULL, color = NULL))) # Does not work - removes line but turns all keys to black and restores grey background

mp + 
  guides(color = guide_legend(override.aes = list(fill = NULL, line = 0))) # Does not work - removes line but turns all keys to black and restores grey background

R ggplot2 可视化 图例 sjplot

评论


答:

1赞 stefan 9/7/2023 #1

轮廓是由引擎盖下用于绘制置信带引起的。摆脱轮廓的一种选择是操作 返回的对象,即通过使用以下方法为该层(即第二层)设置:geom_ribbonggplotplot_modelshow.legend=FALSEmp$layers[[2]]$show.legend <- FALSE

library(ggplot2)
library(sjPlot)
library(ggthemes) # Library of themes for ggplot


mp <- plot_model(m, type = "int") +
  theme_hc() + # Change theme
  theme(plot.title = element_text(hjust = 0.5)) + # Center plot title
  theme(legend.position = "right") + # Move legend to right side
  guides(color = guide_legend(
    override.aes = list(
      fill = NA, # Make square transparent
      size = 2
    ), # Change size of square
    reverse = TRUE
  )) # Reverse order of legend

mp$layers[[2]]$show.legend <- FALSE

mp

评论

0赞 Zaida 9/8/2023
谢谢!它完美地工作:D