创建具有非线性x轴R的图形

问题描述 投票:1回答:1

我正在尝试重新创建下图,但无法摆脱x的“半步长”增量。3.有更好的方法吗?我现在正在使用ggplot2-正确设置,然后计划切换到plotly进行交互式绘图。

代码

仅显示PoC的3条痕迹:

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))

输出

enter image description here

所需图

enter image description here

r ggplot2 plotly axis r-plotly
1个回答
0
投票

无需以ggplot2开头。您可以直接在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 = list(0.2, 0.4, 0.6, 0.8, 1, 2, 4, 6, 8, 10)))

“”

© www.soinside.com 2019 - 2024. All rights reserved.