【问题标题】:Cannot get my legend on the top right inside my boxplot无法将我的图例显示在箱线图的右上角
【发布时间】:2022-01-03 07:44:13
【问题描述】:

在我的箱线图右上角显示我的图例时遇到问题。我已经尝试在 legend.position 参数中使用“topright”术语。我也尝试输入特定的坐标,但它只是没有显示在情节上。当我使用“top”、“bottom”、“left”或“right”时,它没有问题。只是“topleft”和“topright”似乎是个问题。帮助将是惊人的。感谢我正在使用的脚本:

【问题讨论】:

标签: r ggplot2 boxplot


【解决方案1】:

不确定我是否理解问题;也许这些例子之一会有所帮助:

library(tidyverse)
library(palmerpenguins)
library(cowplot)

penguins %>%
  na.omit() %>%
  ggplot(aes(x = factor(sex), y = body_mass_g)) +
  geom_boxplot(outlier.shape = NA) +
  geom_jitter(aes(color = sex),
              width = 0.2,
              alpha = 0.4,
              size = 2) +
  labs(x = "Sex",
       y = "Body Mass (g)") +
  theme_bw(base_size = 14) +
  theme(legend.position = "right",
        legend.justification = "top")

penguins %>%
  na.omit() %>%
  ggplot(aes(x = factor(sex), y = body_mass_g)) +
  geom_boxplot(outlier.shape = NA) +
  geom_jitter(aes(color = sex),
              width = 0.2,
              alpha = 0.4,
              size = 2) +
  labs(x = "Sex",
       y = "Body Mass (g)") +
  theme_bw(base_size = 14) +
  theme(legend.position = c(0.91, 0.89))

reprex package (v2.0.1) 于 2021 年 11 月 25 日创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-12
    • 2022-01-16
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多