There are two problems to solve - calculate log from negative values and then combine logarithmic scale and reverse scale.
To combine log and reverse scales you can use solution provided by @Briand Diggs on this SO question.
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))
}
To make it work with negative values, provide x
values as -x
in ggplot()
call and then use another transformation for labels=
inside scale_x_continuous()
to get back negative values.
df<-data.frame(x=c(-1,-10,-100),y= c(1,2,3))
ggplot(df,aes(-x,y))+geom_point()+
scale_x_continuous(trans=reverselog_trans(base=10),
labels=trans_format("identity", function(x) -x))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…