[R] barplot with more than 1 variable

ilai keren at math.montana.edu
Sun Feb 19 20:26:54 CET 2012


You could have found the solution in
http://had.co.nz/ggplot2/geom_bar.html yourself since all help pages
for ggplot refer you to the web site. But to speed things up for you,
try this for starters:

TUSE2 <- data.frame(country = rep(c("United
States","Italy","Germany","Netherlands"),each=2),
year3 = factor(1:2),
mw = c( 245.8, 255.9,  248.5, 207.4,263.9, 197.7, 174.2, 189.5),
st = c( 200.5, 218.0,  236.1, 237.3, 220.5, 242.7, 221.0, 206.0),
all= c( 446.3, 473.9,  484.6, 444.7,484.5, 440.4, 395.2, 395.5))
# Note year3 is a factor

library(ggplot2)
forplot <- stack(TUSE2,c('st','mw','all'))
ggplot(TUSE2) +
geom_bar(aes(year3,forplot$values,fill=forplot$ind),position="dodge")+
facet_grid(. ~ country)

Hope this helps
Elai

On Sun, Feb 19, 2012 at 5:31 AM, Francesco Sarracino
<f.sarracino at gmail.com> wrote:
> Dear R listers,
> I am trying to produce a simple (for a stata user) barplot with 4
> countries on the x axis, each country observed in 2 subsequent years
> and 3 variables.
> Basically, I should have three bars for each year for each country. I
> am attaching the chart I made in Stata, but I am not sure you'll
> manage to see it!
>
> I did the following:
> #here I create the data-set TUSE2. The vectors mw, st and all are the
> three variables I'd like to plot in each of the two years of the
> variable year3 for each country.
>
> country <- c("United States","United
> States","Italy","Italy","Germany","Germany","Netherlands","Netherlands")
> year3 <- c(1, 2, 1, 2, 1, 2,1 , 2)
> mw <- c( 245.8, 255.9,  248.5, 207.4,263.9, 197.7, 174.2, 189.5)
> st <- c( 200.5, 218.0,  236.1, 237.3, 220.5, 242.7, 221.0, 206.0)
> all <- c( 446.3, 473.9,  484.6, 444.7,484.5, 440.4, 395.2, 395.5)
> TUSE2 <- data.frame(country, year3, mw, st, all)
> attach(TUSE2)
>
> #I load the library ggplot (But if you know of alternative techniques,
> I'll be happy to try them out)
> library(ggplot2)
>
> #I start producing the chart:
> p <- ggplot(TUSE2, aes(country, all, group=year3))
> p <- p + layer(
>  geom="bar",
>  position="dodge",
>  stat="identity")
> #so far I manage to get what I want for just one variable: all. Let's
> try to add another variable, let's say: mw
>
> f <- p + layer(
>  geom="bar",
>  aes(x=country, y = mw),
>  position="dodge",
>  stat="identity")
> f
> #and here my adventure crashes. I get the following message and I
> can't figure out how to get my chart. The error message is:
> Error in pmin(y, 0) : object 'y' not found
> In addition: Warning message:
> In min(diff(sort(x))) : no non-missing arguments to min; returning Inf
>
> Can you please help me understanding how to make my chart? I am trying
> to switch from Stata to R, but so far life is really hard!
> Best,
> f.
>
> ______________________________________________
> 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