[R] need help in plotting barchart

Petr PIKAL petr.pikal at precheza.cz
Fri Nov 7 13:37:34 CET 2008


"Kurapati, Ravichandra \(Ravichandra\)" 
<ravichandra.kurapati at alcatel-lucent.com> napsal dne 07.11.2008 11:06:28:

> Hi
>    I want NA counts also.


Well

barley[5:15, 1] <- NA
barchart(yield ~ variety | site, data = barley,
          groups = year, layout = c(1,6),
          ylab = "Barley Yield (bushels/acre)",
          scales = list(x = list(abbreviate = TRUE,
                        minlength = 5)))

works and bars with NA values are missing but all labels are preserved.

So obviously everything works. Without reproducible example what you did 
and what and how something is not as you expect it is rather difficult to 
provide suitable advice.

Regards
Petr 


> 
> K.Ravi 
> 
> -----Original Message-----
> From: Petr PIKAL [mailto:petr.pikal at precheza.cz] 
> Sent: Friday, November 07, 2008 3:31 PM
> To: Kurapati, Ravichandra (Ravichandra)
> Cc: r-help at r-project.org
> Subject: Odp: [R] need help in plotting barchart
> 
> Hi
> 
> I did not see any response and will not give you any answer too, just a 
> hint. Your code can not be reproduced and it is also difficult to say
> what 
> you really want.
> 
> Eg. this is rather confusing.
> 
> > On a generated plot  I observed that what ever records have counts as
> > "NA", those are not suppressed. but I don't want them to be
> suppressed.
> 
> Do you want NA to be suppressed or not?
> 
> I would
> 
> ensure my data frame has variables with correct class and then I would 
> select those with non zero and non NA counts by e.g.
> 
> Df.OK <- Df[which(Df$counts),]
> 
> if you want to drop unused levels of factor you can use e.g. drop=T in 
> subseting this factor
> 
> Regards
> Petr
> 
> 
> r-help-bounces at r-project.org napsal dne 06.11.2008 12:30:13:
> 
> > Df contains 
> > 
> >      Session_Setup   DCT RevDataVols_bin    counts
> > comp
> > 
> > 1    Session_Setup   RLL               1        NA
> > Session_Setup+RLL+1
> > 
> > 2    Session_Setup   RLL               2        NA
> > Session_Setup+RLL+2
> > 
> > 3    Session_Setup   RLL               3        NA
> > Session_Setup+RLL+3
> > 
> > 4    Session_Setup   RLL               4        NA
> > Session_Setup+RLL+4
> > 
> > 5    Session_Setup   RLL               5        NA
> > Session_Setup+RLL+5
> > 
> > 6    Session_Setup   RLL               6        NA
> > Session_Setup+RLL+6
> > 
> > 7    Session_Setup   RLL               7        NA
> > Session_Setup+RLL+7
> > 
> > 8    Session_Setup   RLL               8        NA
> > Session_Setup+RLL+8
> > 
> > 9    Session_Setup   RLL               9        NA
> > Session_Setup+RLL+9
> > 
> > 10   Session_Setup   RLL              10        NA
> > Session_Setup+RLL+10
> > 
> > 11   Session_Setup   RLL              11        NA
> > Session_Setup+RLL+11
> > 
> > 12   Session_Setup   RLL              12        NA
> > Session_Setup+RLL+12
> > 
> > 13   Session_Setup   RLL              13        NA
> > Session_Setup+RLL+13
> > 
> > 14   Session_Setup   RLL              14        NA
> > Session_Setup+RLL+14
> > 
> > 15   Session_Setup   RLL              15        NA
> > Session_Setup+RLL+15
> > 
> > 16   Session_Setup   RLL              16        NA
> > Session_Setup+RLL+16
> > 
> > 17   Session_Setup   RLL              17        NA
> > Session_Setup+RLL+17
> > 
> > 18   Session_Setup   RLL              18        NA
> > Session_Setup+RLL+18
> > 
> > 19   Session_Setup   RLL              19        NA
> > Session_Setup+RLL+19
> > 
> > 20   Session_Setup   RLL              20        NA
> > Session_Setup+RLL+20
> > 
> > 21   Session_Setup   RLL              21        NA
> > Session_Setup+RLL+21
> > 
> > 22   Session_Setup   RLL              22        NA
> > Session_Setup+RLL+22
> > 
> > 23   Session_Setup   RLL              23        NA
> > Session_Setup+RLL+23
> > 
> > 24   Session_Setup   RLL              24        NA
> > Session_Setup+RLL+24
> > 
> > 25   Session_Setup   RLL              25        NA
> > Session_Setup+RLL+25
> > 
> > 26   Session_Setup   RLL              26        NA
> > Session_Setup+RLL+26
> > 
> > 27   Session_Setup   RLL              27        NA
> > Session_Setup+RLL+27
> > 
> > 28   Session_Setup   RLL              28        NA
> > Session_Setup+RLL+28
> > 
> > 29   Session_Setup   RLL              29        NA
> > Session_Setup+RLL+29
> > 
> > 30   Session_Setup   RLL              30        NA
> > Session_Setup+RLL+30
> > 
> > 31  User_Initiated   RLL               1  8.487840
> > User_Initiated+RLL+1
> > 
> > 32  User_Initiated   RLL               2  6.066089
> > User_Initiated+RLL+2
> > 
> > 33  User_Initiated   RLL               3        NA
> > User_Initiated+RLL+3
> > 
> > 34  User_Initiated   RLL               4        NA
> > User_Initiated+RLL+4
> > 
> > 35  User_Initiated   RLL               5  5.906891
> > User_Initiated+RLL+5
> > 
> > 36  User_Initiated   RLL               6        NA
> > User_Initiated+RLL+6
> > 
> > 37  User_Initiated   RLL               7        NA
> > User_Initiated+RLL+7
> > 
> > 38  User_Initiated   RLL               8        NA
> > User_Initiated+RLL+8
> > 
> > 39  User_Initiated   RLL               9        NA
> > User_Initiated+RLL+9
> > 
> > 40  User_Initiated   RLL              10        NA
> > User_Initiated+RLL+10
> > 
> > 41  User_Initiated   RLL              11        NA
> > User_Initiated+RLL+11
> > 
> > 42  User_Initiated   RLL              12        NA
> > User_Initiated+RLL+12
> > 
> > 43  User_Initiated   RLL              13        NA
> > User_Initiated+RLL+13
> > 
> > 44  User_Initiated   RLL              14        NA
> > User_Initiated+RLL+14
> > 
> > 45  User_Initiated   RLL              15        NA
> > User_Initiated+RLL+15
> > 
> > 46  User_Initiated   RLL              16        NA
> > User_Initiated+RLL+16
> > 
> > 47  User_Initiated   RLL              17        NA
> > User_Initiated+RLL+17
> > 
> > 48  User_Initiated   RLL              18        NA
> > User_Initiated+RLL+18
> > 
> > 49  User_Initiated   RLL              19        NA
> > User_Initiated+RLL+19
> > 
> > 50  User_Initiated   RLL              20        NA
> > User_Initiated+RLL+20
> > 
> > 51  User_Initiated   RLL              21        NA
> > User_Initiated+RLL+21
> > 
> > 52  User_Initiated   RLL              22        NA
> > User_Initiated+RLL+22
> > 
> > 53  User_Initiated   RLL              23        NA
> > User_Initiated+RLL+23
> > 
> > 54  User_Initiated   RLL              24        NA
> > User_Initiated+RLL+24
> > 
> > 55  User_Initiated   RLL              25        NA
> > User_Initiated+RLL+25
> > 
> > 56  User_Initiated   RLL              26        NA
> > User_Initiated+RLL+26
> > 
> > 57  User_Initiated   RLL              27        NA
> > User_Initiated+RLL+27
> > 
> > 58  User_Initiated   RLL              28        NA
> > User_Initiated+RLL+28
> > 
> > 59  User_Initiated   RLL              29        NA
> > User_Initiated+RLL+29
> > 
> > 60  User_Initiated   RLL              30        NA
> > User_Initiated+RLL+30
> > 
> > 61   Session_Setup NoRLL               1  8.906891
> > Session_Setup+NoRLL+1
> > 
> > 62   Session_Setup NoRLL               2        NA
> > Session_Setup+NoRLL+2
> > 
> > 63   Session_Setup NoRLL               3        NA
> > Session_Setup+NoRLL+3
> > 
> > 64   Session_Setup NoRLL               4        NA
> > Session_Setup+NoRLL+4
> > 
> > 65   Session_Setup NoRLL               5        NA
> > Session_Setup+NoRLL+5
> > 
> > 66   Session_Setup NoRLL               6        NA
> > Session_Setup+NoRLL+6
> > 
> > 67   Session_Setup NoRLL               7        NA
> > Session_Setup+NoRLL+7
> > 
> > 68   Session_Setup NoRLL               8        NA
> > Session_Setup+NoRLL+8
> > 
> > 69   Session_Setup NoRLL               9        NA
> > Session_Setup+NoRLL+9
> > 
> > 70   Session_Setup NoRLL              10        NA
> > Session_Setup+NoRLL+10
> > 
> > 71   Session_Setup NoRLL              11        NA
> > Session_Setup+NoRLL+11
> > 
> > 72   Session_Setup NoRLL              12        NA
> > Session_Setup+NoRLL+12
> > 
> > 73   Session_Setup NoRLL              13        NA
> > Session_Setup+NoRLL+13
> > 
> > 74   Session_Setup NoRLL              14        NA
> > Session_Setup+NoRLL+14
> > 
> > 75   Session_Setup NoRLL              15        NA
> > Session_Setup+NoRLL+15
> > 
> > 76   Session_Setup NoRLL              16        NA
> > Session_Setup+NoRLL+16
> > 
> > 77   Session_Setup NoRLL              17        NA
> > Session_Setup+NoRLL+17
> > 
> > 78   Session_Setup NoRLL              18        NA
> > Session_Setup+NoRLL+18
> > 
> > 79   Session_Setup NoRLL              19        NA
> > Session_Setup+NoRLL+19
> > 
> > 80   Session_Setup NoRLL              20        NA
> > Session_Setup+NoRLL+20
> > 
> > 81   Session_Setup NoRLL              21        NA
> > Session_Setup+NoRLL+21
> > 
> > 82   Session_Setup NoRLL              22        NA
> > Session_Setup+NoRLL+22
> > 
> > 83   Session_Setup NoRLL              23        NA
> > Session_Setup+NoRLL+23
> > 
> > 84   Session_Setup NoRLL              24        NA
> > Session_Setup+NoRLL+24
> > 
> > 85   Session_Setup NoRLL              25        NA
> > Session_Setup+NoRLL+25
> > 
> > 86   Session_Setup NoRLL              26        NA
> > Session_Setup+NoRLL+26
> > 
> > 87   Session_Setup NoRLL              27        NA
> > Session_Setup+NoRLL+27
> > 
> > 88   Session_Setup NoRLL              28        NA
> > Session_Setup+NoRLL+28
> > 
> > 89   Session_Setup NoRLL              29        NA
> > Session_Setup+NoRLL+29
> > 
> > 90   Session_Setup NoRLL              30        NA
> > Session_Setup+NoRLL+30
> > 
> > 91  User_Initiated NoRLL               1 12.909331
> > User_Initiated+NoRLL+1
> > 
> > 92  User_Initiated NoRLL               2 10.878817
> > User_Initiated+NoRLL+2
> > 
> > 93  User_Initiated NoRLL               3  9.499846
> > User_Initiated+NoRLL+3
> > 
> > 94  User_Initiated NoRLL               4  8.891784
> > User_Initiated+NoRLL+4
> > 
> > 95  User_Initiated NoRLL               5  7.888743
> > User_Initiated+NoRLL+5
> > 
> > 96  User_Initiated NoRLL               6  7.930737
> > User_Initiated+NoRLL+6
> > 
> > 97  User_Initiated NoRLL               7  7.475733
> > User_Initiated+NoRLL+7
> > 
> > 98  User_Initiated NoRLL               8  6.906891
> > User_Initiated+NoRLL+8
> > 
> > 99  User_Initiated NoRLL               9  5.906891
> > User_Initiated+NoRLL+9
> > 
> > 100 User_Initiated NoRLL              10  6.882643
> > User_Initiated+NoRLL+10
> > 
> > 101 User_Initiated NoRLL              11        NA
> > User_Initiated+NoRLL+11
> > 
> > 102 User_Initiated NoRLL              12  6.894818
> > User_Initiated+NoRLL+12
> > 
> > 103 User_Initiated NoRLL              13        NA
> > User_Initiated+NoRLL+13
> > 
> > 104 User_Initiated NoRLL              14  5.906891
> > User_Initiated+NoRLL+14
> > 
> > 105 User_Initiated NoRLL              15  5.882643
> > User_Initiated+NoRLL+15
> > 
> > 106 User_Initiated NoRLL              16  5.906891
> > User_Initiated+NoRLL+16
> > 
> > 107 User_Initiated NoRLL              17        NA
> > User_Initiated+NoRLL+17
> > 
> > 108 User_Initiated NoRLL              18        NA
> > User_Initiated+NoRLL+18
> > 
> > 109 User_Initiated NoRLL              19        NA
> > User_Initiated+NoRLL+19
> > 
> > 110 User_Initiated NoRLL              20        NA
> > User_Initiated+NoRLL+20
> > 
> > 111 User_Initiated NoRLL              21        NA
> > User_Initiated+NoRLL+21
> > 
> > 112 User_Initiated NoRLL              22        NA
> > User_Initiated+NoRLL+22
> > 
> > 113 User_Initiated NoRLL              23  5.906891
> > User_Initiated+NoRLL+23
> > 
> > 114 User_Initiated NoRLL              24        NA
> > User_Initiated+NoRLL+24
> > 
> > 115 User_Initiated NoRLL              25        NA
> > User_Initiated+NoRLL+25
> > 
> > 116 User_Initiated NoRLL              26        NA
> > User_Initiated+NoRLL+26
> > 
> > 117 User_Initiated NoRLL              27        NA
> > User_Initiated+NoRLL+27
> > 
> > 118 User_Initiated NoRLL              28        NA
> > User_Initiated+NoRLL+28
> > 
> > 119 User_Initiated NoRLL              29  5.906891
> > User_Initiated+NoRLL+29
> > 
> > 120 User_Initiated NoRLL              30        NA
> > User_Initiated+NoRLL+30 
> > 
> > 
> > 
> > 
> > 
> > With the following code I tried to generate a barchart  plot
> > 
> > 
> > 
> > 
> > 
> > ok <- df$counts>0 & is.finite(df$counts)
> > 
> >    ylim <- c(0, ceiling(max(log2(df$counts[ok]))))
> > 
> >    by.n <- ifelse(abs(diff(ylim)) <= 5, 1, 2)
> > 
> >    at <- seq(from=0, to = ylim[2], by = by.n)
> > 
> >    lbls <- format(2^at)
> > 
> >    yscales <- list(y=list(at = at, labels = lbls))   ## log base 2
> ticks
> > 
> >    scales = c(list(...)$xscales, yscales)     ## combine passed
> scales,
> > if any
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> >    grps <- df[[group.var]][ok, drop = FALSE]
> > 
> >    cond <- df[[cond.var]][ok, drop = FALSE]
> > 
> > 
> > 
> >    j <- match(group.var, names(df))
> > 
> >    names(df)[j] <- "grps"
> > 
> >    j <- match(cond.var, names(df))
> > 
> >    names(df)[j] <- "cond"
> > 
> > 
> > 
> >    f <- formula(sprintf("counts ~ %s | cond * grps", resp.var))
> > 
> >    layout <- c(length(levels(cond)), length(levels(grps)))
> > 
> > 
> > 
> >    print(
> > 
> >       barchart(f,
> > 
> >          data = df,
> > 
> >          subset = ok,
> > 
> >          as.table = TRUE, horizontal = FALSE,
> > 
> >          drop.unused.levels = list(cond=FALSE,data=FALSE),
> > 
> >          main = main, sub = sub, xlab = xlab, ylab = ylab,
> > 
> >          ylim = ylim,
> > 
> >          panel = function(x, y, ...){
> > 
> >                     panel.abline(h = at, col.line = grey(.75))
> > 
> >                     panel.barchart(x, y, ...)
> > 
> > 
> > 
> >                         x1 <-x
> > 
> >                         y1 <-y
> > 
> > 
> > 
> >                         horz <- list(...)$horizontal
> > 
> >                         n<-0
> > 
> >                         if(horz){
> > 
> >                             n<-sum(2^x1)
> > 
> >                           }
> > 
> > 
> > 
> >                         else {
> > 
> > 
> > 
> >                             n<-sum(2^y1)
> > 
> > 
> > 
> >                             }
> > 
> > 
> > 
> > 
> > 
> >                   grid.text(label = paste("n=",round(n,0), sep=""),
> > 
> >                               x = unit(0.5, "char"),
> > 
> >                               y = unit(0.975, "npc"),
> > 
> >                               just = c("left", "top"),
> > 
> >                               gp = gpar(cex = 0.8),
> > 
> >                               vp = current.viewport())
> > 
> > 
> > 
> > 
> > 
> >                  },
> > 
> >          scales = scales,
> > 
> >          layout = layout,
> > 
> >          page = function(n){ annotate(ann) },
> > 
> >          ...)
> > 
> >    )
> > 
> > 
> > 
> > 
> > 
> > On a generated plot  I observed that what ever records have counts as
> > "NA", those are not suppressed. but I don't want them to be
> suppressed.
> > 
> > How can I get exact plot having 30 tick labels for each panel.
> > 
> > 
> > 
> > Have a look at the generated plot.
> > 
> > 
> > 
> > 
> > 
> > Please help me.
> > 
> > 
> > 
> > Thanks
> > 
> > K.Ravichandra
> > 
> > 
> > 
> > 
> > 
> > 
> >    [[alternative HTML version deleted]]
> > 
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list