簡體   English   中英

只顯示一些圖例內容

[英]Only displaying some legend contents

我有興趣在我的 ggplot2 圖例中僅顯示前 3 個最豐富的組。

例如,在此表中,我有 7 個組,我只想在我的 ggplot2 圖例中顯示組 D、E、F

團體 樣本量
一個 2
3
C 1
D 25
23
F 20
G 3

我嘗試在線搜索,但我得到的最接近的答案是重新排序圖例。

提前致謝!

干杯,梅爾

您可以通過設置填充比例的breaks來實現這一點:

df <- data.frame(
             group = c("A", "B", "C", "D", "E", "F", "G"),
       sample.size = c(2L, 3L, 1L, 25L, 23L, 20L, 3L)
)

library(ggplot2)
library(dplyr)

top_group <- df %>% top_n(3, sample.size) %>% pull(group)

ggplot(df, aes(group, sample.size, fill = group)) +
  geom_col() +
  scale_fill_discrete(breaks = top_group)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM