我找到了关于更改ggplot中x和y轴标签的精彩教程,并在条形图和散点图上成功使用它:
http://www.moeding.net/archives/32-Metric-prefixes-for-ggplot2-scales.html#comments
我想将此功能扩展到此热图'我也开发了。这是代码:
library(plot3D)
format_si <- function(x,...) {
# Format a vector of numeric values according
# to the Semi-International System of Units.
#
# Based on code by Ben Tupper
# https://stat.ethz.ch/pipermail/r-help/2012-January/299804.html
# Args:
# ...: Args passed to format()
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", "B", "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)
return(paste(format(round(x/limits[i], 1),
trim=TRUE, scientific=FALSE, ...),
prefix[i], sep=""))
}
}
#Generate some data
xs<-rnorm(50000)*100000
ys<-rnorm(50000)*100000
zs<-sin(xs)*10000000
df<-as.data.frame(cbind(xs,ys,zs))
#Heat Map
df$x<-cut(df$xs, breaks=50, labels=FALSE)
df$y<-cut(df$ys, breaks=50, labels=FALSE)
df.max<-expand.grid(x=1:50, y=1:50)
df.max<-merge(df.max, aggregate(zs~x+y, df, max), all.x=TRUE)
z<-t(matrix(df.max$zs, nr=50, nc=50))
x.values <- min(df$xs)+(0:49)*diff(range(df$xs))/50
y.values <- min(df$ys)+(0:49)*diff(range(df$ys))/50
image2D(x=x.values, y=y.values, z, rasterImage = TRUE, contour = list(lwd = 2, col = jet.col(11)),
main="Random Data", xlab="random xs",ylab="random ys", clab="zs=sin(xs)", ylim=c(0,max(df$ys)*1.05),
xlim=c(0,max(df$xs)*1.05))
在ggplot中,我只是将scale_y_continuous(labels=format_si())
添加到我的绘图中,轴标签将是1M而不是1.0e6或1000000.我尝试在image2d内关闭标签,然后使用{{1}但那也不起作用。有没有人试过这个呢?