为什么我的图形是空的?

时间:2011-11-17 13:43:08

标签: r data-structures ggplot2

我的结构有问题。 这是我正在做的事情的例子。

x <- c(211.50, 200.50, 148.60, 144.20, 132.20, 159.80, 107.70,  91.40,  63.10,  62.10,  55.70,  74.60, 224.90, 208.001, 45.80, 133.50, 122.70, 161.70, 160.00, 136.80,  92.20,  91.20, 79.20, 109.90, 244.60, 212.20, 147.20, 129.30, 118.50, 165.80, 120.60,  97.90,  69.30,  65.50,  59.10,  81.90,  94.15, 114.20, 131.03, 133.89, 132.25, 153.51)

y <- x
Ref <- c(rep("ref1",36), rep("ref2",6))
ID <- c(rep("id1",6), rep("id2",6),rep("id3",6),rep("id4",6),rep("id5",6),rep("id6",6),rep("id7",6))

data.split <- data.frame(Ref,ID,x,y)
l.ref <- ddply(data.split, .(Ref), "nrow")
vec1 <- c(rep(1,l.ref$nrow[1]))
for (i in 2:length(l.ref$Ref)) { 
  vec2 <- c(rep(i,l.ref$nrow[i]))
  vec3 <- append(vec1,vec2, after =length(vec1))
  vec1 <- vec3
 }
vec_ref <- vec3

l.id <- ddply(data.split, .(ID), "nrow")
vec1 <- c(rep(1,l.id$nrow[1]))
for (i in 2:length(l.id$ID)) { 
 vec2 <- c(rep(i,l.id$nrow[i]))
 vec3 <- append(vec1,vec2, after =length(vec1))
 vec1 <- vec3
}
vec_id <- vec3
df <- structure(list(Ref = structure(vec_ref, .Label = l.ref$Ref, class = "factor"), 
Id = structure(vec_id, .Label = l.id$ID, class = "factor"),
x = data.split$x, y = data.split$y), 
.Names = c("Ref", "Id", "x", "y"), 
 row.names = c(NA, -length(data.split$x)), class = "data.frame")

ggplot(data = df, aes(x = x, y = y, colour = df$Ref)) + 
    geom_point(aes(shape = df$Id)) + scale_shape_manual(value=1:length(l.id$ID))

警告讯息: 1:在[<-.factor*tmp*,is.na(值),值=“NA”):   无效因子级别,生成的NA 2:删除了包含缺失值的42行(geom_point)。

我有空图,我不明白是什么问题?

我做错了什么?

1 个答案:

答案 0 :(得分:5)

你在数据框中弄乱了你的因素。什么是“结构”业务?失去它,并使用它:

df = data.frame(Ref = factor(vec_ref,labels=l.ref$Ref),
        Id = factor(vec_id,labels=l.id$ID),
         x=data.split$x,y=data.split$y)

然后ggplot工作。