Search code examples
rggplot2plotgraphscatter-plot

How to make a Scatter plot in R with a big data frame?


I have this CSV file which is a data of a heart rate of an animal before and after of exposure to a stimuli. I want to make an scatter plot in R. but I have one problem. R make 20 scatter plot for each columns! I want to have only on scatter plot with all of this data. this is my data

This was my initial code:

data= read.csv('1.csv')
View(data)
data= read.csv('1.csv', header = FALSE)
plot(data)

and this was its output: it seems r make multiple scatter plot instead of one scatter!

Then I tried to change my data frame with melt function. and make the graph. this was my code:

library(reshape2)
data2 = melt(data)
plot(data2)

and this is its output: this is the second plot

but I want a scatter plot like this:

this is the ideal scatter plot that I am trying to make

What should I change in my code or my data frame to have scatter plot like that?

this is my data:

structure(list(V1 = structure(1:20, levels = c("8598", "8628", "8658", "8688", "8718", "8748", "8778", "8808", "8838", "8868", "8898", "8928", "8958", "8988", "9018", "9048", "9078", "9108", "9138", "9168"), class = "factor"), V2 = c(-9.66, -9.3, -9.41, -9.9, -9.24, -9.87, -9.93, -9.67, -9.73, -9.87, -9.75, -9.83, -9.49, -9.71, -9.46, -9.71, -9.47, -9.7, -9.17, -9.9), V3 = c(-8.9, -8.51, -8.63, -9.1, -8.41, -9.12, -9.16, -8.82, -8.96, -9.14, -9.01, -8.98, -8.66, -8.92, -8.66, -8.99, -8.64, -8.9, -8.35, -9.08), V4 = c(-8.23, -7.73, -7.89, -8.34, -7.6, -8.32, -8.27, -8, -8.08, -8.49, -8.22, -8.14, -7.87, -8.15, -7.84, -8.17, -7.79, -8.12, -7.53, -8.25), V5 = c(-7.5, -7.02, -7.22, -7.54, -6.81, -7.57, -7.5, -7.15, -7.26, -7.82, -7.39, -7.34, -7.03, -7.46, -7.02, -7.31, -6.97, -7.42, -6.69, -7.46), V6 = c(-6.84, -6.34, -6.49, -6.76, -6.01, -6.87, -6.66, -6.34, -6.47, -7.17, -6.6, -6.49, -6.23, -6.76, -6.19, -6.49, -6.14, -6.65, -5.85, -6.58 ), V7 = c(-6.07, -5.67, -5.83, -6.07, -5.23, -6.14, -5.84, -5.58, -5.65, -6.44, -5.76, -5.71, -5.43, -6.1, -5.37, -5.62, -5.31, -5.97, -5.05, -5.74), V8 = c(-5.24, -5, -5.1, -5.41, -4.45, -5.47, -5.03, -4.74, -4.82, -5.63, -4.95, -4.88, -4.62, -5.34, -4.58, -4.8, -4.49, -5.31, -4.25, -4.87), V9 = c(-4.38, -4.27, -4.31, -4.76, -3.67, -4.72, -4.35, -3.94, -4, -4.84, -4.14, -4.05, -3.84, -4.55, -3.8, -3.96, -3.72, -4.55, -3.45, -4.04), V10 = c(-3.57, -3.47, -3.46, -4.02, -2.94, -3.9, -3.67, -3.13, -3.22, -4.02, -3.34, -3.26, -3.05, -3.76, -3.01, -3.11, -3.05, -3.76, -2.69, -3.14), V11 = c(-2.78, -2.62, -2.66, -3.23, -2.26, -3.09, -3.02, -2.3, -2.36, -3.2, -2.47, -2.41, -2.31, -2.96, -2.21, -2.29, -2.32, -2.92, -1.96, -2.42), V12 = c(-2.06, -1.8, -1.86, -2.44, -1.56, -2.24, -2.31, -1.51, -1.56, -2.42, -1.72, -1.58, -1.56, -2.16, -1.46, -1.48, -1.63, -2.07, -1.27, -1.76), V13 = c(-1.32, -1.07, -1.07, -1.62, -0.9, -1.44, -1.52, -0.72, -0.74, -1.6, -0.98, -0.75, -0.89, -1.34, -0.74, -0.66, -0.85, -1.26, -0.53, -0.98), V14 = c(-0.62, -0.25, -0.28, -0.79, -0.08, -0.65, -0.74, -0.44, 0.06, -0.8, -0.21, 0.03, -0.23, -0.53, -0.04, 0.2, -0.02, -0.4, 0.2, -0.13), V15 = c(0.17, 0.49, 0.51, -0.01, 0.73, 0.18, 0.1, 0.11, 0.89, 0, 0.5, 0.94, 0.49, 0.27, 0.7, 1.08, 0.82, 0.4, 0.88, 0.74), V16 = c(1.03, 1.21, 1.24, 0.83, 1.54, 0.95, 0.87, 0.92, 1.68, 0.8, 1.16, 1.81, 1.32, 1.12, 1.38, 1.94, 1.67, 1.3, 1.62, 1.57), V17 = c(1.82, 1.87, 1.96, 1.64, 2.35, 1.72, 1.74, 1.78, 2.49, 1.6, 1.93, 2.62, 2.13, 1.95, 2.18, 2.79, 2.55, 2.14, 2.45, 2.41), V18 = c(2.61, 2.52, 2.64, 2.3, 3.2, 2.41, 2.52, 2.58, 3.28, 2.4, 2.76, 3.46, 3.01, 2.77, 3.02, 3.6, 3.38, 2.94, 3.26, 3.26), V19 = c(3.39, 3.32, 3.46, 3.01, 3.99, 3.08, 3.33, 3.39, 4.02, 3.07, 3.58, 4.29, 3.79, 3.55, 3.88, 4.41, 4.21, 3.74, 4.06, 4.09), V20 = c(4.11, 4.15, 4.29, 3.66, 4.79, 3.91, 4.09, 4.2, 4.69, 3.75, 4.43, 5.07, 4.67, 4.35, 4.74, 5.21, 5.03, 4.46, 4.88, 4.97), V21 = c(4.83, 4.98, 5.11, 4.39, 5.6, 4.76, 4.93, 4.96, 5.34, 4.43, 5.29, 5.9, 5.49, 5.08, 5.58, 6.02, 5.85, 5.12, 5.7, 5.79), V22 = c(5.51, 5.77, 5.92, 5.18, 6.39, 5.59, 5.73, 5.68, 6.03, 5.22, 6.12, 6.69, 6.34, 5.79, 6.41, 6.76, 6.65, 5.84, 6.54, 6.6), V23 = c(6.33, 6.55, 6.71, 5.98, 7.19, 6.41, 6.49, 6.41, 6.81, 6.06, 6.92, 7.46, 7.18, 6.52, 7.24, 7.51, 7.36, 6.51, 7.37, 7.45), V24 = c(7.17, 7.32, 7.5, 6.8, 7.97, 7.19, 7.33, 7.14, 7.59, 6.88, 7.77, 8.12, 8.01, 7.2, 8.06, 8.23, 8.14, 7.3, 8.19, 8.28), V25 = c(7.97, 8.08, 8.21, 7.62, 8.74, 7.97, 8.14, 7.82, 8.42, 7.71, 8.58, 8.83, 8.84, 7.97, 8.88, 8.96, 8.87, 8.17, 8.97, 9.1), V26 = c(8.63, 8.77, 8.89, 8.43, 9.47, 8.67, 8.94, 8.6, 9.21, 8.55, 9.44, 9.54, 9.66, 8.82, 9.71, 9.65, 9.54, 8.99, 9.83, 9.93), V27 = c(9.37, 9.44, 9.54, 9.26, NA, 9.35, 9.75, 9.43, 9.98, 9.37, NA, NA, NA, 9.66, NA, NA, NA, 9.81, NA, NA)), row.names = c(NA, -20L), class = "data.frame")


Solution

  • The reshape2 package has been deprecated and will most likely not work when R gets updated next. Using tidyr is an easier and future-proof solution. In the following solution, "df" is a version of your dput():

    library(tidyr)
    library(ggplot2)
    
    # Create a long version of your data, excluding the V1 column
    # V1 becomes the identifier column by default using this method
    df1 <- df %>% pivot_longer(!V1)
    

    and then plot using:

    ggplot(df1, aes(y=V1, x=value)) + 
      geom_point(size = 1) + 
      labs(y="n_trial", x="t(s) respect to the stim") +
      ggtitle("phase1-stim1") +
      geom_vline(xintercept = 0) +
      scale_x_continuous(breaks = seq(-10, 10, 2),
                         expand = c(0,0),
                         limits = c(-10, 10)) +
      scale_y_discrete(expand = c(0, 0)) +
      coord_cartesian(clip = "off") +
     theme( 
       plot.title = element_text(size = 14, hjust = .5, face="bold"),
       axis.title = element_text(size = 12),
       axis.ticks.length=unit(-2.5, "mm"),
       axis.ticks.length=unit(-2.5, "mm"),
       axis.line = element_line(linewidth = .5),
       panel.grid.minor=element_blank(),
       panel.background=element_blank())
    

    And the result:

    result

    Review the ggplot2 documentation and play around with the settings to get it the way you want it.