Plotting ~75K data points with R.

I have moderate size data with ~75K data points. I am trying to plot a facet_wrapped line graph with ggplot using multiple variables in the data. The code is like this:

library(ggplot2)
library(ggthemes)
df <- read.delim("D-CL-allblock.txt")
My_Theme = theme(
  title = element_text(size = 20, face = "bold"),
  axis.title.x = element_text(size = 16, face = "bold"),
  axis.text.x = element_text(size = 14),
  axis.title.y = element_text(size = 16, face = "bold"),
  axis.text.y = element_text(size = 14),
  legend.text = element_text(size = 14),
  aspect.ratio = 0.8,
  legend.position = "right",
  panel.background = element_rect(fill = "white", colour = "grey50"),
  axis.line = element_line(size = 3, colour = "grey80"),
  axis.text = element_text(size = 10),
  axis.ticks.length.y = unit(.25, "cm"),
  axis.ticks.length.x = unit(.25, "cm"))
plot <- ggplot(data = df, aes(Pos, value, group = Stock)) +
  geom_line(aes(color = Stock), size = 1) +
  facet_grid(variable ~ Chr, scales = 'free') +   ###############use scales = "free" for individual axes length
  scale_color_brewer(palette="Reds") + #put palette="Paired" for blues
  # scale_color_brewer(palette="Paired") +
  My_Theme +
  theme_clean()

plot

Head of the data looks like

  Chr Pos variable        value   Stock
1  2L   1       d1 -0.488857515 Control
2  2L   2       d1  0.295529037 Control
3  2L   3       d1  0.003036192 Control
4  2L   4       d1 -1.113652386 Control
5  2L   5       d1 -0.668819366 Control
6  2L   6       d1 -1.302787150 Control

Stock variables are - Control and Late, Chr variables are - 2L, 2R, 3L, 3R, and X, variable values are d1, d2, d3, d4.
ggplot is unable to plot this, takes a long time and then gives an unreadable plot which doesn't make sense.
Any help regarding what I am doing wrong?

Is this what you're looking for?

library(ggplot2)
library(ggthemes)

DF <- structure(list(Chr = c("2L", "2L", "2L", "2L", "2L", "2L"), Pos = c(
  1,
  2, 3, 4, 5, 6
), variable = c("d1", "d1", "d1", "d1", "d1", "d1"), value = c(
  -0.488857515, 0.295529037, 0.003036192, -1.113652386,
  -0.668819366, -1.30278715
), Stock = c(
  "Control", "Control", "Control",
  "Bunny", "Bunny", "Bunny"
)), class = c(
  "spec_tbl_df", "tbl_df",
  "tbl", "data.frame"
), row.names = c(NA, -6L), spec = structure(list(
  cols = list(Chr = structure(list(), class = c(
    "collector_character",
    "collector"
  )), Pos = structure(list(), class = c(
    "collector_double",
    "collector"
  )), variable = structure(list(), class = c(
    "collector_character",
    "collector"
  )), value = structure(list(), class = c(
    "collector_double",
    "collector"
  )), Stock = structure(list(), class = c(
    "collector_character",
    "collector"
  ))), default = structure(list(), class = c(
    "collector_guess",
    "collector"
  )), skip = 1L
), class = "col_spec"))


p <- ggplot(DF, aes(Pos, value, group = Stock)) +
  geom_line(aes(color = Stock), size = 1) +
  facet_grid(variable ~ Chr, scales = "free") + ############### use scales = "free" for individual axes length
  scale_color_brewer(palette = "Reds") + # put palette="Paired" for blues
  # scale_color_brewer(palette="Paired")  +
  theme_clean() +
  theme(
    title = element_text(size = 20, face = "bold"),
    axis.title.x = element_text(size = 16, face = "bold"),
    axis.text.x = element_text(size = 14),
    axis.title.y = element_text(size = 16, face = "bold"),
    axis.text.y = element_text(size = 14),
    legend.text = element_text(size = 14),
    aspect.ratio = 0.8,
    legend.position = "right",
    panel.background = element_rect(fill = "white", colour = "grey50"),
    axis.line = element_line(size = 3, colour = "grey80"),
    axis.text = element_text(size = 10),
    axis.ticks.length.y = unit(.25, "cm"),
    axis.ticks.length.x = unit(.25, "cm")
  )

p

Created on 2021-01-13 by the reprex package (v0.3.0.9001)

Thank you @technocrat. Yes, something like this. However, I figured out what was problematic, my I set my value column as.numeric and it worked like a charm.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.