Skip to content

dotchart for matrix data

14 messages · David Winsemius, Ben Bolker, Peter Ehlers +2 more

#
Readers,

I am trying to use the function dotchart. The data is:
category values1 values2 values3 values4
1        a      10      27      56     709
2        b       4      46      47     208
3        c       5      17      18     109
4        d       6      50      49     308

The following error occurs
Error in dotchart(testdot, labels = testdot[, 1], groups = testdot[, 2]) :
        'x' must be a numeric vector or matrix

According to my understanding (clearly wrong!) of the documentation
for dotchart (accessed from the manual in section 'graphics'), columns
of data can be selected by 'groups' for subsequent plotting. The
objective is to be able to create a dot chart where each row is
labelled according to the row names in the 'category' column and two
columns can be selected, e.g. 'values1' and 'values2'. Then I tried:
A graph is produced, but not as expected. Instead of 4 rows labelled
(descending order from top row) a,b,c,d, and each row containing two
data points, the graph shows 8 rows (?) with the top 4 rows
un-labelled and the bottom 4 rows labelled (descending order) d,c,b,a
and each row shows only 1 datum point.

How do I specify the order of labelling of the rows?
How do I write correct commands to obtain a dot chart with 4 rows,
each row containing the two (or if required three) data points?

Thanks in advance.
#
On Dec 18, 2010, at 7:01 AM, e-letter wrote:

            
The misunderstanding is in how you see the grouping information versus  
how R expects it. R generally expects such data in what is called  
"long" format, i.e. there will be one values columns and a category  
column. There are various ways to change the arrangement of your data.  
The function stack(), the function reshape(), or probably most  
commonly the function melt from reshape2 being the typical chosen  
routes.
See if this is more to your liking:

require(reshape)   # I'm not sure why I have reshape_0.8.3 rather than  
reshape2 loaded
                    # I'm pretty sure Hadley would prefer that people  
use pkg:reshape2
  mdot <- melt(dot)
dotchart(mdot$value, groups=mdot$category, labels=mdot$variable)
# OR more readable
with(mdot, dotchart(value, groups=category, labels=variable)  )

I'm not sure I got the roles of "values" and "category" correct, but  
it should be a simple matter to switch them in the dotcghart call if  
that is your pleasuRe.
David Winsemius, MD
West Hartford, CT
#
David Winsemius <dwinsemius <at> comcast.net> writes:
Following up on David's response:


