Reputation: 35
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.
Upvotes: 0
Views: 233
Reputation: 263362
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.
Upvotes: 1