0

I am having a problem with a plotting function in R. Here is what i got so far.

countries <- c("CHINA ", "UNITED STATES", "UNITED KINGDOM", "GERMANY")
KeyItems <- c("Pretax Income", "Total Liabilities", "Total Assets")
datA <- mean_values[mean_values$Country %in% countries & mean_values$KeyItem %in% KeyItems, ]
datA$KeyItem <- factor(datA$KeyItem, levels = KeyItems, order = TRUE)
p <- xyplot(mn_value ~ Year | KeyItem, datA, groups = datA$Country[, drop = TRUE],
            auto.key = list(space = "right"), par.settings = simpleTheme(pch = 1:5),
            type = c("p", "l"), as.table = TRUE)
print(p)

my dataframe looks like this:

    KeyItem         Year     Country                                      mn_value
172 Pretax Income   1980    SWITZERLAND                                 2.091623e+08
173 Pretax Income   1980    IRELAND                                     3.597619e+07
174 Pretax Income   1980    GERMANY                                     2.301015e+07
175 Pretax Income   1980    SWEDEN                                      4.980680e+07

It returns this error:

Error in dat$Year == Year : 'Year' is missing

I have hardly any experience in R. I just can't find a fix for my Problem. Thank you in advance.

BenBarnes
  • 19,114
  • 6
  • 56
  • 74
MarMarko
  • 35
  • 2
  • 3
  • 9
  • 3
    I think you need to spend some more time creating an example, because your example code simply doesn't run at all (well before you get to the plotting command). I would pay particular attention to what, exactly `datA` is. – joran Nov 03 '12 at 14:43
  • 2
    Reproducible example please! When I try your code, I get **no error** - perhaps you are omitting a line in your code (there's no mention of `Year` other than in your `xyplot` call, and the error message doesn't really match up with your code). Can you isolate which line of code produces the error? – mathematical.coffee Nov 03 '12 at 14:47

1 Answers1

1

As others have mentioned, you code is obviously incomplete, and it appears you are trying to construct mean values within categories in a manner that is incorrect. So here is a worked example leading up to an xyplot that in some (but not all) ways resembles your problem:

Values <- rpois(100*4*3, 200)
datA=data.frame(Values=Values, countries=countries, KeyItems=KeyItems)
datAaggr <- with( datA, aggregate(Values , list(KeyItems, countries) , FUN=mean)) 
# At this point you could rename the Group variables,
#    or you could have done that in the aggregate call with:
datAaggr <- with( datA, aggregate(Values , list(KeyItems=KeyItems, countries=countries) , FUN=mean)) 
# This then succeeds using the aggregated dataframe with the re-named mean values as input:

p <- xyplot(x ~ countries | KeyItems, data=datAaggr, 
             auto.key = list(space = "right"), par.settings = simpleTheme(pch = 1:5),
             type = c("p", "l"), as.table = TRUE)
print(p)

It's going to need some further work with the labels but that can probably be deferred until you have learned how to do the data transformation using data.frames which is a learning task that precedes the plotting task. The Lattice plotting system is critically dependent on having the right data.frame input.

IRTFM
  • 258,963
  • 21
  • 364
  • 487