How to draw a curve() with ggplot2

[This article was first published on Rronan » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 their blog: Rronan » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)