I found a unexpected line in my scatterplot, how can I extract all the data near the line for further analysis?

Viewed 66

My data is about filesize and the time cost dealing with the file.

When I drew the point plot I got the below result:

ggplot(data,aes(filesize,time))+geom_point()

enter image description here You can see there are 2 lines in the plot.

How can I extract all the data near the line for further analysis?

Any advice for what to learn? Thank you in advance.

2 Answers

A good next step would be to identify those ratios that seem more common, to make it easier to isolate those observations.

library(dplyr)

data %>%
  mutate(time_per_size = time/file_size) %>%
  ggplot(aes(time_per_size)) +
    geom_histogram(bins = 50) # 30 bins is default, fiddle to see what value captures the predominant ratios most cleanly

Using @PavoDive's sample data, for instance, we can look at the ratios using this process, and use plotly to look at the spikes interactively, seeing that they are around 1.5 and 3.

library(ggplot2); library(dplyr)
dt %>%
  mutate(time_per_size = y/x) %>%
  filter(time_per_size < 10) %>%
  ggplot(aes(time_per_size)) +
  geom_histogram(bins = 300) 
plotly::ggplotly(.Last.value)

enter image description here

I agree with @heds1 that there's probably some underlying relationship between your outcome and [at least] a third variable, wether it's know to you or not.

See the following example with dummy data:

library(data.table)
library(ggplot2)

# try to mimic your data in the x axis. Include some random types
set.seed(1)
dt <- data.table(x = rbeta(3000, shape1 = 1.8, shape2 = 10), type = sample(LETTERS[1:5], 3000, TRUE))

# introduce a couple lines:
dt[type == "A", y := 3*x]
dt[type == "C", y := 1.5*x]

# and add some "white noise":
dt[!type %chin% c("A", "C"), y := abs(rnorm(.N, .5, .25))]

# see what you have:
plot(dt$x, dt$y)

# now see the light:
ggplot(dt, aes(x, y, colour = type))+geom_point()

enter image description here

enter image description here

Related