1

My graph looks like this:

graph

The code I use is this

graph_substrat <- ggplot(substrat_long, aes(x=substrat,y=value, 
+ group=variable, linetype=variable)) + geom_line() + ylab("Suitability Index") 
+ xlab("Substrat") + background_grid(major = "xy", minor = "none")

graph_substrat <- graph_substrat + theme(legend.position="none") 

I only want to plot the line between Cobble and Sand, and delete the free space in the beginning and end of the graph, so that the lines start at the Y-axis (or at least very close to it, and ends with the the last tick on the X-axis).

I tried it with limits, expand and coord_cartesian, but it did not work.

Note that X is of type factor and not numeric.

iled
  • 2,142
  • 3
  • 31
  • 43
Konstantin_o
  • 125
  • 1
  • 6

1 Answers1

1

Include expand = c(0,0) in scale_x_discrete as described here.

Compare this plot:

library(ggplot2)
ggplot(iris, aes(x = Species, y = Petal.Length)) +
  stat_summary(fun.y = "mean", geom = "bar")

enter image description here

To the one with expand = c(0,0):

ggplot(iris, aes(x = Species, y = Petal.Length)) +
  stat_summary(fun.y = "mean", geom = "bar") +
  scale_x_discrete(expand = c(0,0))

enter image description here

Community
  • 1
  • 1
  • Thanks! I had tried to solve it with `expand=c(0,0)` but I did it for the wrong aesthetics. `scale_x_discrete (expand=c(0,0))` does the trick! – Konstantin_o Apr 17 '17 at 07:16