d <- read.table(textConnection("category values1 values2 values3 values4
1        a      10      27      56     709
2        b       4      46      47     208
3        c       5      17      18     109
4        d       6      50      49     308"),
                header=TRUE)

## Something like this is probably as close as you can get with
## stock 'dotchart' -- it does *not* (as far as I can tell) put
## different points on the same line, just groups lines

dotchart(as.matrix(d[,-1]),labels=as.character(d[,1]))
dotchart(as.matrix(d[,c("values1","values2")]),labels=as.character(d[,1]))

## reshaping data:
library(reshape)
mdot <- melt(d)

## using the lattice package

library(lattice)
dotplot(value~category,groups=variable,data=mdot)
dotplot(value~variable,groups=category,data=mdot,auto.key=TRUE,
        scales=list(y=list(log=10)))

## you could also use ggplot2 ...
#
On 18/12/2010, David Winsemius <dwinsemius at comcast.net> wrote:
Reshape and melt are not installed (version251) so for this task
manual rearrangement data is easier.
Loading required package: reshape
[1] FALSE
Warning message:
there is no package called 'reshape' in: library(package, lib.loc =
lib.loc, character.only = TRUE, logical = TRUE,
Error in library(reshape) : there is no package called 'reshape'
Error: could not find function "melt"

However before doing so why this is relevant because of the
alternative creation objects 'testdot1'. Aren't these objects
suitable, since a (undesireable) graph was produced?
I don't have dotcghart either.
#
I am trying to create a chart like this
(http://www.b-eye-network.com/images/content/Fig4_3.jpg); so this is
not possible using R?
No documentation for 'ggplot2' in specified packages and libraries:
you could try 'help.search("ggplot2")'; seems I need to retrieve this
package first. Thanks for the suggestion.
#
On 2010-12-18 07:50, e-letter wrote:
[... snip ...]
That looks an awful lot like what lattice's dotplot would
produce. So: have you tried dotplot() as Ben has suggested?

Peter Ehlers
#
On 18/12/2010, Peter Ehlers <ehlers at ucalgary.ca> wrote:
Unfortunately I have been unable to use the "melt" package; 3 mirrors
have failed to install. What I don't understand is why the objects
created are not in the correct format, otherwise how would the plot be
created?
#
On Dec 18, 2010, at 11:58 AM, e-letter wrote:

            
There is no melt package. There is a reshape package and a reshape2  
package, either of which will have melt as a function.
David Winsemius, MD
West Hartford, CT
#
On 18/12/2010, Peter Ehlers <ehlers at ucalgary.ca> wrote:
For the benefit of other novices, this is what I did (gnu/linux):

sign-in to a command terminal as root
start R
install.packages("reshape2")

In another terminal as normal user

require(reshape2)
mdot<-melt(testdot)
dotplot(value~category,groups=variable,data=mdot)

The resultant graph shows the categories on the abscissa and there
does not seem to be a way of selecting two columns from the original
matrix. It is not like the graph cited in the hyperlink quoted above,
but at least I have successfully followed instructions!

I changed the axes with the command:

dotplot(category~value,groups=variable,data=mdot)

I believe there is a command to select only certain rows of data which
I think will achieve the desired graph, if unable I'll ask again, so
thank you all.
#
On Dec 18, 2010, at 1:27 PM, e-letter wrote:

            
If you want to see a worked example of a dotplot that has two separate  
data series on the same set of lines then look at Fig 10.18 in  
Sarkar's "Lattice". The illustration and code are all at the book's  
website. I don't think you did a very good job of explaining in text  
what you expected to see (at least on my reading of it more than once)  
but this may do what you hoped:

dotplot(as.character(mdot$category) ~ mdot$value, labels=mdot$variable)
David Winsemius, MD
West Hartford, CT
#
On 2010-12-18 10:27, e-letter wrote:
It seems to me that you're getting exactly the plot referred
to by the link, except that you have a factor with 4 levels, not 2.
See if the 'subset=' argument works for you:

  dotplot(category ~ value, groups = variable, data = mdot,
          subset = {variable %in% c("values1", "values2")},
          pch = c(1,3), cex = 1.5)

Peter Ehlers
1 day later
#
On 18/12/2010, e-letter <inpost at gmail.com> wrote:
If one set of value ranges for 10-20 and another set ranges form
1000-1500, how to adjust the graph such that:

with categories on the ordinate (y-axis), can the bottom abscissa
(x-axis bottom) be set with a scale suitable for the data set of range
1000-1500?
can the top abscissa (x-top) be set with a scale 10-20?
or is it better practice to change the scale of the data set 1000-1500
by two orders of magnitude?
how is it possible to control the order of the category variables? For
example, if the graph shows:

a
b
c
d

Is it possible to change to

a
d
b
c

Thanks
1 day later
#
On 20/12/2010, e-letter <inpost at gmail.com> wrote:
A csv file is imported:
category values
1        a     10
2        b     44
3        c     51
4        d     65
5        a     27
6        b     64
7        c     71
8        d     49

A dotplot is produced with the command:

dotplot(category~values,data=testdot)

The csv file is adjusted to move the values of 'a':
category values
1        b     44
2        c     51
3        d     65
4        a     10
5        b     64
6        c     71
7        d     49
8        a     27

Why  does the order of the categories not change from d,c,b,a to
a,d,c,b (descending from top)?