亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用ggplot2在R中的每個條形圖上在geom_bar上放置標簽

如何使用ggplot2在R中的每個條形圖上在geom_bar上放置標簽

楊魅力 2019-10-26 12:21:11
我發現了,如何使用ggplot2在R中的geom_bar上放置標簽,但是它只是將label(numbers)放在僅一個標簽上。假設這是每個x軸的兩個條形,該怎么做?我的數據和代碼如下所示:dat <- read.table(text = "sample Types Numbersample1 A   3641sample2 A   3119sample1 B   15815sample2 B   12334sample1 C   2706sample2 C   3147", header=TRUE)library(ggplot2)bar <- ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) +   geom_bar(position = 'dodge') + geom_text(aes(label=Number))似乎數字文本也位于“躲避”模式中。我搜索了geom_text手冊以查找一些信息,但是無法使其正常工作。有什么建議嗎?
查看完整描述

2 回答

?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

嘗試這個:


ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) + 

     geom_bar(position = 'dodge', stat='identity') +

     geom_text(aes(label=Number), position=position_dodge(width=0.9), vjust=-0.25)


查看完整回答
反對 回復 2019-10-26
?
元芳怎么了

TA貢獻1798條經驗 獲得超7個贊

要添加到rcs的答案中,如果您希望在x是POSIX.ct日期時將position_dodge()與geom_bar()結合使用,則必須將寬度乘以86400,例如,


ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) + 

 geom_bar(position = "dodge", stat = 'identity') +

 geom_text(aes(label=Number), position=position_dodge(width=0.9*86400), vjust=-0.25)


查看完整回答
反對 回復 2019-10-26
  • 2 回答
  • 0 關注
  • 4230 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號