全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
30841 6
2017-01-02
如题:R语言绘图(plot/ggplot2) 如何设置坐标轴标签小数位数?



谢谢帮助!



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2017-1-3 20:07:36
Google了一下,试了下可以。
http://stackoverflow.com/questions/38369855/how-to-put-exact-number-of-decimal-places-on-label-ggplot-bar-chart

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.

r ggplot2 bar-chart
shareimprove this question
edited Jul 14 '16 at 12:04




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))
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-1-3 20:20:42
https://www.google.com/#safe=strict&q=ggplot+digits
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-1-3 23:20:45
+scale_x_continous(limits=c(min(x),max(x)),breaks=round(seq(min(x),max(x),length.out=10),4))
标10个刻度,保留四位小数,想改的话,把10和4按自己要求改成别的数还可以scale_y_continous,scale_color_continous,你懂的
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-1-4 11:49:49
johnmy 发表于 2017-1-3 20:20
https://www.google.com/#safe=strict&q=ggplot+digits
谢谢你的帮助!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-1-4 11:50:05
机械唯物主义123 发表于 2017-1-3 23:20
+scale_x_continous(limits=c(min(x),max(x)),breaks=round(seq(min(x),max(x),length.out=10),4))
标10个 ...
非常感谢!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群