How to draw a curve() with ggplot2

June 3, 2012
By

(This article was first published on Rronan » R, and kindly contributed to R-bloggers)

ggplot2 improves the graphics drawn with R. A (very) short adaptation time is needed to find how to make graphs equivalent to the ones of graphics.

For example, to draw the curve of a function, there is no function similar to curve(). You have to use qplot() by setting the stat and geom arguments as shown below.

First, the ggplot2 package must be installed.

install.packages("ggplot2")

Here, I plot the probability density function of the gamma distribution with parameters /(/alpha = 1/) and /(/beta = 1/) between 1 and 10:

require("ggplot2")
qplot(1:10, stat = "function", geom = "line",
      fun = function(x) dgamma(x, shape = 1))

qplot

With curve():

curve(dgamma(x, shape = 1), from = 1, to = 10)

curve

The ggplot2 community is fairly active and the web searches with the keyword “ggplot2″ almost always lead to relevant results.

To leave a comment for the author, please follow the link and comment on his blog: Rronan » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , ,

Comments are closed.