繁体   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