简单的绘图函数



我在R中的绘图功能有问题。这是我到目前为止得到的。

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)

我的数据帧看起来像这样:

    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

它返回此错误:

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

我几乎没有R中的任何经验。我只是找不到解决问题的方法。预先感谢您。

正如其他人提到的那样,您的代码显然不完整,看来您正在尝试以不正确的方式在类别中构造mean值。因此,这是一个有效的示例,导致xyplot在某些(但不是全部)的方式类似于您的问题:

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)

它将需要与标签进行一些进一步的工作,但是可能会推迟使用,直到您学习了如何使用Data.Frames进行数据转换的方式,这是绘图任务之前的学习任务。晶格绘图系统非常取决于拥有正确的数据。帧输入。

最新更新