Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

I want to draw vertical line graph (like the first graph on this page):

enter image description here

The deal is I have about ~15k data points whose frequencies (along Y axis) I want to present. I also want to be able to to present the frequencies in different colors : for example for the 1st 5k points, I want yellow colored line graphs for the next 5k I want red etc.

How can I do this in R? Pointers to packages (and to helpful source code where possible) will be very useful. Beginner in R here.

Thank you.

share|improve this question
How many categories do these points occur in? How are the points numbered? – Michael Bishop Oct 19 '11 at 2:39
4  
The plot on the page you point to has 10 vertical lines. You want to do that with 15k lines? A typical display is <2000 pixels wide. – Karl Oct 19 '11 at 2:48
@Michael : Well it's simply $X$ number of points that have some frequencies associated with them. I have 3 categories. Karl : yes, so the lines will be very close to each other and it will seem like the oscillation plots that you see. – user721975 Oct 19 '11 at 19:59
@Karl : On more thinking, I think I get your point now. I will consider making plots for values that exceed a certain frequency threshold. – user721975 Oct 19 '11 at 20:08

2 Answers

up vote 2 down vote accepted

You can use the plot function with type="h" to get the vertical lines and col to specify the colors, using rep to create the vector of colors that you want, as follows:

# simulate some data
x <- runif(15000)
x[sample(15000, 50)] <- runif(50, 0, 5)

# make the plot
plot(x, type="h", col=rep(c("red", "blue", "green"), each=5000))

This makes the following (yellow looked terrible):

enter image description here

share|improve this answer
(+1): I should have waited for clarification from the OP. – steffen Oct 20 '11 at 6:32
Super. Exactly what I was looking for. – user721975 Oct 20 '11 at 15:06

Use a barplot in combination with the grDevices-package to create a color-palette.

require(grDevices)

# data
dat <- sample(1:10,15000,prob=runif(10),replace=T)
dat <- sort(dat)
plotdat <- as.data.frame(table(dat))
plotdat[,2] <- plotdat[,2]/sum(plotdat[,2])

# generate colors
colors <- heat.colors(10)
# and sort them according to frequency
colors <- colors[order(order(plotdat[,2],decreasing=T))]
barplot(plotdat[,2],names.arg=as.character(1:10),col=colors)

This creates a plot with the property "the higher the color-heat, the higher the frequency"

enter image description here

share|improve this answer
This looks good. Except that I want contiguous blocks of red, followed by contiguous blocks of yellow etc. – user721975 Oct 19 '11 at 20:03
@user721975 I ordered the colors. If you skip this step, the color increases from left to right. Another option is to provide a vector of colors (vector-length=number of bars to plot) by using rep(). – steffen Oct 20 '11 at 6:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.