1

I'm trying to find a way to force the x-axis to be at y=0 instead of slightly below it. I've looked around for answers and have found there are solutions for plot in base R, but haven't found a way to do this in ggplot2. Apologies for the simple example and that I can't post the resulting image as I don't have enough reputation. All help appreciated!

EDIT1 (incorrect, corrected by @joran): In addition, if I were to have negative values I'd want them to be displayed instead of being cut off. The current linked solution would cut off negative values. I've also changed my example data to have a negative number to illustrate my point.

EDIT2: I ran the code in the linked example with '-1:5's instead of '1:5's and see that the limits expand appropriately. However, the x-axis labels are still at the bottom of the chart along the new minimum, y=-1, instead of being fixed along y=0. Sorry for the poor description. Link to image of Excel example.

Code Example:

library(ggplot2)

test_df <- data.frame(dates = as.Date(c('2015-01-01', '2015-01-02', '2015-01-03', '2015-01-04', '2015-01-05', '2015-01-06', '2015-01-07', '2015-01-08', '2015-01-09', '2015-01-10', '2015-01-11', '2015-01-12', '2015-01-13', '2015-01-14', '2015-01-15')), numbers = c(13,12,11,10,9,8,8,7,6,5,4,3,2,-1,0))

test_plot <- ggplot(test_df) +
  geom_line(aes(x = dates,
               y = numbers),
           size = 1)
  • You need `scale_y_continuous(expand = c(0, 0))` – zx8754 Apr 29 '15 at 18:00
  • Another way would be to use `test_plot+coord_cartesian(ylim=c(0,max(test_df$numbers)))` – ccapizzano Apr 29 '15 at 18:02
  • See my edit y'all. I think both of those solutions would cut off any negative numbers that may be in my data (forgot to mention this may happen). I'd link a picture of what I _want_ the chart to look like using Excel but don't have image posting privileges yet. Thanks for your help! – SubgamePerfect Apr 29 '15 at 18:22
  • When people point you to a duplicate, you should read it completely and actually try the solution there before you claim it isn't a duplicate. The `scale_y_continuous(...,expand = c(0,0))` solution there will absolutely _not_ cutoff any values, regardless of whether they are negative or not. – joran Apr 29 '15 at 18:32
  • Good call. I definitely called out the wrong thing. When I tried the linked example with '-1:5's instead of '1:5's it didn't cut off the negative values as you mentioned. However, it places the x-axis now along the new minimum, which is y=-1. Is there a way to have the tick marks and labels of the x-axis appear along the y=0 line instead of at the bottom when there are negative values? – SubgamePerfect Apr 29 '15 at 18:46
  • I presume you mean something like [this](http://stackoverflow.com/q/17753101/324364)? I do not know of any easy way to do that style of graph in ggplot (and the age of that Q and a lack of an answer reinforces that impression for me...). Base graphics would handle it, I'm sure, though. – joran Apr 29 '15 at 19:04
  • `with(test_df, plot(numbers ~ dates, xaxt = "n", bty = "n", type = "line")); axis.Date(x = test_df$dates, pos = 0, side = 1)` – dayne Apr 29 '15 at 19:19
  • @joran With the clarification of this question, the duplicate question is not really the same. – dayne Apr 29 '15 at 19:29
  • @dayne No, it's just that they have migrated to a different, new question. How to have the axis start at zero is answered at the duplicated. The question of moving the axis into the graph is a separate, new question that should probably be asked separately. (Although, as I pointed out, for ggplot there is still probably a duplicate suggesting it is not (easily) possible. If they allowed base solutions in their new question, as you showed it is clearly possible.) – joran Apr 29 '15 at 19:31
  • @joran Thanks for all your help! Looks like the consensus is that the general form of the question I meant to ask is 'Is there an option to fix an axis and its labels on the chart area using the ggplot2 package?' and the answer is 'It's easily doable in base R, however not easily done using the ggplot2 package'. As this is my first question on Stack Overflow, what's the best course forward that will help others? Edit the question to be more clear? Delete? Thanks again! – SubgamePerfect Apr 29 '15 at 20:40

0 Answers0