Plotting multiple smooth lines from a dataframe

14,060

Have a look at ggplot2

#create dummy data
n <- 200
dataset <- data.frame(xval = runif(n), col1 = rnorm(n), col2 = rnorm(n, sd = 2), col3 = rnorm(n, mean = seq(0, 2, length = n)), col4 = rnorm(n, sd = seq(0, 1, length = n)), col5 = rnorm(n, mean = 1))
#convert data to long format
library(reshape)
Molten <- melt(dataset, id.vars = "xval")
#plot it
library(ggplot2)
ggplot(Molten, aes(x = xval, y = value, colour = variable)) + 
    geom_smooth() + geom_point()
#some tweaking
ggplot(Molten, aes(x = xval, y = value, colour = variable)) + 
    geom_smooth(se = FALSE) + geom_point() + theme_bw() + 
    scale_x_continuous("the x label") + scale_x_continuous("the y label") +
    scale_colour_discrete("")

enter image description here

Share:
14,060
Homunculus Reticulli
Author by

Homunculus Reticulli

Updated on June 28, 2022

Comments

  • Homunculus Reticulli
    Homunculus Reticulli almost 2 years

    I am relatively new to R. I am trying to plot a dataframe loaded from a csv file. The data consists of 6 columns like this:

    xval,col1,col2,col3,col4,col5
    

    The first column (xval) consist of a sequence of monotonically increasing positive integers (e.g. 10, 40, 60 etc), the other columns columns 1 to 5, consist of floating point numbers.

    I want to create a plot in R as follows:

    • plot xval term on x axis
    • plot remaining columns (col1 ... col5) lines
    • create a legend legend with col2, ... col5 renamed

    The data to be plotted (col1, ... col5) are 'snapshot' values so although I want to plot them as lines, I want the lines to be smoothed (i.e. interpolated).

    I am looking for a snippet that help me create the plot once I have read the data into a dataframe. Any help will be appreciated.

  • Homunculus Reticulli
    Homunculus Reticulli almost 12 years
    +1 for succinct beautiful code!. Thanks!. I can't believe you were able to do this in so few lines of code! :). I need to modify the chart slightly to be like what I want it to be. I have managed to remove the 'scatter points', but I need to: 1. Remove the 'confidence band(?)' around the plotted lines 2. Rename the x and Y axis 3. Remove the legend title 3. use white background for the plot.
  • Homunculus Reticulli
    Homunculus Reticulli almost 12 years
    As an aside, (correct me if I am wrong), I am new to R and it seems that most answers to questions relating to R graphics these days involve a solution withh ggplot(2) - should I concentrate on learning how to use ggplot2 instead of R's own plot() command?
  • Thierry
    Thierry almost 12 years
    If have adding a line of code for a tweaked plot. Have a look at the ggplot2 website (had.co.nz/ggplot2) for code examples. It is good to know the basics of the plot() command, but it is much easier to generate high-quality plots with ggplot(). Generating the same plot with plot() command will require several lines of code.
  • Paul Hiemstra
    Paul Hiemstra almost 12 years
    I would definitely focus on ggplot2. In my opinion is superior to both standard plot, and lattice.
  • Johy
    Johy over 6 years
    scale_x_continuous("the y label") should be scale_y_continuous("the y label")