0

I'm making a graph in ggplot, and it cuts off my data

`ggplot(hhcomp, aes(x=utility, y=consumption))+
      xlim(0,16)+ylim(0,16)+          
      labs(x = "leisure(hours)",y="counsumption(units)")+
                  geom_line(aes(x = leisure, y = consumption,expand=TRUE))+
                  geom_line(aes(x = utilityc, y = consumption))+
                  geom_line(aes(x = leisure1, y = consumption1))+
                  geom_line(aes(x = utilityc1, y = consumption))`

How do I include all the data points, so that my lines go to the edge of my graph?

user801855
  • 15
  • 4
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What's the range of the `utility` and `consumption` variables? – MrFlick Feb 20 '20 at 16:33
  • my max is 16, min is 0. I have a lot of data frames, so not sure how to show that. – user801855 Feb 20 '20 at 16:41
  • Did you try it without the `xlim` and `ylim`? – markhogue Feb 20 '20 at 16:45
  • To include all data points and also have the lines go to the edge of the graph, remove `xlim(0,16)` and add `scale_x_continuous(expand=c(0,0))`. Also, see [here](https://stackoverflow.com/a/32506068/496488) for differences in how `xlim`, `scale_x_continuous` vs. `coord_cartesian` deal with data outside the range of the plot panel. Similar considerations apply to the y-axis range. – eipi10 Feb 20 '20 at 16:45
  • Now, it's showing parts of the graph I don't want to show. I want it to only show values between 0 and 16 on both axis, with lines to the edge. – user801855 Feb 20 '20 at 17:07
  • I tried doing that for x and y, just doing it for x fixes a lot of things, but now the part of the graph I'm interested in is too small – user801855 Feb 20 '20 at 17:13
  • To show only 0-16 on the x axis: `scale_x_continuous(limits=c(0,16), expand=c(0,0))` or `coord_cartesian(xlim=c(0,16)) + scale_x_continuous(expand=c(0,0))`. These will behave the same in your example, but will give different results for any geoms that involve statistical calculations, such as `geom_smooth` or `stat_summary`. – eipi10 Feb 20 '20 at 18:42

1 Answers1

0

I think you want coord_cartesian instead of xlim.

Here's a small example:

library(ggplot2)
set.seed(69)
df <- data.frame(x = 1:10, y = rnorm(10))

If I use xlim, I get a warning that not all my points are plotted, and the lines don't go to the edge of the plot.

ggplot(df, aes(x = x, y = y)) + geom_line() + xlim(c(2, 6))
#> Warning: Removed 5 rows containing missing values (geom_path).

But if I use coord_cartesian, I get no warning and my lines go to the edge of the plot:

ggplot(df, aes(x = x, y = y)) + geom_line() + coord_cartesian(xlim = c(2, 6))

Created on 2020-02-20 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • can coord_cartesian be used to set y limits too? Does it have to go after a line? Do I have to enter all values, like in my data frame? – user801855 Feb 20 '20 at 16:49
  • @user801855 Yes, you can set `ylim` in `coord_cartesian` too. Just put `ylim = ` and `xlim = ` inside coord_cartesian. It works just the same as setting `xlim` and `ylim` except it has the properties you are looking for. The order doesn't matter. – Allan Cameron Feb 20 '20 at 16:54
  • ok, all looks good, except I have one row with indeterminate values, and some lines go weird. Can I exclude just those rows? – user801855 Feb 20 '20 at 17:20
  • @user801855 yes, if for example your indeterminate values were on row 13, then instead of plotting with `ggplot(hhcomp...` you plot with `ggplot(hhcomp[-13,]...` – Allan Cameron Feb 20 '20 at 18:29
  • How do I do that? – user801855 Feb 20 '20 at 18:37