我發現了,如何使用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)

元芳怎么了
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)
- 2 回答
- 0 關注
- 4230 瀏覽
添加回答
舉報
0/150
提交
取消