Logarithmic axis with modified ticks

Viewed 704

I'm trying to re-create the below graph but am not able to get rid of the 'half-step' increments for x > 3. Is there a better way of doing this? I'm using ggplot2 for now - to get it right and then plan on switching to plotly for interactive plots.

Code

Only showing 3 traces for PoC:

set.seed(69)
library(data.table)
library(ggplot2)

k <- lapply(1:10, function(z){
  ret <- sample(x = (5*z):100, 
                size = 20, 
                replace = T)
  dat <- data.table(V1 =  ret[order(ret)] )
  colnames(dat) <- paste('trace', z, sep = '')
  return(dat)
  })

dat <- do.call(cbind, k)
dat$size <- c(seq(0.1, 1, 0.1), 2:11)

ggplot(dat[size >= 0.2, ], aes(x = size)) + 
  geom_line(size = 1.5, aes(y = trace1), color = '#003366') + 
  geom_line(size = 1.5, aes(y = trace2), 
            color = rgb(red = 0, green = 103, blue = 62, maxColorValue = 255)) + 
  geom_line(size = 1.5, aes(y = trace3), color = '#b20000') + 
  scale_x_log10(breaks = c(seq(0.1, 1, 0.1), 2:11))

Output

Desired Plot

2 Answers

There is no need to start with ggplot2. You can do this directly in plotly:

library(dplyr)
library(tidyr)
library(plotly)

dat %>% 
  filter(size >= 0.2) %>% 
  pivot_longer(-size) %>% 
 plot_ly(x = ~size, y = ~value, color = ~name, type = 'scatter', mode = 'lines') %>% 
  layout(xaxis = list(type = "log",
                      tickvals = as.list(c(seq(0.2,1,0.2), seq(2,10,2)))))

Minor grid lines can be removed via the theme, by setting panel.grid.minor = element_blank().

set.seed(69)
library(data.table)
library(ggplot2)

k <- lapply(1:10, function(z){
  ret <- sample(x = (5*z):100, 
                size = 20, 
                replace = T)
  dat <- data.table(V1 =  ret[order(ret)] )
  colnames(dat) <- paste('trace', z, sep = '')
  return(dat)
})

dat <- do.call(cbind, k)
dat$size <- c(seq(0.1, 1, 0.1), 2:11)

ggplot(dat[size >= 0.2, ], aes(x = size)) + 
  geom_line(size = 1.5, aes(y = trace1), color = '#003366') + 
  geom_line(size = 1.5, aes(y = trace2), 
            color = rgb(red = 0, green = 103, blue = 62, maxColorValue = 255)) + 
  geom_line(size = 1.5, aes(y = trace3), color = '#b20000') + 
  scale_x_log10(breaks = c(seq(0.1, 1, 0.1), 2:11)) +
  theme(panel.grid.minor = element_blank())

Created on 2020-05-28 by the reprex package (v0.3.0)

Related