I have some data like

A  2
A  4
A  76
B  8
B  13
.. ....

Basically, what is the spread of A, B, etc. What would be a suitable graph to visualize such information in R?

  • 2
    $\begingroup$ Can you provide some more information about your data. How many points might there be for "A", "B", etc? Are the values in the second column presumably continuous? $\endgroup$ – Ananda Mahto Jun 30 '13 at 9:42
  • $\begingroup$ Sure. Thanks for your response. The numeric values are age, so numeric data is not continuous but discrete. There can be any number of points for A, B, etc., ranging from, say, 1 to 150. $\endgroup$ – Kumar Vaibhav Jun 30 '13 at 9:46
  • $\begingroup$ How many values in each group? $\endgroup$ – Glen_b Jun 30 '13 at 9:49
  • $\begingroup$ So, there can be varying number of values in each group. Say, 20 for A and then 100 for B and then maybe 67 for C, and so on. $\endgroup$ – Kumar Vaibhav Jun 30 '13 at 9:52

There are numerous possible displays, depending on what more specifically you want.

One example would be a boxplot for each group (A, B, ...) (assuming there are enough values in each group to support one*):



But you might want to look at histograms, ecdfs, or a number of other possibilities

* Edit: from your later comments, it looks like there's enough data for boxplots.

  • $\begingroup$ Excellent! Let me try this! $\endgroup$ – Kumar Vaibhav Jun 30 '13 at 9:53
  • $\begingroup$ Some other possibilities that haven't been mentioned in the answers yet would include Q-Q plots, beeswarm/beehive plots, and violin plots $\endgroup$ – Glen_b Jul 28 '13 at 1:15

You already got some excellent answers but let me suggest another plot that was not mentioned yet (this is an example that I created to answer another question):

Dot plot

In R, it is available e.g. through stripchart() or ggplot2's geom_point() or geom_jitter(). (Jitter adds a little bit of noise to avoid too much overlap.) This plot allows you to look at the data somewhat more directly than histograms (which can be badly misleading, see Glen_b's great answer to another question) or boxplots (which are great but a little more complicated to understand and explain).

In GGPlot, you can also combine boxplots and jittered dots, see the documentation.

  • 1
    $\begingroup$ +1. There are many, many names for this plot. Dot and strip plots or charts are two of the most common. As "dot" is widely used in other senses, the name element "strip" is perhaps preferable, at least in searching for implementations (there are several, not just in R but also in all leading packages or languages, e.g. Stata stripplot). $\endgroup$ – Nick Cox Jun 30 '13 at 12:17
  • $\begingroup$ This is awesome! $\endgroup$ – Kumar Vaibhav Jun 30 '13 at 12:28
  • $\begingroup$ @NickCox +1 Thanks for the precisions, I was so confused by the names that I tried very hard not to choose one… Your comment reminded me of another R function to create a stripchart (now added to my answer). $\endgroup$ – Gala Jun 30 '13 at 12:48
  • $\begingroup$ But how do I make this if one of the axes doesn't have any numeric data? $\endgroup$ – Kumar Vaibhav Jun 30 '13 at 14:02
  • $\begingroup$ No problem i did it - ggplot() + geom_point(data=mydata, mapping=aes(x=mydata[,3], y=mydata[,2])) + coord_cartesian(xlim = c(5, 90)) + xlab("Age") + ylab("Something") + ggtitle("Demographic Analysis of Age and something") $\endgroup$ – Kumar Vaibhav Jun 30 '13 at 14:15

As mentioned by Glen_b, there are a number of possibilities.

Here is an example of a histogram and density plot using the "lattice" package. I've also provided some sample data.

mydf <- data.frame(V1 = sample(LETTERS[1:5], 500, replace = TRUE),
                   V2 = sample(0:50, 500, replace = TRUE))
histogram(~V2 | V1, data = mydf)

enter image description here

densityplot(~V2 | V1, data = mydf)

enter image description here

Both are with default settings applied.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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