Hi all! I do hope question from newcomers are wellcome here! Thanks in advance. Trying to catch up and to acquired the needed background to easily read R documents it is being a bit hard to me to get into the required concepts to deal with data. I am trying to get data from a MySQL database and plotting it with barplot. Here the code... library(RMySQL) con <- dbConnect(dbDriver ("MySQL"),host='localhost',username='root',dbname='ibdona') rs <- dbGetQuery (con,"select n,year from ibdona.library_location") dbDisconnect(con) Graph <- barplot(rs) And here the error I get... Error in barplot.default(rs) : 'height' must be a vector or a matrix paste(rs) gives me... [1] "c(307, 65, 2, 28, 3, 229, 81, 5, 7, 558, 134, 53, 9)" [2] "c(2002, 2002, 2002, 2002, 2002, 2003, 2003, 2003, 2003, 2004, 2004, 2004, 2004)" So, I am guessing I've gotten the data but there is at least an step lacking to be able to use it as the entry matrix required by barplot. Please, could you help me with this issue? What is the step/steps lacking in my quite short code? Thanks!!! Best, -- Ricardo Rodríguez Your EPEC ICT Team [[alternative HTML version deleted]]
On Nov 7, 2006, at 6:25 PM, Ricardo Rodr?guez - Your EPEC ICT Team wrote:> library(RMySQL) > con <- dbConnect(dbDriver > ("MySQL"),host='localhost',username='root',dbname='ibdona') > rs <- dbGetQuery (con,"select n,year from ibdona.library_location") > dbDisconnect(con) > Graph <- barplot(rs) > > And here the error I get... > > Error in barplot.default(rs) : 'height' must be a vector or a matrix > > paste(rs) gives me... > > [1] "c(307, 65, 2, 28, 3, 229, 81, 5, 7, 558, 134, 53, 9)" > [2] "c(2002, 2002, 2002, 2002, 2002, 2003, 2003, 2003, 2003, 2004, > 2004, 2004, 2004)"nums <- c(307, 65, 2, 28, 3, 229, 81, 5, 7, 558, 134, 53, 9) names(nums) <- c(2002, 2002, 2002, 2002, 2002, 2003, 2003, 2003, 2003, 2004, 2004, 2004, 2004) barplot(nums) or require(gplots) barplot2(nums, plot.grid = TRUE, las = 1) # offers many nice options _____________________________ Professor Michael Kubovy University of Virginia Department of Psychology USPS: P.O.Box 400400 Charlottesville, VA 22904-4400 Parcels: Room 102 Gilmer Hall McCormick Road Charlottesville, VA 22903 Office: B011 +1-434-982-4729 Lab: B019 +1-434-982-4751 Fax: +1-434-982-4766 WWW: http://www.people.virginia.edu/~mk9y/
On Nov 8, 2006, at 11:12 AM, Patrick Burns wrote:> My guess is that you have a data frame and not a matrix. > Try > > barplot(as.matrix(rs)) > > See chapter 1 of S Poetry for an explanation of data structures > in R.Thanks, Patrick, You are completely right. I've been trying to plot a data frame. I'd found a file this morning written by a Portuguese professor at Lisbon that helps me a lot. Allow me to post here the link even though it is already available at http://cran.r-project.org/other-docs.html. I'm guessing it can be of much help for newcomers like me myself. http://cran.r-project.org/doc/contrib/Torgo-ProgrammingIntro.pdf With your help and much more reading I will master R in some more... years! :-) Best regards, Ricardo
On Nov 8, 2006, at 11:41 AM, Michael Kubovy wrote:> On Nov 8, 2006, at 4:44 AM, Ricardo Rodr?guez wrote: > > Hi Ricardo, > > You probably have two columns in 'rs'. You need to do the barplot > on one of them and use the other as the vector of labels. Assuming > that the first column is n and the second is years: > barplot(n, names.arg = years, xlab = 'years', ylab = 'number of > events') > > if the columns are not labelled: > barplot(rs[, 1], names.arg = rs[, 2], xlab = 'years', ylab = > 'number of events') > > For more details, > ?barplot > or > ?barplot2 > Also try RSiteSearch('barplot') > See also http://wiki.r-project.org/rwiki/doku.php? > id=graph_gallery:graph54Thank you so much for the detailed answer, Michael. Once I've understood that I've been trying to plot a data frame without specifying the columns I wanted to use thing are going smoother over here! Your answer has directed me to the RGraphGallery. What a repository of great ideas! I do hope I can contribute in the near future. Just two tips: I don't know why, I am not able to use the link you have posted to reach graph 54. This one works for me... http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=54 As for my error trying to plot a data frame, ?dbGetQuery clearly states that the function returns a data.frame with the results of the query. 4.3.1 Packages DBI and RMySQL clearly reads the same thing, dbGetquery sends the query and retrieves the results as a data frame. So, I must apology. Only my lack of skills and a non attentive reading of the manuals have caused this doubt. Thanks for your help. With my best regards, Ricardo