Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
标绘变量是指自变量的每一级。R_R - Fatal编程技术网

标绘变量是指自变量的每一级。R

标绘变量是指自变量的每一级。R,r,R,给定下一个代码和数据帧: require(data.table) require(ggplot2) dat1 <- fread('J S1 S2 S3 S4 Z 1 4 5 3 2 0 1 6 5 6 5 1 2 3 5 8 9 0 2 12 11 34 44 1 3 11

给定下一个代码和数据帧:

require(data.table)
require(ggplot2)

dat1 <- fread('J  S1  S2  S3  S4  Z
              1   4   5   3   2  0
              1   6   5   6   5  1
              2   3   5   8   9  0
              2  12  11  34  44  1
              3  11  23  23  22  0
              3  12  15  22  21  1')

temp <- melt(dat1, id.vars = c("J", "Z"))

ggplot(temp, aes(x = J, y = value, color = variable, shape = as.factor(Z))) +
  geom_point() 


我得到的每一个完整的数据集只有一个点。但我想在同一个图中得到每一级J(1,2,3)的S1平均值,每一级J的S2平均值,每一级J的S3平均值,以及每一级J的S4平均值。

您需要在数据中为平均值添加行

请让我知道这是否有意义,或者你希望有不同的东西

你可以做:

library(data.table)

temp1 <- setDT(temp)[,.(value = mean(value)),by=.(J,variable)]
ggplot(temp1, aes(x = J, y = value, color=factor(variable))) +
  geom_point() 
在OP请求后编辑

要将Z变量考虑在内,您需要汇总数据基础Z,如下所示,然后绘制:

temp1 <- setDT(temp)[,.(value = mean(value)),by=.(J,variable,Z)]

ggplot(temp1, aes(x = variable, y = value, color=factor(J),shape=factor(Z))) +
  geom_point() 

谢谢你的回答。这很有帮助。然而,我试图更好地解释我想在我的情节中得到什么。我不想要三个级别的Z。我想要S1、S2、S3、S4的平均值来表示每个级别的J。谢谢!这和我想要的东西更相似。但是我不知道为什么我在运行temp1@pyring时出错。你是否已经将data.frame转换为data.table,比如setDT(temp),我已经编辑了该部分,你现在必须能够运行它。太好了。我的数据文件要大得多,所以我还想为Z的每个级别表示S1、S2、S3、S4。因此,如果Z有两个级别,0和1,我怎么能在同一个图形中用“形状”(而不是颜色)表示因子Z。@pyring,请参见编辑部分,如果您有更多问题,也请您同时询问所有问题,请另外提问。
ggplot(temp1, aes(x = variable, y = value, color=factor(J))) +
  geom_point() 
temp1 <- setDT(temp)[,.(value = mean(value)),by=.(J,variable,Z)]

ggplot(temp1, aes(x = variable, y = value, color=factor(J),shape=factor(Z))) +
  geom_point() 
ggplot(temp1, aes(x = variable, y = value, color=factor(J),shape=factor(Z))) +
  geom_point() + facet_wrap(~Z)