Reputation: 1
I really cannot figure out why I can't get this to work, I have tried so many different things I've found online to no avail. Here's the data I start with:
structure(list(Date = structure(c(16923, 16924, 16925, 16930,
16931, 16932), class = "Date"),
bfolioTotalInvNZ = c("100", "100.396798622275",
"100.289579106988", "99.9190144893418", "100.439784570027", "100.124973416093"
),
hpfolioTotalInvNZ = c("100", "99.8882748824908", "98.8225791351364",
"98.0760852984932", "99.6998217808326", "99.8428365508291"),
lpfolioTotalInvNZ = c("100", "100.680353722841", "99.7285569078393",
"99.2649714247455", "100.21300064583", "100.043862125597"),
hROATotalInvNZ = c("100", "100.38312056174", "100.860708831452",
"99.4017877917418", "100.396352403539", "101.764817682509"),
lROATotalInvNZ = c("100", "100.611388876841", "99.7567183567618",
"99.9634937211577", "101.234588176424", "102.218770263564"
)), row.names = c("20160502", "20160503", "20160504", "20160509",
"20160510", "20160511"), class = "data.frame")
Basically its a list of multiple portfolio returns over a 4 year period. The first column is a date column formatted as such with row names also as dates even though it's redundant.
I then use the melt command and get the following output:
melted<- melt(plottable, id = "Date")
Date variable value
1 2016-05-02 bfolioTotalInvNZ 100
2 2016-05-03 bfolioTotalInvNZ 100.396798622275
3 2016-05-04 bfolioTotalInvNZ 100.289579106988
4 2016-05-09 bfolioTotalInvNZ 99.9190144893418
5 2016-05-10 bfolioTotalInvNZ 100.439784570027
6 2016-05-11 bfolioTotalInvNZ 100.124973416093
And then the following code to plot them:
ggplot(melted,
aes(x = Date,
y = value,
col = variable)) +
geom_line()
It gives me the following error: geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
I've been working on this for what feels like forever and can't seem to solve this. If anyone could help me out I'd be so appreciative. I'm just trying to plot all these inside the same plot simultaneously and I'm really not sure what keeps going wrong.
Edit: If I add the group = variables command such as:
ggplot(melted, aes(Date, value)) +
geom_line(aes(colour = variable, group = variable))
I get the following output: enter image description here
I have no idea why because the values are between 50-250 and I even cut the number of observations down to 600 each.
Upvotes: 0
Views: 289
Reputation: 6769
Because your current value
is a character. Converting it to numeric will fix the problem.
library(data.table)
library(tidyverse)
melted<- melt(plottable, id = "Date") %>%
mutate(value = as.numeric(value))
ggplot(melted,
aes(x = Date,
y = value,
col = variable)) +
geom_line()
Upvotes: 1