1

I have a dataset with y-axis = diversity indices and x-axis = depth. I am looking at how diversity changes with depth (increases/decreases). It is informative to visualize these changes over depth (so transforming isn't helpful), however it is difficult with the disparity between number of samples for different depths (more samples at shallower versus deeper depths. With the following code:

breaks_depth=c(0,50,100,150,250,350,450,500,1200) ggplot(data=df, aes(x=Depth, y=Diversity)) + geom_line()+ scale_y_continuous(breaks=seq(0,1400,200), limits=c(0,1400))+ scale_x_continuous(breaks=breaks_depth, limits=c(0,1200)) 

I get the following plot:

enter image description here

I would like to get a plot such that the distance between 500m and 1200m depth is smaller and the distance between the shallower depths (0-150m) is greater. Is this possible? I have tried expand and different break and limit variations. The dput() of this dataset can be found here. The rownames are the sample IDs and the columns I am using for the plot are: y-axis=invsimpson_rd, and x-axis=Depth_rd. TIA.

****EDIT***** Winner code and plot modified from Calum's answer below.

ggplot(data=a_div, aes(x=Depth_rd, y=invsimpson_rd)) + geom_line()+ scale_y_continuous(breaks=seq(0,1400,200), limits=c(0,1400))+ scale_x_continuous(trans="log10",breaks = c(0, 15,25,50,100,150,200,250,300,350,400,450, seq(600, 1200, by = 200))) 

enter image description here

4
  • 1
    You can apply a transformation to your data, for example scale_x_log10 is a common one though may be too extreme in this case. Others can be supplied with the trans argument to scale_x_continuous. Can you dput() your data so we can play around? Commented Apr 27, 2018 at 19:45
  • I want to avoid transforming if possible because I will lose depth information. I'll dput() the dataset and create a link to it in the main question text. Commented Apr 27, 2018 at 19:53
  • 2
    Permissions aren't correct on that link. The point of the transformation is that it transforms the values plotted but keeps the labels the same, and your original data is not changed, so I am not sure what information you are worried about losing. Commented Apr 27, 2018 at 20:00
  • I changed permissions on link. Also, I am super embarrassed realizing now that the 'trans' argument keeps depth labels the same. I assumed it did what was done when I had transformed depth myself and put the log10 values on the x-axis instead. Commented Apr 27, 2018 at 20:10

1 Answer 1

2

Here's an example with the built in economics dataset. You can see that you can specify the breaks however you want as per usual, but the "sqrt" transformation shifts the actual plotted values to have more space near the beginning of the series. You can use other built in transformations or define your own as well.

EDIT: updated with example data and some comparison of common different trans options.

library(tidyverse) tbl <- structure(list(yval = c(742, 494, 919, 625, 124, 788, 583, 213, 715, 363, 15, 313, 472, 559, 314, 494, 388, 735, 242, 153, 884, 504, 267, 454, 325, 305, 746, 628, 549, 345, 327, 230, 271, 486, 971, 979, 857, 779, 394, 903, 585, 238, 702, 850, 611, 710, 694, 674, 1133, 468, 784, 634, 234, 61, 325, 505, 693, 1019, 766, 435, 407, 772, 925, 877, 187, 290, 782, 674, 1263, 1156, 935, 499, 791, 797, 537, 308, 761, 744, 674, 764, 560, 805, 540, 427, 711), xval = c(80, 350, 750, 100, 20, 200, 350, 50, 110, 20, 200, 350, 60, 100, 20, 40, 60, 100, 20, 40, 350, 50, 20, 40, 50, 30, 40, 260, 1000, 200, 200, 200, 500, 50, 350, 360, 380, 250, 60, 190, 40, 70, 70, 40, 40, 70, 180, 180, 440, 370, 130, 1200, 20, 20, 30, 80, 120, 200, 220, 120, 40, 80, 350, 750, 20, 80, 200, 320, 500, 220, 160, 80, 140, 350, 100, 40, 350, 100, 200, 340, 60, 40, 100, 60, 40)), .Names = c("yval", "xval"), row.names = c(NA, -85L), class = c("tbl_df", "tbl", "data.frame")) ggplot(tbl) + geom_line(aes(x = xval, y = yval)) + scale_x_continuous(trans = "sqrt", breaks = c(0,50,100,150,250,350,450,500,1200)) 

ggplot(tbl) + geom_line(aes(x = xval, y = yval)) + scale_x_continuous(trans = "log10", breaks = c(0,50,100,150,250,350,450,500,1200)) 

Created on 2018-04-27 by the reprex package (v0.2.0).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.