停止解析ggplot2注释中的小数点后的零 [英] Stop parsing out zeros after decimals in ggplot2's annotate

查看:141
本文介绍了停止解析ggplot2注释中的小数点后的零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在ggplot2绘图上的位置注释一行,该行同时包含一个(真实的)希腊字母和一个四舍五入到小数点后两位的数字.出现我的问题是因为我想显示小数位,即使它们都为零.不幸的是,annotate中的parse = T设置将字符串"1.00"转换为"1".这是一个具体示例:

I need to annotate a location on a ggplot2 plot with a line that contains both a (real) greek letter and a number rounded to 2 decimal places. My problem arises because I want to display the decimal places even if they are both zero. Unfortunately, the parse = T setting in annotate converts the string "1.00" into "1". Here's a specific example:

alpha_num <- "1.00"
p <- ggplot(data.frame(x=1,y=1,label=paste0("alpha == ", alpha_num)))
p <- p + geom_text(aes(x,y,label=label), parse = TRUE, size = 30)

上面的代码生成以下图:

The code above produces the following plot:

如何完整显示alpha_num?

推荐答案

您可以使用deparse来实现.

所以您的代码看起来像这样

So your code looks like this

alpha_num <- "1.00"
p <- ggplot(data.frame(x=1,y=1,label=paste0("alpha == ", alpha_num)))
# Use deparse in label
p <- p + geom_text(aes(x,y,label=paste0("alpha == ", deparse(alpha_num))), parse = TRUE, size = 30)
p  

输出为:

这篇关于停止解析ggplot2注释中的小数点后的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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