r - 如何从图例中删除已用值?

标签 r ggplot2 legend

简介

我知道 scale_*_*(drop = TRUE) 可用于从图例中删除空因子水平。也许违反直觉,我正试图从情节中删除使用过的级别。

您可能已经猜到了,这是一个有点 hack-y 的用例:我使用带有 geom_bar 的隐形条来抵消“ float ”李克特响应可视化。我对框架挑战或解决此问题的替代方法感兴趣,我特别询问如何从图例中删除已使用的关卡。

用例和示例

下面的代码再现了我的可视化的一个非常简单的版本。它工作得很好,但图例与中心的偏移很小(这在“真实”可视化中更明显)。我想放弃“隐形”级别来展示这一点。我知道我可以使用 legend.position 的数字参数来重新居中整个内容,但这很繁琐且不可推广。

## libraries ---
require(ggplot2)
#> Loading required package: ggplot2
require(dplyr)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

## data ---
plotData <- tibble(label = 
                     factor(c("", "strawberries", "blueberries", "", "strawberries", "blueberries"),
                             levels = c("strawberries", "blueberries", ""), ordered = TRUE),
                   value = c(30, 40, 20, 15, 30, 15),
                   bowl = factor(c("bowl1", "bowl1", "bowl1", "bowl2", "bowl2", "bowl2"))) 

## plot ---

### Specs for the legend
legendSpecs <- guide_legend(nrow = 1, label.position = "bottom",
                            reverse = TRUE, title = NULL)
### desired plot
ggplot(plotData, aes(x = value, y = bowl,
                     fill = label, colour = label)) +
  geom_bar(stat = "identity", position = "stack") +
  scale_fill_manual(values = c("blue", "red", NA), guide = legendSpecs) +
  scale_colour_manual(values = c("black",  "black", NA), guide = legendSpecs) +
  theme_minimal() +
  theme(legend.position = "bottom")

演示我试图摆脱的图例中的小偏移量:

### demonstrating legend offset
ggplot(plotData, aes(x = value, y = bowl,
                     fill = label, colour = label)) +
  geom_bar(stat = "identity", position = "stack") +
  scale_fill_manual(values = c("blue", "red", NA), guide = legendSpecs) +
  scale_colour_manual(values = c("black",  "black", "black"), guide = legendSpecs) +
  theme_minimal() +
  theme(legend.position = "bottom")

reprex package 创建于 2021-06-30 (v2.0.0)

最佳答案

这可以通过将比例的 breaks 设置为仅包含所需的类别来实现:

## libraries ---
require(ggplot2)
require(dplyr)

## data ---
plotData <- tibble(label = 
                     factor(c("", "strawberries", "blueberries", "", "strawberries", "blueberries"),
                            levels = c("strawberries", "blueberries", ""), ordered = TRUE),
                   value = c(30, 40, 20, 15, 30, 15),
                   bowl = factor(c("bowl1", "bowl1", "bowl1", "bowl2", "bowl2", "bowl2"))) 

## plot ---

### Specs for the legend
legendSpecs <- guide_legend(nrow = 1, label.position = "bottom",
                            reverse = TRUE, title = NULL)
### desired plot
ggplot(plotData, aes(x = value, y = bowl,
                     fill = label, colour = label)) +
  geom_bar(stat = "identity", position = "stack") +
  scale_fill_manual(values = c("blue", "red", NA), na.value = NA,
                    guide = legendSpecs, breaks = c("blueberries", "strawberries"))  +
  scale_colour_manual(values = c("black",  "black", NA), , na.value = NA, 
                      guide = legendSpecs, 
                      breaks = c("blueberries", "strawberries")) +
  theme_minimal() +
  theme(legend.position = "bottom")

关于r - 如何从图例中删除已用值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68196365/

相关文章:

R:绘制 OOB 错误率

r - 使用 purrr 将自定义函数重复应用于同一数据帧

r - 如何使用一些 apply 函数来解决 R 中需要两个 for 循环的问题

r - ggplot2 的多个图例

javafx - 如何换行 javafx 图表图例的文本

python - 全局图例和标题旁的次要情节

c - .C 到 R 的接口(interface)

r - 在此直方图中将计数更改为概率

label - jqplot 无法正确显示图例和 y 轴标签

r - 图例标题的垂直对齐(ggplot2)