Reputation: 151
ggplot2 i am trying plot the scatter plot but and merging with facet grid some values are looking incomplete near y axis points how to fix this anyone help me.
dummy <- data.frame(Value = c(rnorm(100, mean = 35, sd = 2),
rnorm(100, mean = 47, sd = 2),
rnorm(100, mean = 28, sd = 1)),
Method = c(rep("base", times = 100),
rep("new", times = 100),
rep("edge", times = 100)),
Subject = rep(paste0("M", seq_len(100)), times = 3))
library("ggplot2")
ggplot(dummy, aes(y=Value, x=Subject)) +
geom_point(aes(shape=Method),size = 2.5) +
facet_wrap(~ Method)
Upvotes: 2
Views: 613
Reputation: 174468
Although using the expand
parameter would be the textbook answer, the sanest way to do this with the given data is to make Subject
a numeric variable instead of a factor. If you don't do this, your x axis labels will be hopelessly overlapped, and your underlying panel grid will look weird.
Compare Subject
as numeric:
ggplot(dummy, aes(y = Value, x = as.numeric(gsub("M", "", Subject)))) +
geom_point(aes(shape=Method),size = 2.5) +
facet_wrap(~ Method) +
labs(x = "Subject")
Versus as a character with expand
:
ggplot(dummy, aes(y = Value, x = Subject)) +
geom_point(aes(shape = Method),size = 2.5) +
facet_wrap(~ Method) +
scale_x_discrete(expand = c(0.1, 0.1))
I think it's pretty clear that the numeric version is easier to read.
Upvotes: 2
Reputation: 4344
You can increase the margin of the X axis with ggplot2::scale_x_discrete(), just experiment with the "add = 5" until you have what you need
library("ggplot2")
ggplot2::ggplot(dummy, aes(y=Value, x=Subject)) +
ggplot2::geom_point(aes(shape=Method),size = 2.5) +
ggplot2::scale_x_discrete(expand = expand_scale(add = 5)) +
ggplot2::facet_grid(~ Method, scales = "free")
Upvotes: 2