I am showing three examples that will help an average R user to create beautiful graphs. The interesting (may be useful) parts of these examples are the use of some very smart but tricky functions to, for example, add a Greek symbol on a plot, add a title to a plot with some mathematical symbols. In these examples I used `expression`

and paste

functions repeatedly. For more examples please see this site.

### EXAMPLE 1: Gamma density with scale parameter =1 and various shape parameter

x <- seq(0, 30, length=300)

hx <- dgamma(x, shape=1, scale=1)gshape <- c(2, 5, 10, 15)

colors <- c("red", "blue", "darkgreen", "gold")plot(x, hx, type="n", lty=2, lwd=2, xlab="x", ylab=expression(f(x)), main=expression(theta == 1), ylim=c(0,0.4), frame.plot=F)

for (i in 1:4){

lines(x, dgamma(x,shape=gshape[i], scale=1), lwd=2, col=colors[i])

}# Inserting mathematical expressions

text(3.5,.35 , expression(paste(alpha==2)))

text(6.5,.18 , expression(paste(alpha==5)))

text(11.5,.13 , expression(paste(alpha==10)))

text(23,.05 , expression(paste(alpha==15)))

### EXAMPLE 2: Barplot for Poisson distribution

# Generating Poisson random numbers with rates 1, 2, 3, and 4

# and drawing the histograms and arranging them on a 2 x 2 matrixpar(mfrow=c(2,2))

lambda <- 1:4

x <- 0:10for (i in lambda)

{

x1 <- dpois(x, lambda[i])

barplot(x1, names.arg=c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"), ylab=expression(P(x)), ylim=c(0,.4), col="lightgreen")

title(main = substitute(lambda == i,list(i=i)))

}

### EXAMPLE 3: Binomial probability plots (line diagram)

# Binomial probability plot with vertical lines

par(mfrow=c(2,2))

n <- 10

p <- c(0.1, 0.25, 0.5, 0.8)

x <- 0:10

for (i in 1:4)

{

x1 <- dbinom(x, n, p[i])

plot(x1, type="h", xlab="x", ylab=expression(P(x)),

ylim=c(0,.4),lwd=2, col="red")

j <- p[i]

title(main = substitute(p == j,list(j=j)))

}

### SUGGEST A GRAPH

If you have an idea of a complicated graph, please submit your code to me. I will add your contribution on this page.

If you find this page useful, please link this site from your blog/website or refer it in your report/thesis.

Thank you.

## Comments

## Re: How to do this graph using R

Wow! I had no idea that Greek letters can be incorporated in R graphs. Is that command expression a built in function in R? Or we'd need some package to use it?

By the way, how have you added this menu in the comment section with these bold/italic/enumeration etc. options? Can this be added in free domain blogs like mine?

## Re: How to do this graph using R

Dear Shomoita,

Thanks for your comment. Yes, it is the

`expression`

command that is used to write expressions in R-graphs. Its under the`base`

package, the default one.The menu over the comment box cannot be added to blogspot. I used a module to do this. Its a module for Drupal, called, BUEditor.

## Re: How to do this graph using R

Hello Sir,

nice post...we didn't know the "expression" code... Is that possible to make animated graphs with R codes? As far as I remember, I had seen one package, probably in R, producing some moving graphs with lots of colors in it. Or, it might be in MATLAB as well. But, with a package we can't really get into the basic codes and logic behind the outputs. If possible could you please make an entry of those codes to your post, so that we could use them for our work.

Aniqa.

## Re: How to do this graph using R

I am not sure if I understood your question clearly, did you want me to explain the codes that I used to produce the graphs? I did not use any special package in doing this. My primary objective was to show the use of expressions in R graphs. The function called

`expression`

is in base package, which comes with the default installation of R.I did not explore the animated graph thing in R, so I cannot comment on that.

If my answer is not what you really meant to ask, please let me know :)

## Re: How to do this graph using R

Not really I asked you to explain the codes you provided with. I was just wondering if you can give us some information on the animated graphs if possible in R. It was kind of a suggestion...'expression' is a new thing we learned from post, thanks :)

## Re: How to do this graph using R

Okay, I will keep that in mind. Why do you need that, by the way? I am asking to get an idea about what that graph might look like.

