Readers, I am trying to use the function dotchart. The data is:> testdotcategory 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> dotchart(testdot,groups=testdot[,2])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:> testdot1<-testdot[,1] > testdot2<-testdot[,2] > testdot3<-testdot[,3] > dotchart(c(testdot2,testdot3),labels=testdot1)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:> Readers, > > I am trying to use the function dotchart. The data is: > >> testdot > 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 > >> dotchart(testdot,groups=testdot[,2]) > 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 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.> 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: > >> testdot1<-testdot[,1] >> testdot2<-testdot[,2] >> testdot3<-testdot[,3] >> dotchart(c(testdot2,testdot3),labels=testdot1)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.> > 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?David Winsemius, MD West Hartford, CT
>Ben Bolker >Sat, 18 Dec 2010 07:07:24 -0800 >David Winsemius <dwinsemius <at> comcast.net> writes: > >> >> >> On Dec 18, 2010, at 7:01 AM, e-letter wrote: >> >> > Readers, >> > >> > I am trying to use the function dotchart. The data is: >> > >> >> testdot >> > 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 >> > >> >> dotchart(testdot,groups=testdot[,2]) >> > 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. >> > > 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 >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?>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 ...> ?ggplot2No 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.
Readers, The following commands were applied, to create a dot chart with black dots and blue squares for data:> library(lattice) > testdotcategory 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 dotplot(category~values,col=c("black","black","black","black","blue","blue","blue","blue"),bg=c("black","black","black","black","blue","blue","blue","blue"),pch=c(21,21,21,21,22,22,22,22),xlab=NULL, data=testdot) The resultant graph shows correctly coloured points, but not filled, only the border is coloured. The documentation for the command 'pch' (?pch) indicates that the commands shown above should show appropriately coloured solid symbols. What is causing this error please?