I have a matrix that I want to reform for plotting in ggplo2
using the melt
function from reshape2
but cannot find a way to add custom header names.
#Create toy data
MyData <- matrix(rnorm(15,500), nrow = 5, ncol = 3, dimnames = list(
c("Unknown","0-4","4-9","10-14","15-19"),c("Area1","Area2","Area3")))
Dat2 <- melt(MyData, value.name = "Count")
#Reform data using melt, define Count as value name
MyData2 <- melt(MyData, value.name = "Count")
This gets me what I want but then operations that follow have to refer to the Var1
and Var2
.
I tried naming them explicitly using variable.name
:
MyData2 <- melt(MyData, value.name = "Count",
variable.name = c("AgeGroup", "Geo"))
I can of course name them after the fact using colnames()
but would like to do it using melt
. Is this possible? Do I need to back up?