0

I am trying to set the axis limits on a geom_line() plot made with R/ggplot2.

Below I report the simple code:

Graph <- ggplot(data=Test,aes(x=Enc1,y=N,group=Trial,colour=Trial)) + 
geom_line(size = 1) + 
coord_cartesian(xlim = c(0, 100), ylim = c(0, 7)) +
scale_color_manual(values=c('#f92410','#644196')) +
ggtitle("Valutazione peel su provino 50 mm") +  
theme_bw() + 
theme(plot.title = element_text(hjust = 0.5)) +
xlab('Allungamento (mm)') + 
ylab('Forza (N)')
Graph

My question is why the axis don't start from the origin but a little below? I would like to see the line starting directly from the bottom/left corner of the graph, but it doesn't.

enter image description here

Is there any way to adjust this distance? If you like I can also upload the original spreadsheet data file, but I do not know how to do.

Thank you in advance for every reply.

GiacomoDB
  • 369
  • 1
  • 10

1 Answers1

2

Try adding the parameter expand to the scale_x_discrete or scale_x_continuos (not sure what your x variable is).

library(ggplot2)

#generic, reproducible data
df <- data.frame(x = 1:100, 
                y = runif(100))

ggplot(df, aes(x = x))+
    geom_line(aes(y = y))+
    scale_x_discrete(expand = c(0,0)) #control axis parameters
Pedro Alencar
  • 1,049
  • 7
  • 20
  • It works partially. Thank you for this help. I added also for the y-axis the same: `scale_x_discrete(expand = c(0,0))` and I got the graph starting from the origin as needed but now the problem is that the numbers ofthe axis are lost. – GiacomoDB Jul 06 '21 at 16:04
  • I solved changing from `scale_x_discrete(expand = c(0,0))` to `scale_x_continuous(expand = c(0,0))` – GiacomoDB Jul 06 '21 at 16:12