'How to adjust the ratio of my x axis in my line graph in r?

I am creating a line graph in R using ggplot2. So far the graph is looking fine (I am relatively new to all this), but I have run into an issue trying to adjust my x axis to define the trend a bit better - scale the ratio down slightly. Whenever I use coord_fixed - messing about with it - the scale messes up for the whole graph. What is your advice on using coord_fixed in this situation?

the graph currently

this is the code

ggplot(Met_IOPC, aes(x= Year, y= Total, group = 1))+
  geom_line(color="darkblue")+
  geom_point()+
  scale_y_continuous(expand = c(0,0), limits = c(0,1200))+


Solution 1:[1]

My understanding is that you want to adjust your plot so that the trend line appears steeper.

I think that, due to the difference in range of the x and y axis, you are struggling to find the correct ratio in coord_fixed. However, this is only one method to achieve your aim.

If we reproduce your plot:

library(ggplot2)

Met_IOPC <- data.frame(Total = c(740, 840, 890, 940, 1040),
                       Year = as.character(2018:2022))

p <- ggplot(Met_IOPC, aes(x= Year, y= Total, group = 1)) +
  geom_line(color="darkblue") +
  geom_point() +
  scale_y_continuous(expand = c(0,0), limits = c(0, 1200))

p

enter image description here

The simplest way to change the steepness of the line is to drag the plotting window to make it narrower:

enter image description here

If you wish the plot to retain the same image dimensions, we can either set a very small ratio in coord_fixed

p + coord_fixed(ratio = 0.008)

enter image description here

Or adjust the aspect ratio:

p + theme(aspect.ratio = 2)

enter image description here

Or adjust the y axis scale:

p + coord_cartesian(ylim = c(700, 1100))

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Allan Cameron