Search code examples
rggplot2ribbonlogarithmaxis-labels

R ggplot2: filling the space between 2 lines with varying x values (geom_ribbon) and formatting log axis labels


I am trying to fill the space between two curves, however geom_ribbon uses varying y values rather than x values. My y axis is depth so I don't really want to swap the axes over. Is there any way to do this? Here is a sample of my dataset:

Statsrep <- structure(list(Depth = c(34L, 74L, 114L, 164L, 204L, 264L, 304L, 344L, 384L, 424L, 
464L, 504L, 554L), Min = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), Max = c(38, 105, 86, 44, 68, 
155, 160, 68, 120, 1670, 178, 110, 84), Average = c(5.64, 12.08, 17.42, 6.5, 11.59, 11.09, 10.1, 
9.09, 8.08, 38.96, 15.94, 6.53, 13.21), Mode = c(2,2,3,2,6,2,2,2,2,2,2,2,2)), .Names = 
c("Depth", "Min", "Max", "Average", "Mode"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L), class = "data.frame")
Statsrep

The code i am currently using is:

Statsrep <- read.csv(file="filename.csv", header=TRUE, sep=" ")
p1 <- ggplot(data = Statsrep, aes(xmin=Min, xmax=Max, y=Depth)) +
geom_ribbon(aes(fill="#CCCCCC"))
p1 <- p1 + coord_trans(x = "log10") + 
scale_x_continuous(breaks=c(1, 10, 100, 1000, 1500)) +
ylim(600,0)
p1 <- p1 + layer(geom = "path")
p1 + xlab("GRAINSIZE / mm")
p1 + ylab("DEPTH / m")

Also, I want to customise my log x axis so that there are 1, 10, 100, 1000 labels showing and equally spaced, similar to log graph paper. This code only shows 100, 1000, 1500 that are bunched up toward the right of the axis. Is there any way to format the axis like this? Thanks for any help in advance. Holly


Solution

  • I guess she wants the depth to go downwards

      ggplot(data = Statsrep, aes(ymin=Min, ymax=Max, x=Depth, y = Average)) +
      geom_ribbon(alpha = 0.5) + 
      geom_line(aes(group = 1)) +
      scale_y_log10("GRAINSIZE / mm", breaks=c(1, 10, 100, 1000)) +
      scale_x_reverse("DEPTH / m", limits = c(600,0)) + 
      coord_flip()
    

    The graph looks diferent because I'm using a custom theme for the whole session.

    enter image description here