## Re: How to do this graph using R

Not sure what kind of animation is requested, but some cool 3d animation or point-click rotation is possible in R (as is possible in matlab).

Here is something I did last year using package "animation" - re-uploading here. You need to press ">" to start. I am guessing - you might need a good internet connection to see the whole run. All the codes / data / labels are included in that page. I modifies html code a bit to achieve that.

Later, if the provided link appears to be broken, feel free to contact :)

## Re: How to do this graph using R

I've found the

`animation`

package lately but thanks for giving a real implementation. I confirm that I can see the animation. But it may take a while to load with slow Internet connection.## Re: How to do this graph using R

A few days ago, some one called R an epic fail! The main reason being that the language is not for everybody (of course not!) and those who wants to learn it, have to go through enough trouble (thats why we are titled as statisticians :P, those jobs are for us - not for some one with just a mouse). In context of producing graphs, this applies as easily. Let me introduce three different grammers of R graphics - to produce same old histograms. For that, lets generate "something" to play with: symmetric distributions are too trivial - lets go for a skewed one ;)

`Generated.Data`

`The first choice is obviously graphics package. Lets set graphical window layout in graphics package, since I am always worried about the wasted space in the margin space (when inserted in a document / thesis, it looks awful just because of too wide margins, too small graphs and additional titles in every graph!):`

`par(mfrow = c(1,1), oma = c(0, 0, 0, 0), mar=c(4,4,0.5,0.5))`

`hist(Generated.Data, freq=FALSE, breaks = 25, main = "", col = "yellow")`

plot the density over the histogram`lines(density(Generated.Data), col="red", lwd = 3)`

This is the output. Aww ... too simple?

Before we move on, lets explore a bit more. Wouldn't it be nice to fill up the density plot?

`par(mfrow = c(1,1), oma = c(0, 0, 0, 0), mar=c(4,4,0.5,0.5))`

`plot(density(Generated.Data), col="red", main = "")`

Filling the density

`polygon(density(Generated.Data), col="light grey", border="dark grey")`

Adding a rug representation

`rug(Generated.Data, side=1, col = "grey")`

This is the output. Still not impressed?

`library(lattice)`

`histogram(~ Generated.Data, data = data.comb, type="count", col="red")`

This is the output.`densityplot( ~ Generated.Data, data = data.comb)`

This is the output. Formatting is nicer even with default codes.

`require(ggplot2)`

`qplot(Generated.Data,data=data.comb,binwidth=1)`

This is the output.`last_plot()+ylab("frequency")+xlab("Generated Data")`

This is the output. Notice the change of labels.

Copy as *.eps to use in a latex documant

`dev.copy2eps(file = paste("myplot",".eps", sep = ""), onefile = TRUE, paper = "a4",family = "Helvetica", pointsize=1, print.it = FALSE, fonts ="Helvetica")`

`dev.off()`

This can be applied in all of the above plots to save. Just remember to change the names.

Just showed at least three simplest ways to do the same thing in R. I guess I seriously need a better hobby :D FYI, don't be duped by these silly examples, this graphics grammers are very powerful and can be very hard, but eventually gives you fancy graphs! People wrote entire books on them! I couldn't finish reading them, yet. And I didn't even start about grid package :(

Ref:

## Re: How to do this graph using R

R is very powerful indeed in producing sophisticated graphics. Thanks for your input and I believe it will enlightened many.

## Re: How to do this graph using R

No luck for a speedy internet :(, still couldn't get into the package 'animation'... I don't actually need the codes for any meaningful purpose, I mean, something related to study! It's just about self interest that I wanted to see how the 3D animations are coded...I am looking for the package I saw in R producing those moving graphs, I'll soon let you know about that... thanks for the graphical information, they are really useful :)

## Re: How to do this graph using R

I guess a good starting point would be here: http://animation.yihui.name/animation:start

A direct link to the PDF manual for this package is:

http://cran.r-project.org/web/packages/animation/animation.pdf

## Re: How to do this graph using R

I didn't have any idea what R can do. My BS project analysis was done in R. But just for nice graphical presentation, I used SPSS :-(

## Re: How to do this graph using R

R can do amazing things. You just need to love it and understand it.