2012-02-04 136 views
1

我一直在使用下面的代码。似乎一切似乎 没问题,除了X轴上的离散值远离图的每个末端 。我已经尝试了几件事情,包括更改离散值和玩极限,但无法使其工作。 我对模拟数据进行了测试,并没有相同的问题,所以我猜测它是我如何处理数据。我会很感激任何 关于如何调整这个和/或正确处理数据的指针,所以它不会发生 。导入的数据文件是 连续,离散和字符串变量的组合。GGPLOT2:x轴各端离散值的距离

的数据我用:

id_finger sex pre_post angle 
1 F 0 7 
1 F 2 5 
2 F 0 11 
2 F 2 1 
3 F 0 21 
3 F 2 7 
4 M 0 12 
4 M 2 1 
5 F 0 11 
5 F 2 4 
6 M 0 18 
6 M 2 8 
7 M 0 28 
7 M 2 9 
8 F 0 10 
8 F 2 2 
9 M 0 12 
9 M 2 5 
10 F 0 14 
10 F 2 0 
11 M 0 27 
11 M 2 5 
12 M 0 15 
12 M 2 3 
13 F 0 19 
13 F 2 0 
14 M 0 5 
14 M 2 4 
15 M 0 24 

而且我的代码:

vicryl.wide <- read.table("C:/vicryl2.csv", 
        header=TRUE, sep=",", na.strings=" ") 

library(reshape2) 
vicryl.long <- melt(vicryl.wide, 
       id.vars=c("id_finger","sex"), 
       measure.vars=c("pre_angle_r", "post_angle_r"), 
       variable.name="pre_post") 

names(vicryl.long)[names(vicryl.long)=="value"] <- "angle" 

levels(vicryl.long$pre_post)[levels(vicryl.long$pre_post)=="pre_angle_r"] <- 0 
levels(vicryl.long$pre_post)[levels(vicryl.long$pre_post)=="post_angle_r"] <- 2 

vicryl.long <- vicryl.long[ order(vicryl.long$id_finger, 
vicryl.long$pre_post), ] 

library(ggplot2) 
ggplot(data=vicryl.long, aes(x=pre_post, y=angle, group=id_finger)) + 
geom_line() 
+1

你能否给我们提供一个样本数据框,或输出总结结果(vicryl.long),以便我们自己生成它? – 2012-02-04 16:57:53

+0

我添加了数据以供将来参考。请让我知道是否有更好的方式来呈现它,因为我是新手。 – Tom 2012-02-04 21:39:32

回答

1

有了一个因素,你可以添加:

scale_x_discrete(expand=c(0, 0)) 

一路密谋边缘:

df <- data.frame(x=factor(letters[1:10]), y=rnorm(100), group=rep(letters[20:24], each=20)) 

p <- ggplot(df, aes(x=x, y=y, colour=group)) + geom_line() 
c <- scale_x_discrete(expand=c(0, 0) 

p 
p + c 

,但我不知道你想要的到底是什么没有一些样本数据。

0

再试一次,但转换vicryl.long $ pre_post到连续数值变量,而不是一个因素,它会绘制到边缘,而不是中心(因为它应该)作为分类变量。

vicryl.long$pre_post <- as.numeric(as.character(vicryl.long$pre_post))