I am trying to play around with the mpg
dataset in ggplot2
, and I would like to convert the wide data format to long data format for the columns cty and hwy:
Wide/Original data format
manufacturer model cty hwy class
audi a4 18 29 compact
audi a4 21 28 compact
to this long data format:
manufacturer model variable value class
audi a4 cty 18 compact
audi a4 hwy 29 compact
audi a4 cty 21 compact
audi a4 hwy 28 compact
I tried to use reshape2
to do this conversion:
mpg_long <- melt(mpg, id.vars=c("hwy", "cty"), variable.name="road_type", value.name="efficiency")
This does not work for me. I appreciate your help!