簡體   English   中英

從ggplot2中的圖例中刪除標簽

[英]Removing Labels from Legend in ggplot2

我有下面繪制的數據框,如圖所示。 如何將圖例中顯示的值限制為僅前三個? 換句話說,我希望它僅顯示“ A”,“ B”和“ C”。

graph_table <- read.table(header=TRUE, text="
   names freq  rank percs sums sums_str
1      A 1208 'Top 3'  46.1 61.1    61.1%
2      B  289 'Top 3'  11.0 61.1    61.1%
3      C  105 'Top 3'   4.0 61.1    61.1%
4      D  388     D  14.8 14.8    14.8%
5      E  173     E   6.6  6.6     6.6%
6      F  102     F   3.9  3.9     3.9%
7      G   70     G   2.7  2.7     2.7%
8      H   54     H   2.1  2.1     2.1%
9      I   44     I   1.7  1.7     1.7%
10     J   32     J   1.2  1.2     1.2%
11     K   24     K   0.9  0.9     0.9%
12     L   20     L   0.8  0.8     0.8%
13     M   20     M   0.8  0.8     0.8%
14     N   18     N   0.7  0.7     0.7%
15     O   13     O   0.5  0.5     0.5%
16     P   10     P   0.4  0.4     0.4%
17     Q   10     Q   0.4  0.4     0.4%
18     R   10     R   0.4  0.4     0.4%
19     S    7     S   0.3  0.3     0.3%
20     T    5     T   0.2  0.2     0.2%
21     U    5     U   0.2  0.2     0.2%
22     V    5     V   0.2  0.2     0.2%
23     W    3     W   0.1  0.1     0.1%")

library(ggplot2) 

p <- ggplot(graph_table[1:10,], aes(x=rank, y=percs,   
            fill=names))+geom_bar(stat="identity")
p <- p+geom_text(aes(label=sums_str, y=(sums+4)), size=4)
p

一開始很困惑,但是您想顯示前三名,因此其他名稱不需要圖例。 干得好:

p <- ggplot(graph_table[1:10,], aes(x=rank, y=percs,   
                                    fill=names))+geom_bar(stat="identity")
p <- p+geom_text(aes(label=sums_str, y=(sums+4)), size=4)
p + scale_fill_discrete(breaks=c("A","B","C"))

在此處輸入圖片說明

暫無
暫無

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

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