【问题标题】:GGPLOT2: Distance of discrete values of from each end of x-axisGGPLOT2:离x轴每一端的离散值的距离
【发布时间】:2012-02-04 16:46:42
【问题描述】:

我一直在使用下面的代码。一切似乎都有效 好的,除了 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()

【问题讨论】:

  • 能否给我们提供一个示例数据框,或者输出summary(vicryl.long)的结果以便我们自己生成?
  • 我添加了数据以供将来参考。请让我知道是否有更好的方式来展示它,因为我是 SO 新手。

标签: r graphics plot ggplot2 linegraph


【解决方案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

但如果没有一些示例数据,我不确定您到底要尝试什么。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      相关资源
      最近更新 更多