GGPLOT2:x轴各端离散值的距离 [英] GGPLOT2: Distance of discrete values of from each end of x-axis

查看:252
本文介绍了GGPLOT2:x轴各端离散值的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在处理下面的代码。似乎所有东西都可以工作
,除了x轴上的离散值远离图的每个
端。我已经尝试了几件事情,包括更改
离散值并玩限制,但无法使其工作。
我在模拟数据上测试了这个,并没有同样的问题,所以我猜测它是我处理数据的方式。我会很感激任何
指针如何调整和/或正确处理数据,因此
不会发生。导入的数据文件是
continuous,discrete和string变量的组合。

I have been working with the code below. Everything seems to work okay, except that the discrete values on the x-axis are far from each end of the graph. I've tried several things including changing the discrete values and playing with the limits, but can't get it to work. I tested this on simulated data and do not have the same problem, so I guessing it is how I'm processing the data. I'd appreciate any pointers on how to adjust this and/or process the data correctly so it does not occur. The data file being imported is a combination of continuous, discrete and string variables.

我使用的数据:

The data I use:

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

我的代码:

And my code:

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()


推荐答案

您可以添加一个因子:

With a factor you can add:

scale_x_discrete(expand=c(0, 0))

一直到边缘:

to plot all the way to the edges:

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

但我不确定你的'重新尝试没有一些示例数据。

but I'm not sure exactly what you're trying for without some sample data.

这篇关于GGPLOT2:x轴各端离散值的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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