0

Using ggplot in R, I want to increase the distance between the values around the value 1 on the x axis, ie between 0.8 and 1.2.

set.seed(123)
library(ggplot2)

somedatasetx <- append(round(runif(100,0.2,10),2),round(runif(100,0.8,1.2),2))
somedatasety <- round(runif(200,0.2,10),2)
    ggplot(data=NULL, aes(x=somedatasetx, y=somedatasety)) +
    geom_point(color="red") +
    scale_x_log10(
    breaks=c(0.2,1,10),
    limits=c(0.2,11)
    )

enter image description here I have a concentration of values around those limits. Simply put, is there a way to expand the distance between 0.8 to 1.2 in order to visualize the values in this area? Another way to put it is that I want the value range from 0.8 to 1.2 to go as from 0.5 to 5 on the graph above? Obviously the log scale on the x axis is compromised as a consequence of this action.

Edit: Maybe there is a way to combine both continuous and log10 scale on an x-axis? Say, continuous between 0.2 to 1.2, and log10 from there and above? If I could control the proportions of each on the x axis, that could be the solution. Just don't know if it's possible.

pha
  • 316
  • 2
  • 9

2 Answers2

1

I think ggforce is what you have in mind. How about this?

library(ggplot2)
library(ggforce)

somedatasetx <- append(round(runif(100,0.2,10),2),round(runif(100,0.8,1.2),2))
somedatasety <- round(runif(200,0.2,10),2)
to_plot = data.frame( x = somedatasetx, y = somedatasety )

ggplot(data = to_plot, aes(x = x, y = y)) +
  geom_point(color = "red") +
  facet_zoom(xlim = c( 0.8, 1.2 ) )

enter image description here

This way, you have your original plot in the top panel and the highlighted area in the bottom one.

Francesco Grossetti
  • 1,555
  • 9
  • 17
  • Thx but given that I will be having many plots, this would make the results even more difficult to interpret. I'd rather have it all in the same plot. I just made an edit see above. – pha Mar 31 '20 at 20:48
  • I get the intent but am not sure this is doable. `ggplot()` treats the scales very precisely and what you want to do seems to be a hard modification of the internal x-scale. This is not just a simple relabeling of the axis ticks and text, but rather a physical modification. – Francesco Grossetti Mar 31 '20 at 20:52
  • This is a **ggforce** tutorial: https://rviews.rstudio.com/2019/09/19/intro-to-ggforce/ . Have a look and you may find a way to rescale to zoomed plot to be smaller. I do think this is the best way you can achieve what you want. – Francesco Grossetti Mar 31 '20 at 20:55
0

Not sure if this is what you want??

somedatasetx <- append(round(runif(100,0.2,10),2),round(runif(100,0.8,1.2),2))
somedatasety <- round(runif(200,0.2,10),2)
ggplot(data=NULL, aes(x=somedatasetx, y=somedatasety)) +
  geom_point(color="red") +
  scale_x_log10(
    breaks=c(0.2,1,10),
    limits=c(0.2,11)
  )+ coord_cartesian(xlim=c(0.8,1.2))

enter image description here

johnjohn
  • 716
  • 2
  • 9
  • 17
  • (And of course you can change the xlim if you want) – johnjohn Mar 31 '20 at 20:25
  • Sorry, it's not. I'd rather keep the limits on the x. – pha Mar 31 '20 at 20:26
  • @pha sorry about that. in that case, did you try other transformations? – johnjohn Mar 31 '20 at 20:30
  • No problem I appreciate all input. So do you have any in mind? I can't come to think of any that has the ability to expand the resolution around the value 1. – pha Mar 31 '20 at 20:34
  • So, you basically want to have a discontinuous x-axis? If so, this might be useful: https://stackoverflow.com/questions/7194688/using-ggplot2-can-i-insert-a-break-in-the-axis. Or rather, it explains why this is not possible/desirable :) – johnjohn Mar 31 '20 at 20:58