1

I have a question about how to transform the y-axis in ggplot2. My plot now has two lines and a scatter plot. For the scatter plot, I am very interested in the area around zero. Is there a possible way to enlarge the space between 0% and 5% and narrow the space between 20% and 30%?

I have tried to use coord_trans(y = "log10") to transform into a log form. But in this case, I have a lot of negative values, so if I want to use sqrt or log, the negative values will be removed. Do you have any suggestions?

Example of data points:

df1 = data.frame(y = runif(200,min = -1, max = 1))
df1 = data.frame( x= seq(1:200), y = df1[order(abs(df1$y)),])

ggplot(df1) + 
    geom_point(colour = "black",aes(x,y) ,size = 0.1) 

I want to have more space between 0% and 5 % and less space between 5% and 30%.

I have tried to use trans_new() to transform the axes.

eps <- 1e-8

tn <- trans_new("logpeps",
                function(x) (x+eps)^(3),
                function(y) ((y)^(1/3) ),
                domain=c(- Inf, Inf)
              )

ggplot(df1)+ geom_point(colour = "black",aes(x,y) ,size = 0.1) + 
  # xlab("Observations sorted by PD in v3.1") + ylab("Absolute PD difference ") + 
  # ggtitle("Absolute PD for RiskCalc v4.0 relative to v3.1") +

  scale_x_continuous(breaks = seq(0, round(rownum/1000)*1000, by = round(rownum/100)*10)) + 
  scale_y_continuous(limits = c(-yrange,yrange),breaks = c(-breaksY,breaksY),
                     sec.axis = sec_axis(~.,breaks = c(-breaksY[2:length(breaksY)],breaksY), labels = scales:: percent
                                        )) +
#   geom_line(data = df, aes(x,y[,3], colour = "blue"),size = 1) +  
#   geom_line(data = ds,aes(xval, yval,colour = "red"),size = 1) + 
  coord_trans(y = tn) +
  scale_color_discrete(name = element_blank())

But it compresses the plot to the center, which is opposite to what I want. Then I try to use y = y^3, but it shows an

ERROR: zero_range(range)

enter image description here

James Skemp
  • 8,018
  • 9
  • 64
  • 107
Mars Chen
  • 103
  • 1
  • 2
  • 9
  • 1
    You can start by filtering off everything < 0, and exclude any outliers on the positive side which should give you a more useful window. – Mako212 Jul 27 '17 at 20:34
  • @Mako212 has a good point. I'm not sure an axis with two scales at different ranges is commonly used or recommended for data visualization. Consider setting the limits of the y axis at 10% and -10% – Alex P Jul 27 '17 at 20:42
  • 1
    @AlexP I'd strongly suggest avoiding different axis scales on the same chart (assuming you're talking about using a different range for red and blue) because it suggests a false equivalency/comparison between the two sets of values on the same chart. Just limiting the chart to +/- 10% would be fine – Mako212 Jul 27 '17 at 20:45
  • 1
    You can use the log-modulus transformation `L(x) = sign(x) * log(|x| + 1)` in a custom scale/axis transformation https://stackoverflow.com/questions/38333603/ggplot-custom-scale-transformation-with-custom-ticks – Marco Sandri Jul 27 '17 at 20:49
  • 1
    Also! Is that possible to remove the space above 30% or below -30%? it is a kind of waste. – Mars Chen Jul 27 '17 at 23:31
  • @MarsChen Please provide a reproducible example with the actual code you're using to generate the chart. – Mako212 Jul 28 '17 at 15:46
  • @Mako212 Hi I just attach part of my data. Please have a look! Thank you – Mars Chen Jul 28 '17 at 16:28
  • @MarsChen Please use `dput(data)` and provide your ggplot code. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for example – Mako212 Jul 28 '17 at 16:33

2 Answers2

0

Try a cube root transform on the y values:

aes(y=yVariable^(1/3))

or use trans_new() to define a new transformation (such as cube root, with pleasing breaks and labels).

Alex P
  • 1,574
  • 13
  • 28
  • Using a trans_new() to transform y-axis with cube root compress the plot to the x-axis which is opposite to what I want. So I tried to transform y-axis to cube but it just shows error message: missing TRUE/FALSE value. – Mars Chen Jul 28 '17 at 16:19
  • hmmm... oh yea, I guess since you are working with percentages, the roots are gonna do funny things. . . try that log modulus in the comments above, and limiting the y axis to +/- 10% – Alex P Jul 28 '17 at 16:33
-1

A couple thoughts:

You can remove the empty edges of the plot like so:

scale_y_continuous(expand = c(0,0))

If you want to try the log transformation, just do:

scale_y_log10()

If you want to focus the window:

scale_y_continuous(limits=c(-.15,.15), expand=c(0,0))

Also consider adding theme_bw() for a cleaner look

Mako212
  • 6,787
  • 1
  • 18
  • 37