本文介绍了如何在R中用ggplot2绘制的图的y轴比例中准确显示数字的SI前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我用下面的代码生成了以下图表: plt 主题(legend.text = element_text(size(1),y =value))+ geom_point = axis_text = size = 20),legend.title = element_text(size = 14))+ theme(axis.text = element_text(size = 20))+ theme(axis.title = element_text(size =标题= c(1,2,3,4,5,6,7,8)+ scale_color_discrete(name =title (x =x,y =y)+ guides(color = guide_legend(override.aes = list(size = 4),ncol = 2,title.hjust = 0.5) )+主题(plot.margin =单位(c(0,0,0,0),mm)) 然而,我需要在y轴上的数字的SI前缀表示法,以得到我做了以下步骤, library( sos) 要在sitools p中使用findFn ackage findFn({SI前缀}) 然后,我使用标签中的f2si将浮点数字号码转换为带有SI前缀的数字 plt2 导致阴谋看起来像这样, 虽然f2si将y轴准确地更改为-1e ^ -0.8至-10 n,但它并不准确地显示0和1e ^ -0.8的值将分别为0和10 n。有人可能会建议什么应该纠正在这里,以便数字显示,因为他们应该贯穿始终。 谢谢。解决方案我无法重现您的行为。请参阅: df p p + scale_y_continuous(labels = f2si) 我也有找到另一个类似的功能,如果你不喜欢0 n标签: format_si< - function(...){#根据Ben Tupper的代码#https://stat.ethz.ch/pipermail/r-help/2012-January/299804.html 函数(x){限制< -c(1e-24,1e-21,1e-18,1e-15,1e-12, 1e-9 ,1e-6,1e-3,1e0,1e3, 1e6,1e9,1e12,1e15,1e18, 1e21,1e24)前缀< -c(y, z,a,f,p,n,μ ,m,,k,M,G,T,P,E,Z,Y) #根据间隔位置的数组索引的向量i #将前缀设置为非常小的值< 1e-24 i paste(format(round(x / limits [i],1) , trim = TRUE,scientific = FALSE,...), prefix [i])} } p + scale_y_continuous(labels = format_si()) I have the following plot, generated using this codeplt <- ggplot(d2, aes_string(x=names(same_df)[1],y= "value")) + geom_point(aes(color = variable), size = 1)+ theme_bw()+ theme(legend.text=element_text(size=14), legend.title=element_text(size=14))+ theme(axis.text=element_text(size=20)) + theme(axis.title=element_text(size=20,face="bold")) + scale_color_discrete(name = "title", labels = c("1", "2", "3", "4","5","6","7","8","9")) + labs(x = "x", y = "y")+ guides(colour = guide_legend(override.aes = list(size=4),ncol=2,title.hjust=0.5))+theme(plot.margin=unit(c(0,0,0,0),"mm"))However I need SI prefix notation for the numbers in the y-axis, to get that I did the following steps,library("sos")To use the findFn in sitools packagefindFn("{SI prefix}")Then I use the f2si in the labels to convert a floating point number number to number with a SI prefix plt2 <- plt + scale_y_continuous(labels=f2si)The resulting plot looks like this,While the f2si accurately changed the y axis for -1e^-0.8 to -10 n, it does not accurately display the value for 0 and 1e^-0.8 which would be 0 and 10 n respectively. Could someone please suggest what should be corrected here so that the numbers are displayed as they should be throughout.Thanks. 解决方案 I was not able to reproduce your behaviour. See this:df <- data.frame(x=runif(100), y=(runif(100)-1/2)/1e8)p <- ggplot(df, aes(x, y)) + geom_point()p + scale_y_continuous(labels=f2si)I have also found another similar function, if you don't like "0 n" label: format_si <- function(...) { # Based on code by Ben Tupper # https://stat.ethz.ch/pipermail/r-help/2012-January/299804.html function(x) { limits <- c(1e-24, 1e-21, 1e-18, 1e-15, 1e-12, 1e-9, 1e-6, 1e-3, 1e0, 1e3, 1e6, 1e9, 1e12, 1e15, 1e18, 1e21, 1e24) prefix <- c("y", "z", "a", "f", "p", "n", "µ", "m", " ", "k", "M", "G", "T", "P", "E", "Z", "Y") # Vector with array indices according to position in intervals i <- findInterval(abs(x), limits) # Set prefix to " " for very small values < 1e-24 i <- ifelse(i==0, which(limits == 1e0), i) paste(format(round(x/limits[i], 1), trim=TRUE, scientific=FALSE, ...), prefix[i]) }}p + scale_y_continuous(labels=format_si()) 这篇关于如何在R中用ggplot2绘制的图的y轴比例中准确显示数字的SI前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 20:39