Home > Enterprise >  Moving the y scale in ggplot, geom_col
Moving the y scale in ggplot, geom_col

Time:09-24

I want my lowest listed y scale number to be at the bottom of the chart. When I run the code, the lowest number is in the middle of the chart.

The dataframe:

|casual_member|weekday|number_of_rides|average_duration|
|-------------|-------|---------------|----------------|
|casual       |Sun    |333451         |23.95861 mins   |
|casual       |Mon    |197285         |22.00945 mins   |
|casual       |Tue    |192038         |20.70753 mins   |
|casual       |Wen    |203911         |20.27327 minss  |
|casual       |Thu    |209860         |20.06790 mins   |
|casual       |Fri    |274578         |21.13946 mins   |
|casual       |Sat    |421152         |23.24371 mins   |
|casual       |Sun    |309201         |15.96388 mins   |
|casual       |Mon    |328976         |13.87167 mins   |
|casual       |Tue    |357244         |13.75974 mins   |
|casual       |Wen    |381470         |13.76543 mins   |
|casual       |Thu    |367625         |13.69283 mins   |
|casual       |Fri    |374362         |14.05773 mins   |
|casual       |Sat    |370914         |15.67448 mins   |

The code

    ggplot(data = all_trips_v3) 
    geom_col(aes(x= weekday, y=number_of_rides, fill = member_casual),
             position = "dodge") 
    ggtitle("Total Weekday Rides of Casuals and Members") 
    scale_y_continuous(breaks = c(150000, 200000, 250000, 300000,
                                  350000, 400000, 450000), 
                       name = "Number of Rides") 
    scale_x_discrete (name = "Weekday")

How do I get the 150000 to be at the bottom of the chart?

I have tried the following, and the codes removed the bars from my chart:

    scale_y_continuous(limits = c(150000,450000), name = "Number of Rides") 
    scale_y_discrete(name = "Number of Rides")  scale_x_discrete (name = "Weekday")  ylim(150000, 450000)

CodePudding user response:

So ylim() removes the data which is outside the plot limits. Instead try adding coord_cartesian, to zoom in on the plot:

coord_cartesian(ylim = c(150000, 450000))
  • Related