GGPLOT2: The distance of the discrete values ​​from each end of the x axis

I worked with the code below. Everything seems to work well, except that the discrete values ​​along the x axis are far from each end of the graph. I tried several things, including changing discrete values ​​and playing with restrictions, but cannot make it work. I tested this on simulated data and did not have the same problem, so I'm guessing how I process the data. I would appreciate pointers on how to properly adjust and / or process the data so that it does not occur. An imported data file 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 is:

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
2

, :

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

, .

+1

, vicryl.long $pre_post , , , ( ) .

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

Source: https://habr.com/ru/post/1610475/


All Articles