GGPLOT2 Question about formatting and arranging x-axis labels : rstats

I am new to using R; please be kind.

PLOT 1:

I have a dataset where I used Lubridate to mutate the hour-of-day, as a number, into a new column from out of a datetime column.

r/rstats - GGPLOT2 Question about formatting and arranging x-axis labels

When I first made the plot, the x-axis labels started at 0 and went to 23, as expected, but I couldn’t figure out how to change the format of the x-axis labels in an easy way.

In order to achieve the above, I resorted to this:

trips_by_hour_of_day_df <- combined_df %>%
  select(start_hour, member_casual) %>%
  mutate(start_hour_text = as.character(start_hour) ) %>%
  group_by(start_hour, start_hour_text, member_casual) %>%
  summarize( count = n() ) 

p7 <- ggplot(trips_by_hour_of_day_df, 
  aes(
    x = start_hour_text,
    y = count/1000,
    fill = member_casual    
  )

)+

geom_col(
  color = "black",
  position=("dodge"),
  width = 0.75
)+
  
scale_y_continuous(
  labels=comma, 
  breaks=seq(0,350,by=50)
)+ 

#replace 24 hour format hours by 12 hour format hours and force the order with 
scale_x_discrete(
  labels=c(
      "0" = "12 am",
      "1" = "1 am", 
      "2" = "2 am",
      "3" = "3 am",
      "4" = "4 am",
      "5" = "5 am",
      "6" = "6 am",
      "7" = "7 am",
      "8" = "8 am",
      "9" = "9 am",
      "10" = "10 am",      
      "11" = "11 am",
      "12" = "12 pm",
      "13" = "1 pm",
      "14" = "2 pm",
      "15" = "3 pm",
      "16" = "4 pm",
      "17" = "5 pm",      
      "18" = "6 pm",
      "19" = "7 pm",
      "20" = "8 pm",
      "21" = "9 pm",
      "22" = "10 pm",
      "23" = "11 pm"
    ),
    limits=c("0", "1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23")
)+  
  
expand_limits( 
  y=c(0, 350) 
)+ 
  
labs(
  x = 'Hour of Day', 
  y = 'Trips (thousands)', 
  title="Hourly Trips by Rider Type",
  caption = "Data source: Divvy system data, 5/2021-4/2022",
  fill="Rider Type"
)+
  
theme(
  axis.text.x=element_text(angle=60, hjust=1),
  legend.title.align = 0.5,
  axis.text = element_text(face="bold"),
  legend.key=element_rect(colour="black")
)+
  
#custom fill colors
#show custom labels for fill legend
scale_fill_manual(
  values=color_values,
  labels=c('Casual', 'Member')
)

So, I had to force the issue within scale_x_discrete(). Note that I had to resort to also use:

mutate(start_hour_text = as.character(start_hour) ) %>%

To get this to work. The previous plot that went from 0 to 23 on the x-axis did not require me to use this line (but was not formatted the way I had liked).

The older code had this in it:

  group_by(start_hour, member_casual) %>%

Is there a way to do what I wanted in an easier, more automatic fashion?

PLOT 2:

r/rstats - GGPLOT2 Question about formatting and arranging x-axis labels

This looks like a pretty typical kind of bar chart. I created it via:

trips_by_month_df <- combined_df %>%
  select(start_month, member_casual) %>%
  mutate(start_month = as.character( month.abb[start_month] )) %>%
  group_by(start_month, member_casual) %>%
  summarize( count = n() ) 

p5 <- ggplot(trips_by_month_df, 
  aes(
    x = start_month,
    y = count/1000,
    fill = member_casual    
  )

)+
  
geom_col(
  color = "black",
  position=("dodge"),
  width = 0.75
)+

scale_y_continuous(
  labels=comma, 
  breaks=seq(0,500,by=100)
)+
  
scale_x_discrete(limits = month.abb)+
  
expand_limits( 
  y=c(0, 500) 
)+
  
labs(
  x = 'Month', 
  y = 'Trips (thousands)', 
  title="Monthly Trips by Rider Type",
  caption = "Data source: Divvy system data, 5/2021-4/2022",
  fill="Rider Type"
)+
  
theme(
  legend.title.align = 0.5,
  axis.text = element_text(face="bold"),
  legend.key=element_rect(colour="black")
)+
  
#custom fill colors
#show custom labels for fill legend
scale_fill_manual(
  values=color_values,
  labels=c('Casual', 'Member')
)    

On its own, the plot shows some valuable information, such as which months are most and least popular, and trends.

GGPLOT2 puts the x-axis in order starting with Jan.

However, the months are not shown chronologically in the x-axis.

For my purposes, the plot is fine as it is, but it is important to note that the bars for Apr are from April, 2022 and the bars for May are from 2021.

How can I show the bars in the same fashion, label them like “Apr. ’21” or “4/21”, and so on and show them from earliest chronological position on the left (5/21) to latest on the right (4/22)?

Here are the color values:

#Okabe-Ito colors for the colorblind 
color_values=c("#E69F00", #orange
    "#56B4E9", #skyblue
    "#009E73", #bluishgreen
    "#F0E442", #yellow
    "#0072B2", #blue
    "#D55E00", #vermillion
    "#CC79A7", #reddishpurple 
    "#999999", #gray
    "#000000" #black                         
)

Thank you all for your help!

Read more here: Source link