0

I have a plot produced with the following code:

plot <- ggplot(lmeans, aes(x=Day, y=value*100, group=variable, colour=variable)) +
  geom_point(aes(shape=variable), size=4) +
  geom_line(aes(linetype=variable), size=1.5) +
  ggtitle(paste("Nausea and Vomitting Frequencies by Day for", group_name)) +
  ylab("Frequency (%)") +
  ylim(0, 40) +
  theme(legend.title=element_blank()) +
  theme(legend.justification = c(1, 1), legend.position = c(1, 1))

Which results in a plot like so:

However I would like the days to be discretely labeled rather than being given as a continuous axis. When I try to achieve this by adding scale_x_discrete(), I get the following result:

In which the 'margins' on the x-axis are altered in an unsightful manner. How can I avoid these unsightly changes?

Here's a minimal example for reproduction:

require(ggplot2)

lmeans <- data.frame(Day=c(0,1,2,3,0,1,2,3),
                     variable=c("x","x","x","x","y","y","y","y"),
                     value=c(5,4,2,1,7,3,2,0))

plot <- ggplot(lmeans, aes(x=Day, y=value, group=variable, colour=variable)) +
  geom_point(aes(shape=variable)) +
  geom_line(aes(linetype=variable)) +
  ylim(0, 10) +
  scale_x_discrete() +
  theme(legend.justification = c(1, 1), legend.position = c(1, 1))

print(plot)

Which produces this:

thomasfedb
  • 5,990
  • 2
  • 37
  • 65
  • What do you your labels to be? Dates? Could you show us an example of what you want? – Pop Sep 09 '14 at 07:10
  • Strange behaviour, see for example [this answer of me](http://stackoverflow.com/questions/25730912/creating-bar-graphs-from-multiple-columns/25732407#25732407) were the plot has normal margins. Could you include some example data? – Jaap Sep 09 '14 at 07:11
  • Yes, please make this example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick Sep 09 '14 at 07:17
  • I have added an example that should be able to be reproduced. – thomasfedb Sep 09 '14 at 07:29

3 Answers3

4

Without scale_x_discrete and setting Day as a factor, the plot is looking OK:

ggplot(lmeans, aes(x=factor(Day), y=value, group=variable, colour=variable)) +
  geom_point(aes(shape=variable), size=4) +
  geom_line(aes(linetype=variable), size=1.5) +
  theme(legend.justification = c(1, 1), legend.position = c(1, 1))

which gives: enter image description here

When you use scale_x_discrete, you can include the expand parameter in order to set the margins. An example:

ggplot(lmeans, aes(x=factor(Day), y=value, group=variable, colour=variable)) +
  geom_point(aes(shape=variable), size=4) +
  geom_line(aes(linetype=variable), size=1.5) +
  ylim(0, 10) +
  scale_x_discrete("Day", expand=c(0.05,0.1), breaks=c(0,1,2,3)) +
  theme(legend.justification = c(1, 1), legend.position = c(1, 1))

which gives: enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193
1

I changed scale_x_discrete() to scale_x_continuous() and used limits. Does this work for you?

ggplot(lmeans, aes(x=Day, y=value, group=variable, colour=variable)) +
geom_point(aes(shape=variable)) +
geom_line(aes(linetype=variable)) +
ylim(0, 10) +
scale_x_continuous(limits = c(-0.5, 4)) +
theme(legend.justification = c(1, 1), legend.position = c(1, 1))

enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76
1

Use factor(Day) in the aesthetic mapping:

plot <- ggplot(lmeans, aes(x=factor(Day), y=value,
               group=variable, colour=variable)) +
  geom_point(aes(shape=variable)) +
  geom_line(aes(linetype=variable)) +
  ylim(0, 10) +
  labs(x="Day") +
  theme(legend.justification=c(1, 1), legend.position=c(1, 1))

print(plot)

plot

rcs
  • 67,191
  • 22
  • 172
  • 153