我想提出用GGPLOT2反转,日志10 x缩放一个情节:
require(ggplot2)
df <- data.frame(x=1:10, y=runif(10))
p <- ggplot(data=df, aes(x=x, y=y)) + geom_point()
然而,似乎我既可以一个日志10级或反向规模:
p + scale_x_reverse() + scale_x_log10()
p + scale_x_reverse()
我想这是符合逻辑的,如果一个层只能有一个规模。 当然,我可以做的数据框日志变换自己,破解它df$xLog <- log10(df$x)
但这种解决方案是一个似乎违背ggplot的精神。 有没有办法让这种阴谋没有做外部ggplot呼叫数据转换?
这@joran在他的评论中给出了链接给正确的想法(打造你自己的转换),但已经过时了关于新的scales
该包ggplot2
现在使用。 纵观log_trans
和reverse_trans
在秤包装的指导和启发,一个reverselog_trans
功能可以进行:
library("scales")
reverselog_trans <- function(base = exp(1)) {
trans <- function(x) -log(x, base)
inv <- function(x) base^(-x)
trans_new(paste0("reverselog-", format(base)), trans, inv,
log_breaks(base = base),
domain = c(1e-100, Inf))
}
这可以简单地使用:
p + scale_x_continuous(trans=reverselog_trans(10))
这给情节:
使用稍有不同的数据集显示,轴绝对是相反的:
DF <- data.frame(x=1:10, y=1:10)
ggplot(DF, aes(x=x,y=y)) +
geom_point() +
scale_x_continuous(trans=reverselog_trans(10))