0

I have a data frame like this:

dput(data)

structure(list(Anno = c(2015L, 2014L), Gennaio = c(381, 270.9
), Febbraio = c(355, 266.75), Marzo = c(352, 285.5), Aprile = c(323, 
288), Maggio = c(296, 288), Giugno = c(307, 276.17), Luglio = c(340, 
298.75), Agosto = c(335, 307.5), Settembre = c(304, 307.5), Ottobre = c(283, 
342.5), Novembre = c(281, 401.25), Dicembre = c(274, 387.5)), .Names = c("Anno", 
"Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", 
"Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"
), class = "data.frame", row.names = c(NA, -2L))

I would like to plot this with multiple lines, gruped by Year (Anno). How can i do that? Something like this: enter image description here

I tried this:

data.melted <- melt(data, id.vars="Anno", value.name="Mese", variable.name="Anno")
ggplot(data=data.melted, aes(x=Anno, y=Mese, group = Anno, colour = Anno)) +
+     geom_line() +
+     geom_point( size=4, shape=21, fill="white")

But i get this error:

Error in eval(expr, envir, enclos) : oggetto "Mese" non trovato
1
  • add a geom_line() or geom_smooth(se = F). Commented Jul 14, 2016 at 20:29

1 Answer 1

1

Try this:

library(reshape2)
library(ggplot2)
library(dplyr)

melt(data, "Anno", variable.name = "Mese") %>%
    ggplot(aes(x=Mese, y = value, color = as.factor(Anno))) + 
    geom_point(size=4, shape=21, fill="white") +
    geom_line(aes(group = Anno))

The piping using dplyr %>% operator is of course optional. You can save intermediate steps as you did in your code.

The result is the following plot: enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I get this error "Error in eval(expr, envir, enclos) : oggetto "Mese" not found"
Did you use the code I posted? I do not get the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.