6730 3

[问答] R语言绘图 ggplot绘制堆积图,数据标签如何不显示值为0的标签 [推广有奖]

  • 0关注
  • 0粉丝

小学生

14%

还不是VIP/贵宾

-

威望
0
论坛币
10 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
46 点
帖子
3
精华
0
在线时间
6 小时
注册时间
2016-9-2
最后登录
2017-7-26

楼主
火云邪神二号 发表于 2016-9-2 11:33:33 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
------------------数据(data2)---------------------
plate family Freq family_percent label_y
w1 f1 1 20 10
w1 f2 1 20 30
w1 f3 2 40 60
w1 f5 1 20 90
w1 f6 0 0 100
w2 f1 2 40 20
w2 f2 1 20 50
w2 f3 1 20 70
w2 f5 0 0 80
w2 f6 1 20 90
-----------------代码-------------------
data2<-read.table(file="data2.txt",sep=" ",quote="")
p<-ggplot(data2,aes(x=plate,y=family_percent,fill=family))+geom_bar(stat="identity")+guides(fill=guide_legend())
p<-p+geom_text(aes(y=label_y,label=family_percent),colour="white")
p
-----------------结果----------------------------
ggplot_out.png
这样出来的结果会在标数据标签时把值为0的值也标上,如何让这些值不显示呢?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:gplot plot R语言 GPL 不显示 ggplot 数据标签

回帖推荐

le.chat 发表于3楼  查看完整内容

如果你是单纯针对这个问题解决方式,直接把family_percent为0对应的y_label改成NA就好了。

沙发
le.chat 发表于 2016-9-2 15:57:15
如果你只是单纯针对这个代码,直接把family_percent为0的对应y_label 改成NA就好啦。
  1. plate family Freq family_percent label_y
  2. w1 f1 1 20 10
  3. w1 f2 1 20 30
  4. w1 f3 2 40 60
  5. w1 f5 1 20 90
  6. w1 f6 0 0 NA
  7. w2 f1 2 40 20
  8. w2 f2 1 20 50
  9. w2 f3 1 20 70
  10. w2 f5 0 0 NA
  11. w2 f6 1 20 90
复制代码


Rplot.png (6.37 KB)

Rplot.png

藤椅
le.chat 发表于 2016-9-2 16:02:06
如果你是单纯针对这个问题解决方式,直接把family_percent为0对应的y_label改成NA就好了。
  1. plate family Freq family_percent label_y
  2. w1 f1 1 20 10
  3. w1 f2 1 20 30
  4. w1 f3 2 40 60
  5. w1 f5 1 20 90
  6. w1 f6 0 0 NA
  7. w2 f1 2 40 20
  8. w2 f2 1 20 50
  9. w2 f3 1 20 70
  10. w2 f5 0 0 NA
  11. w2 f6 1 20 90
复制代码

Rplot.png (6.37 KB)

Rplot.png

板凳
火云邪神二号 发表于 2016-9-2 22:47:35
le.chat 发表于 2016-9-2 16:02
如果你是单纯针对这个问题解决方式,直接把family_percent为0对应的y_label改成NA就好了。
非常感谢,你的回答完美地解决了我的问题!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-2 23:21