Home > OS >  Grouped bar charts for modal split values
Grouped bar charts for modal split values

Time:09-17

I want to plot my data frame containing different modal split values(% of car usage, % of bike usage..) for different path lengths(under 5 km, 5-10km, 10-30km...) Each element in my data frame contains the % of vehicle usage for each path length.

My goal is to plot all values in one plot. I want to create a bar chart, with a bar for each path length, representing all vehicle percentages (the modal split).

My dataframe contains in the first column the vehicle modes(car, bike,..), and the columns 2-10 contain the percentages for each path length group.

I tried:

testtest <- ggplot()   geom_col(data = ms_gruppen_d, 
       aes(x = colnames(ms_gruppen_d)[2:9], 
           y =  ms_gruppen_d[,2:9],
           fill = ms_gruppen_d[,1]))

My values are not categorical, so I cannot use the "count" function. Can someone help?

Thanks

ms_gruppen_d <- structure(list(VM = c("Fußverkehr", "Fahrrad", "Motorrad/Moped/Mofa", 
"Privater_pkw", "Gewerb_pkw", "Lkw_bis_3_5_", "Lkw_ab_3_5_", 
"Sattelzug", "ÖPNV"), `Laenge unter 5km` = c(0.218428835651906, 
0.208360071967382, 0, 0.337471470224058, 0.195785602540656, 0.0103830833919553, 
0.0123737357892543, 0, 0.0171972004347874), `Laenge 5 - 10km` = c(0.138928420064367, 
0.140725324716725, 0.00988051174398964, 0.289334484453904, 0.308718256514345, 
0.0356902893023975, 0.00988051174398964, 0.0222528559093808, 
0.044589345550901), `Laenge 10-20km` = c(0.0667063809168976, 
0.172327489225668, 0, 0.271668790053295, 0.346741728107974, 0.0573103622018356, 
0.0292526145926873, 0.0149058863164426, 0.0410867485852005), 
    `Laenge 20-30km` = c(0.0405426428226048, 0.1463357744637, 
    0.0236972749606593, 0.271246395715663, 0.354248166536575, 
    0.0855256681459516, 0.0173953892663395, 0.0432292937973128, 
    0.0177793942911947), `Laenge 30-50km` = c(0.0213163894963155, 
    0.0503758065644924, 0.0159090254544127, 0.178916279908378, 
    0.485985672387571, 0.148087763700495, 0.0378558845704386, 
    0.026693520571143, 0.0348596573467541), `Laenge 50-100km` = c(0.00652604845092996, 
    0.0123285212525124, 0, 0.177307097376991, 0.380919125770432, 
    0.154233838933756, 0.213479807823156, 0.0441531204824327, 
    0.0110524399097905), `Laenge 100-200km` = c(0, 0.00431357399129567, 
    0, 0.087013827371374, 0.173016082279325, 0.203265193001196, 
    0.399659385606215, 0.0655495360275712, 0.0671824017230226
    ), `Laenge 200-300km` = c(0, 0, 0, 0.00953852353026925, 0.147233787704061, 
    0.130598939323796, 0.518334554408677, 0.146338992010429, 
    0.0479552030227669), `Laenge 300km ` = c(0, 0, 0, 0.0333890118493603, 
    0.0876659311982381, 0.0979219742771943, 0.420951006142259, 
    0.297349051156633, 0.062723025376315)), row.names = c(NA, 
-9L), class = "data.frame") 

CodePudding user response:

The main problem is I think that your data is in the wide format instead of the long format. You can reshape the data using tidyr::pivot_longer(). Here is how you can use that function to make a grouped bar chart:

library(ggplot2)

# Reshape data, excluding column 1
df <- tidyr::pivot_longer(ms_gruppen_d, -1, names_to = "Laenge")

# Making the distances more pretty to print
df$Laenge <- factor(df$Laenge, levels = colnames(ms_gruppen_d)[-1])
levels(df$Laenge) <- gsub("Laenge ", "", levels(df$Laenge))

# A grouped bar chart
ggplot(df, aes(Laenge, value, fill = VM))  
  geom_col(position = "dodge")

However, I think a stacked bar chart might make more sense in this case, as all fractions should add up to 1.

ggplot(df, aes(Laenge, value, fill = VM))  
  geom_col(position = "stack")

Created on 2021-09-10 by the reprex package (v2.0.1)

  • Related