簡體   English   中英

使用ggplot構建條形圖時如何在標簽上顯示所有有效數字?

[英]How to display all significant figures on labels when constructing bar charts using ggplot?

我已經使用ggplot2構建了一個條形圖,並且我正在顯示每個條形圖上的值。 我的問題是我無法顯示每個條形上尾隨值的零。

這是我的數據集:

data <- data.frame(product=c('product1',
                              'product2',
                              'product3',
                              'product4',
                              'product5',
                              'product6',
                              'product7',
                              'product8'),
                   test=c(0.60,
                          0.80,
                          0.50,
                          0.70,
                          0.40,
                          0.30,
                          0.20,
                          0.10))

該圖是使用以下代碼生成的:

ggplot(data, aes(product,test))+
  geom_col()+
  geom_bar(stat="identity", col = "grey", fill="black")+
  coord_flip()+
  labs(title="Product comparisons")+
  theme(text=element_text(size=20))+
  theme(axis.text.y=element_text(size=12))+
  ylim(NA, 0.90)+
  geom_text(aes(label=round(test, digits=2)), nudge_y= -0.05, color="white", size = 6)

結果: 在此處輸入圖片說明

只顯示第一個小數,但我需要在每個標簽后顯示零。 我嘗試使用label=round(test, digits=2)命令來做到這一點,但這並沒有解決它。 可以做些什么來解決這個問題?

嘗試一下!

ggplot(data, aes(product,test))+
  geom_col()+
  geom_bar(stat="identity", col = "grey", fill="black")+
  coord_flip()+
  labs(title="Product comparisons")+
  theme(text=element_text(size=20))+
  theme(axis.text.y=element_text(size=12))+
  ylim(NA, 0.90)+
  geom_text(aes(label=format(round(test, digits = 2), nsmall = 2)), nudge_y= -0.06, color="white", size =3)

暫無
暫無

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

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