[R] barplot with more than 1 variable
Francesco Sarracino
f.sarracino at gmail.com
Sun Feb 19 13:31:23 CET 2012
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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cfr_HRS.png
Type: image/png
Size: 51704 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120219/ca145917/attachment.png>
More information about the R-help
mailing list