Victor Gabillon
2011-May-25 12:56 UTC
[R] barplot groups of different size i.e. height is NOT a matrix
Hello, I want to use the function barplot do display several group of bars. A standard example is given at this link http://onertipaday.blogspot.com/2007/05/make-many-barplot-into-one-plot.html But in their example the 4 groups of bars are all composed of 8 bars. I want to be able do display the same kind of graph but where the number of bars in each group are not the same. For example the first group of bars would have 2 bars and the second group of bars would have 10 bars. barplot function has a first parameter named height which is a matrix where each line are the values for the bars of one particular group. One solution could be to have a height matrix with NA values but then the space occupied by each group is equal to the size of the largest group!! So you end up with gaps (empty) where there are NAs. Do you know how to solve this problem? Do i have to consider multiple barplots in the same plot with the same axis? (btw, i don't know how to do that) In fact the bar would represent the performance of an algorithm. A group of bars would be the performance of an algorithms with different parameters. But when comparing different algorithms it is possible that we don't want to display the same number of parameters for each algorithm. Thanks for your help. Victor
ONKELINX, Thierry
2011-May-25 13:35 UTC
[R] barplot groups of different size i.e. height is NOT a matrix
Dear Victor, Here is a basic solutions using ggplot2 library(ggplot2) dataset <- data.frame(Main = c("A", "A", "A", "B", "B"), Detail = c("a", "b", "c", "1", "2"), value = runif(5, min = 0.5, max = 1)) ggplot(dataset, aes(x = Detail, y = value)) + geom_bar() + facet_grid(.~Main, scales = "free_x") Best regards, Thierry> -----Oorspronkelijk bericht----- > Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > Namens Victor Gabillon > Verzonden: woensdag 25 mei 2011 14:56 > Aan: r-help at r-project.org > Onderwerp: [R] barplot groups of different size i.e. height is NOT a matrix > > Hello, > > I want to use the function barplot do display several group of bars. > A standard example is given at this link > http://onertipaday.blogspot.com/2007/05/make-many-barplot-into-one- > plot.html > > But in their example the 4 groups of bars are all composed of 8 bars. > I want to be able do display the same kind of graph but where the number of > bars in each group are not the same. For example the first group of bars would > have 2 bars and the second group of bars would have 10 bars. > > barplot function has a first parameter named height which is a matrix where > each line are the values for the bars of one particular group. > One solution could be to have a height matrix with NA values but then the space > occupied by each group is equal to the size of the largest group!! So you end up > with gaps (empty) where there are NAs. > > Do you know how to solve this problem? > Do i have to consider multiple barplots in the same plot with the same axis? > (btw, i don't know how to do that) > > In fact the bar would represent the performance of an algorithm. > A group of bars would be the performance of an algorithms with different > parameters. > But when comparing different algorithms it is possible that we don't want to > display the same number of parameters for each algorithm. > > Thanks for your help. > Victor > > ______________________________________________ > 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.
Marc Schwartz
2011-May-25 15:04 UTC
[R] barplot groups of different size i.e. height is NOT a matrix
On May 25, 2011, at 7:56 AM, Victor Gabillon wrote:> Hello, > > I want to use the function barplot do display several group of bars. > A standard example is given at this link > http://onertipaday.blogspot.com/2007/05/make-many-barplot-into-one-plot.html > > But in their example the 4 groups of bars are all composed of 8 bars. > I want to be able do display the same kind of graph but where the number of bars in each group are not the same. For example the first group of bars would have 2 bars and the second group of bars would have 10 bars. > > barplot function has a first parameter named height which is a matrix where each line are the values for the bars of one particular group. > One solution could be to have a height matrix with NA values but then the space occupied by each group is equal to the size of the largest group!! So you end up with gaps (empty) where there are NAs. > > Do you know how to solve this problem? > Do i have to consider multiple barplots in the same plot with the same axis? (btw, i don't know how to do that) > > In fact the bar would represent the performance of an algorithm. > A group of bars would be the performance of an algorithms with different parameters. > But when comparing different algorithms it is possible that we don't want to display the same number of parameters for each algorithm. > > Thanks for your help. > Victorbarplot() is fundamentally built upon the use of rect() to construct the bars, so you could always create your own variant to allow for the flexibility that you desire. That being said, if your performance measures (the bar heights) are other than discrete counts or proportions, I would advise you to consider using other visual presentation forms, as these are really the only two types of data for which barplots are generally considered satisfactory. A key to barplots of course is that they are based at 0 for proper visual comparison. Thus, if you need to have the minima of the relevant axis at a value other than 0, this is another reason to not use them. Even then, many folks have moved away from barplots to use point or dot plots and similar formats, especially where you also need to include some type of confidence interval for each measure. HTH, Marc Schwartz