Home > database >  Unable to plot aggregate vector using two factor variables - double bar graph
Unable to plot aggregate vector using two factor variables - double bar graph

Time:02-19

I have used the below code to successfully plot a similar vector ("todmc_v1"), but not working for the specific vector I'm having trouble with, which I will provide the dput() of.

ggplot(todmc_v1, aes(x=factor(tod, level = c('Morning', 'Afternoon', 'Evening', 'Late')), y=value, fill=variable))   
  geom_bar(stat='identity', position='dodge')

Here is the dput() of the vector "rldow" that I am unable to plot:

structure(list(`data_2021_v2$member_casual` = c("casual", "member", 
"casual", "member", "casual", "member", "casual", "member", "casual", 
"member", "casual", "member", "casual", "member"), `data_2021_v2$day_of_week` = c("Friday", 
"Friday", "Monday", "Monday", "Saturday", "Saturday", "Sunday", 
"Sunday", "Thursday", "Thursday", "Tuesday", "Tuesday", "Wednesday", 
"Wednesday"), `data_2021_v2$ride_length` = c(1760.09364802057, 
799.435593679782, 1891.04649254227, 794.827114155888, 2042.12860343406, 
915.879808325155, 2203.20327031892, 939.474596054804, 1623.62034644899, 
766.524376333107, 1665.79837660326, 767.099664854878, 1613.5331421196, 
768.874141204975)), row.names = c(NA, -14L), class = "data.frame")

Not sure if relevant, but this vector was created from an aggregate function, which is below:

rldow <- aggregate(data_2021_v2$ride_length ~ data_2021_v2$member_casual   data_2021_v2$day_of_week, FUN = mean)

I have tried to melt() the vector so it is sorted by column "member_casual" but variations I have tried end up with the column names in rows as values - which is very odd. This might also have something to do with why dput() included backticks around my column names. I know I could manually create the vector, but that would take time and am hoping I can use the aggregate function to create plottable vectors in the future.

So you have an idea of the output I'm looking for please see below screenshot - produced after I exported the data to EXCEL. I appreciate the help on this.

Example of how I would like plot to appear

CodePudding user response:

Here I tried to reproduce your graph. The pattern of my ggplot graph and your Excel graphs are high similar, but I'm not sure what y-axis values are you using, the values range from 0 to 40 is not present in your dput().

library(ggplot2)

ggplot(df, aes(factor(`data_2021_v2$day_of_week`, 
                      levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", 
                                 "Sunday")), 
               `data_2021_v2$ride_length`, 
               fill = fct_rev(`data_2021_v2$member_casual`)))  
  geom_col(position = position_dodge(width = 1))  
  scale_fill_manual(values = c("dodgerblue4", "darkorange2"), 
                    labels = c("Member Avg Ride Length", "Casual Avg Ride Length"))  
  theme_minimal()  
  theme(legend.position = "bottom", 
        legend.title = element_blank(),
        axis.title = element_blank())

ggplot_mimic_excel

  • Related