Open main menu

Wikibooks β

R Programming/Grammar of graphics

< R Programming(Redirected from R Programming/ggplot)

Plotting a functionEdit

We use qplot() with the option stat=function :

# Plot the quadratic function
square <- function(x){
qplot(c(0, 2), stat = "function", fun = square, geom = "line")

Here is another example with the sinus function  :

# plot the sinus functon
qplot(c(-10, 10), stat = "function", fun = sin, geom = "line")


  • Leland Wilkinson, The Grammar of Graphics (Statistics and Computing), Springer, 2005
  • Hadley Wickham, ggplot2: Elegant Graphics for Data Analysis, Use R!, Springer, 2009