简体   繁体   English

从ggplot2中的图例中删除标签

[英]Removing Labels from Legend in ggplot2

I have the data frame below that I have graphed as shown. 我有下面绘制的数据框,如图所示。 How can I limit the values shown in the legend to only the first three? 如何将图例中显示的值限制为仅前三个? In other words, I want it to only show "A", "B", and "C". 换句话说,我希望它仅显示“ 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

Was confused at first, but you want to show a top-3 so the other names don't need a legend. 一开始很困惑,但是您想显示前三名,因此其他名称不需要图例。 Here you go: 干得好:

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