2

Given this:

kc$sqft_living_group <- cut(kc$sqft_living, breaks = c(0, 1000, 2000, 3000, 5000, 7000, 10000, 15000), dig.lab=5)

How do I set the limit of my ggplot2 graph?

Nothing I can find shows the syntax to set the limit for intervals.

kc %>%
  filter(zipcode %in% top_10_zipcodes) %>% 
  group_by(sqft_living_group) %>%
  summarize(Mean_Price = mean(price)) %>%
  ggplot(aes(y = Mean_Price, x = sqft_living_group)) +
    geom_bar(stat = "identity") +
    scale_y_continuous(labels = comma) +
    scale_x_discrete(limits = "(0, 1000], (1000, 12000]") <---------- HERE

structure of data:

'data.frame':   21613 obs. of  22 variables:
 $ id               : num  7.13e+09 6.41e+09 5.63e+09 2.49e+09 1.95e+09 ...
 $ date             : POSIXct, format: "2014-10-13" "2014-12-09" "2015-02-25" "2014-12-09" ...
 $ price            : num  221900 538000 180000 604000 510000 ...
 $ bedrooms         : int  3 3 2 4 3 4 3 3 3 3 ...
 $ bathrooms        : num  1 2.25 1 3 2 4.5 2.25 1.5 1 2.5 ...
 $ sqft_living      : int  1180 2570 770 1960 1680 5420 1715 1060 1780 1890 ...
 $ sqft_lot         : int  5650 7242 10000 5000 8080 101930 6819 9711 7470 6560 ...
 $ floors           : Factor w/ 6 levels "1","1.5","2",..: 1 3 1 1 1 1 3 1 1 3 ...
 $ waterfront       : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
 $ view             : int  0 0 0 0 0 0 0 0 0 0 ...
 $ condition        : int  3 3 3 5 3 3 3 3 3 3 ...
 $ grade            : int  7 7 6 7 8 11 7 7 7 7 ...
 $ sqft_above       : int  1180 2170 770 1050 1680 3890 1715 1060 1050 1890 ...
 $ sqft_basement    : int  0 400 0 910 0 1530 0 0 730 0 ...
 $ yr_built         : int  1955 1951 1933 1965 1987 2001 1995 1963 1960 2003 ...
 $ yr_renovated     : Factor w/ 70 levels "0","1934","1940",..: 1 46 1 1 1 1 1 1 1 1 ...
 $ zipcode          : Factor w/ 70 levels "98001","98002",..: 67 56 17 59 38 30 3 69 61 24 ...
 $ lat              : num  47.5 47.7 47.7 47.5 47.6 ...
 $ long             : num  -122 -122 -122 -122 -122 ...
 $ sqft_living15    : int  1340 1690 2720 1360 1800 4760 2238 1650 1780 2390 ...
 $ sqft_lot15       : int  5650 7639 8062 5000 7503 101930 6819 9711 8113 7570 ...
 $ sqft_living_group: Factor w/ 7 levels "(0,1000]","(1000,2000]",..: 2 3 1 2 2 5 2 2 2 2 ...
north.mister
  • 500
  • 1
  • 7
  • 24
  • It's `limits`, not `limit`, followwed by a vector. Try `scale_x_discrete(limits = c("(0, 1000]", "(1000, 12000]"))` ? – neilfws Mar 20 '18 at 23:00
  • Already tried that, gives me: "Removed 7 rows containing missing values (position_stack)." To be clear, c("(0, 1000]", "(1000, 2000]") also failed. – north.mister Mar 20 '18 at 23:01
  • That's what you might expect if values lie outside your limits? – neilfws Mar 20 '18 at 23:02
  • All the values are in those limits though. Giving the interval 1000-12000 includes around 95% of the values. When I don't provide xlim the graph renders as expected. My goal is to cut off the top two limits – north.mister Mar 20 '18 at 23:03
  • can you give us a `dput()` of your data? Otherwise we're just guessing. – De Novo Mar 20 '18 at 23:04
  • dput() output would be too large. Is str() enough to get a sense of the data? – north.mister Mar 20 '18 at 23:07
  • That's the work of asking a good question :) Pare down your data to a minimal example that will reproduce the problem, then `dput()` that minimal example. See [how to make a great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – De Novo Mar 20 '18 at 23:13
  • So since the number of rows doesn't really matter...here's a 5 row dput sample: https://pastebin.com/uyxrDUye All I need is to set the xlimit on the groups – north.mister Mar 20 '18 at 23:24

0 Answers0