I've made a barplot that has several bars. I'd like the bars to be colored according to the type of category they are in. Is there a way to put a conditional into the color parameter in barplot? i.e. if data$category[i] == 1, color the bar red Do I need to generate the coloring sequence before the plot code first? Thanks!! -- View this message in context: http://r.789695.n4.nabble.com/Color-barplots-with-a-conditional-tp3813990p3813990.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Sep-15 00:40 UTC
[R] Color barplots with a conditional?
You've almost got it. If your category variables are factors, then adding col=data$category to plot() will do it automatically since factors will be coerced to their integer representation. If you want a different color map, you can do arithmetic transformations on the integer codings directly as well. Let me know if I can clarify this further. Michael Weylandt On Sep 14, 2011, at 4:14 PM, Allie818 <alice.ly at gmail.com> wrote:> I've made a barplot that has several bars. I'd like the bars to be colored > according to the type of category they are in. Is there a way to put a > conditional into the color parameter in barplot? > i.e. if data$category[i] == 1, color the bar red > > Do I need to generate the coloring sequence before the plot code first? > > Thanks!! > > > -- > View this message in context: http://r.789695.n4.nabble.com/Color-barplots-with-a-conditional-tp3813990p3813990.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Hello, There are some specific examples included in the help of barplot() that answer your question. Regards, Carlos Ortega www.qualityexcellence.es On Wed, Sep 14, 2011 at 10:14 PM, Allie818 <alice.ly@gmail.com> wrote:> I've made a barplot that has several bars. I'd like the bars to be colored > according to the type of category they are in. Is there a way to put a > conditional into the color parameter in barplot? > i.e. if data$category[i] == 1, color the bar red > > Do I need to generate the coloring sequence before the plot code first? > > Thanks!! > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Color-barplots-with-a-conditional-tp3813990p3813990.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]