如何在标签ggplot条形图上输入精确的小数位数 [英] how to put exact number of decimal places on label ggplot bar chart

查看:1054
本文介绍了如何在标签ggplot条形图上输入精确的小数位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 ggplot 条形图标签上指定精确的小数位数?



数据:

  strefa <-c(1:13)
a <-c(3.453782,3.295082,3.137755,3.333333 ,3.500000,3.351351,3.458824,3.318681,3.694175,3.241379,3.138298,3.309524,3.380000)
srednie< - data.frame(strefa,a)

$ b

代码是:

$ $ $ $ $ $ $ $ ggplot(srednie,aes(x =因子(strefa),y = a,label = round(a,digits = 2)))+
geom_bar(position = position_dodge(),stat =identity,color =darkgrey,width = 0.5) +
主题(legend.position =none,axis.text.x = element_blank(),axis.ticks.x = element_blank(),axis.ticks.y = element_blank())+
geom_text(size = 4,hjust = 1.2)+
coord_flip(ylim = c(1,6))+
xlab()+
ylab()

正如您所看到的,在标题为5和2的小节中,标签仅限于小数点后第一位。即使有3.000000或5.999999,如何显示2位小数?在这种情况下,我想显示3.00和6.00。



我试图用作 aes 参数 label = round(a,digits = 2)但它不起作用。

解决方案

您可以尝试以下操作,将其舍入为两位数字,并在小数点后打印两位数字。

  ggplot(srednie ,aes(x = factor(strefa),y = a,label = sprintf(%0.2f,round(a,digits = 2))))+ 
geom_bar(position = position_dodge(),stat = identity,color =darkgrey,width = 0.5)+
theme(legend.position =none,axis.text.x = element_blank(),axis.ticks.x = element_blank(),axis .ticks.y = element_blank())+
geom_text(size = 4,hjust = 1.2)+
coord_flip(ylim = c(1,6))+
xlab() +
ylab()

唯一修改是从

  round(a,digits = 2)

  sprintf(%0.2f,r ound(a,digits = 2))


How can I specify the exact number of decimal places on ggplot bar chart labels?

The data:

strefa  <- c(1:13)
a       <- c(3.453782,3.295082,3.137755,3.333333,3.500000,3.351351,3.458824,3.318681,3.694175,3.241379,3.138298,3.309524,3.380000)
srednie <- data.frame(strefa,a)

The code is:

ggplot(srednie, aes(x=factor(strefa), y=a, label=round(a, digits = 2))) +
  geom_bar(position=position_dodge(), stat="identity",  colour="darkgrey", width = 0.5) +
  theme(legend.position="none",axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.ticks.y = element_blank()) +
  geom_text(size = 4, hjust = 1.2) +
  coord_flip(ylim = c(1,6))+
  xlab("") +
  ylab("")

As you can see, on bars entitled 5 and 2 the labels are limited to the 1st decimal place. How to show 2 decimal places even if there is i.e. 3.000000 or 5.999999? In such a cases I would like to show 3.00 and 6.00.

I tried to use as a aes parameter label=round(a, digits = 2) but it doesn't work.

解决方案

You could try the following as it rounds to two digits and prints two digits after the decimal.

ggplot(srednie, aes(x=factor(strefa), y=a, label=sprintf("%0.2f", round(a, digits = 2)))) +
  geom_bar(position=position_dodge(), stat="identity",  colour="darkgrey", width = 0.5) +
  theme(legend.position="none",axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.ticks.y = element_blank()) +
  geom_text(size = 4, hjust = 1.2) +
  coord_flip(ylim = c(1,6))+
  xlab("") +
  ylab("")

The only modification was changing your code from

round(a, digits = 2)

to

sprintf("%0.2f", round(a, digits = 2))

这篇关于如何在标签ggplot条形图上输入精确的小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