Data.table没有返回正确的图 [英] Data.table not returning correct plots

查看:83
本文介绍了Data.table没有返回正确的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始学习data.table包并想尝试绘图。

  library(data.table)
DT<-data.table(iris)

DT [,plot(Sepal.Length,Sepal.Width,main = Species),by = Species]

与返回的结果不同

  DT1<-DT [Species == setosa] 
DT1 [,plot(Sepal.Length,Sepal.Width,main = Species)]

第二个显示所有观察结果,而第一个不显示所有观察结果。



是否有一个我还没有学到的完美逻辑解释?还是我的图形有问题?

解决方案



弗兰克



请注意 0 必须添加到两个变量中。



编辑(如果使用非数字数据(因此添加 0 是不可行的),弗兰克 建议使用另一种方法按值传递数据

  DT [,with(copy(.SD),plot(Sepal.Length,Sepal.Width,main = Species)),by = Species] 


Starting to learn data.table package and wanted to try plotting.

library(data.table)
DT <- data.table(iris)

DT[, plot(Sepal.Length, Sepal.Width, main = Species), by = Species]

Does not return same as for example

DT1 <- DT[Species == "setosa"]
DT1[, plot(Sepal.Length, Sepal.Width, main = Species)]

The second one shows all observations, while the first one does not.

Is there a perfect logical explanation that I have not learned yet? Or something wrong with my graphics?

解决方案

The problem and a workaround have been reported in Saving plots in a data.table list column.

It seem that the issue is related to base plot() as ggplot2 is working as expected, e.g.,

library(data.table)
DT[, print(ggplot(.SD, aes(Sepal.Length, Sepal.Width, color = Species)) +
             geom_point()), by = Species]

creates a series of three plots which are displayed in RStudio's Plots pane, for instance. The first one looks like:

However, with base plot()

par(mfrow=c(1,3))
DT[, plot(Sepal.Length, Sepal.Width, main = Species), by = Species]

some data points have been clipped off:

As mentioned by Frank in his comment one workaround is to make sure the variable is passed by value, e.g, by adding 0. So, this workaround will create a base plots with all data points included:

par(mfrow=c(1,3))
DT[, plot(Sepal.Length + 0, Sepal.Width + 0, main = Species), by = Species]

Note that 0 has to be added to both variables.

EDIT In case of working with non-numeric data (so adding 0 isn't viable), Frank suggested to use another approach to pass the data by value:

DT[, with(copy(.SD), plot(Sepal.Length, Sepal.Width, main = Species)), by = Species]

这篇关于Data.table没有返回正确的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