Hi, I have what should be a simple question. I would like to generate a histogram of x <- c("a","b","c","b","c","c") where the first bar to be labeled 'c' with height 3, second bar to be labeled 'b' with height 2 and third bar to be labeled 'a' with height 1. This should be an easy task in R but I think I am missing something? Thanks, Arend van der Veen
On 18 Nov 2003, Arend P. van der Veen wrote:> Hi, > > I have what should be a simple question. I would like to generate a > histogram of > > x <- c("a","b","c","b","c","c") > > where the first bar to be labeled 'c' with height 3, second bar to be > labeled 'b' with height 2 and third bar to be labeled 'a' with height 1. > > This should be an easy task in R but I think I am missing something? > > Thanks, > Arend van der Veen > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >You don't want a histogram, but a barplot: barplot(table(x)) Uwe Ligges
Arend P. van der Veen wrote:> Hi, > > I have what should be a simple question. I would like to generate a > histogram of > > x <- c("a","b","c","b","c","c") > > where the first bar to be labeled 'c' with height 3, second bar to be > labeled 'b' with height 2 and third bar to be labeled 'a' with height 1.Maybe the following would help you: x <- c("a","b","c","b","c","c") x.fac <- factor(x) plot(x.fac) Best Regards, Ryota Suzuki> > This should be an easy task in R but I think I am missing something? > > Thanks, > Arend van der Veen > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
plot(table(factor(x,levels=c("c","b","a")))) is at least approximately what you want (the only complicated bit is reversing the order of the bars from the default alphabetical order) substituting barplot() for plot() also works you may want to use ylab="something" in the plot or barplot command to get a more descriptive axis label On 18 Nov 2003, Arend P. van der Veen wrote:> Hi, > > I have what should be a simple question. I would like to generate a > histogram of > > x <- c("a","b","c","b","c","c") > > where the first bar to be labeled 'c' with height 3, second bar to be > labeled 'b' with height 2 and third bar to be labeled 'a' with height 1. > > This should be an easy task in R but I think I am missing something? > > Thanks, > Arend van der Veen > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >-- 620B Bartram Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704
"Arend P. van der Veen" <apv at capital.net> writes:> Hi, > > I have what should be a simple question. I would like to generate a > histogram of > > x <- c("a","b","c","b","c","c") > > where the first bar to be labeled 'c' with height 3, second bar to be > labeled 'b' with height 2 and third bar to be labeled 'a' with height 1. > > This should be an easy task in R but I think I am missing something?That's not a histogram... barplot(rev(table(x))) will do it, or - allowing more general level shuffling - barplot(table(factor(x,levels=c("c","b","a")))) or barplot(rev(sort(table(x)))) if you want the columns by decreasing frequency. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On 18 Nov 2003 10:29:50 -0500, "Arend P. van der Veen" <apv at capital.net> wrote :>Hi, > >I have what should be a simple question. I would like to generate a >histogram of > >x <- c("a","b","c","b","c","c") > >where the first bar to be labeled 'c' with height 3, second bar to be >labeled 'b' with height 2 and third bar to be labeled 'a' with height 1. > >This should be an easy task in R but I think I am missing something?Try barplot(rev(table(x))). You use table() to calculate frequencies of discrete classes (and hist() to count frequencies of different bins for continuous variables), rev() to reverse the order from the natural order, and barplot() to plot the bars. Duncan Murdoch