[R] GGPLOT2: Distance of discrete values of from each end of x-axis

Thomas Schmitt schmitta1573 at gmail.com
Sat Feb 4 22:49:22 CET 2012


Sorry group didn't realize this got cross-posted on R-help . . .
thought it didn't make it through. Thanks to Justin Haynes for the
solution below and wanted to pass it along.

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

On Sat, Feb 4, 2012 at 11:18 AM, Thomas Schmitt <schmitta1573 at gmail.com> wrote:
> Hi Group,
>
> 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.
>
> Thanks,
>
> Tom
>
> 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()



More information about the R-help mailing list