Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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(ggplot)-如何基于data.frame生成多个图(面),其中只有一个数值变量和不同的类别。变量_R - Fatal编程技术网

R(ggplot)-如何基于data.frame生成多个图(面),其中只有一个数值变量和不同的类别。变量

R(ggplot)-如何基于data.frame生成多个图(面),其中只有一个数值变量和不同的类别。变量,r,R,从这类数据开始: colnames(mydata)您试图强制数据框列,而ggplot2只需要一个列名。正确的语法更经济: ggplot(mydata, aes(y = freq, x = 1:nrow(mydata), colour = bool)) + geom_point() + facet_wrap(~ digit) + labs(x = "1, 2, ..., n") 另外,我无法检查您堆叠/可视化数据的方式,我不知道这是从何而来,也不知道它代表了什么。您能否分享您的

从这类数据开始:


colnames(mydata)您试图强制数据框列,而
ggplot2
只需要一个列名。正确的语法更经济:

ggplot(mydata, aes(y = freq, x = 1:nrow(mydata), colour = bool)) + 
  geom_point() + 
  facet_wrap(~ digit) +
  labs(x = "1, 2, ..., n")


另外,我无法检查您堆叠/可视化数据的方式,我不知道这是从何而来,也不知道它代表了什么。

您能否分享您的数据作为
dput(mydata)
的结果?检查变量类型后,可以更容易地更正代码。首先,不要在
aes()中使用
$
。在
ggplot(mydata,…)
中指定
mydata
的全部要点是,您随后不会将列引用为
mydata$freq
,而只引用
freq
ggplot(mydata, aes(y = mydata$freq, x = seq(1, length(mydata$freq)))) + 
    geom_point() + 
    facet_wrap(~ mydata$digit)
dput(mydata)
structure(list(digit = c(1.1, 1.1, 1.1, 1.1, 1.1, 1.2, 1.2, 1.2, 
1.2, 1.2, 1.3, 1.3, 1.3, 1.3, 1.3, 1.3, 1.4, 1.4, 1.4, 1.4, 1.4, 
2.1, 2.1, 2.1, 2.1, 2.1, 2.1), freq = c(1L, 2L, 20L, 3L, 7L, 
8L, 25L, 10L, 60L, 28L, 7L, 0L, 56L, 12L, 7L, 4L, 3L, 87L, 25L, 
56L, 167L, 46L, 25L, 75L, 20L, 12L, 15L), bool = structure(c(1L, 
1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L), .Label = c("false", 
"true"), class = "factor")), .Names = c("digit", "freq", "bool"
), class = "data.frame", row.names = c(NA, -27L))
ggplot(mydata, aes(y = freq, x = 1:nrow(mydata), colour = bool)) + 
  geom_point() + 
  facet_wrap(~ digit) +
  labs(x = "1, 2, ..., n")