Plot negative values in logarithmic scale with ggplot 2

后端 未结 2 2041
小鲜肉
小鲜肉 2021-01-06 10:25

I need to plot with ggplot2 package in R a graph with some negative values using an x logarithmic scale.

For example I want to plot these points using an x logarithmi

2条回答
  •  囚心锁ツ
    2021-01-06 10:49

    For this, I find the pseudolog10_trans transformation from the ggallin package to be very helpful, as it can accomodate situations with both positive and negative numbers on a log scale. E.g.

    library(ggplot2)
    library(ggallin)
    
    x <- c(-1,-10,-100, 1, 10, 100)
    y <- c(1,2,3, 1,2,3)
    
    df = data.frame(x = x, y = y)
    
    My_Plot = ggplot(
        df, 
        aes(x=x, y=y)) + 
        geom_point() + 
        scale_x_continuous(trans = pseudolog10_trans)
    
    My_Plot
    

提交回复
热议问题