Dom
Dom

Reputation: 1053

How to stop ylim function in ggplot removing geom

The problem

When I try to reduce the scale of the y-axis in my ggplot I get a warning message "Removed 6 rows containing missing values (geom_col)." and the plot is returned minus the geom layer (I get a blank plot back). I'd like to know why this happens and how I can re-scale the y-axis without an error.

The data

df <- structure(list(income_year = c("2010–11", "2011–12", "2012–13", 
"2013–14", "2014–15", "2015–16"), wage = c(29239.0174658421, 
30887.9384853822, 33531.5431774418, 33938.804992963, 34252.5789256144, 
35474.8271442147)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L))

My attempt

ggplot(data = df, aes(x = income_year, y = wage)) +
  geom_col() +
  ylim(25000, 36000)

I thought this would be ok because the ylim's I've chosen are greater than the respective min and max of my data.

> summary(df)
 income_year             wage      
 Length:6           Min.   :29239  
 Class :character   1st Qu.:31549  
 Mode  :character   Median :33735  
                    Mean   :32887  
                    3rd Qu.:34174  
                    Max.   :35475  

Upvotes: 0

Views: 447

Answers (1)

dylanjm
dylanjm

Reputation: 2101

Setting ylim() actually alters your data before plotting and since you are trying to plot a bar chart its taking your limits and chopping off all the data that goes into the bar. You can use coord_cartesian() to not chop off any values:

ggplot(data = df, aes(x = income_year, y = wage)) +
  geom_col() +
  coord_cartesian(ylim = c(25000, 36000))

Upvotes: 3

Related Questions