Reputation: 53916
Using ggplot I am trying to plot two lines of values. So var0 has values 49,5,20 for "Monday" , "Tuesday" , "Wednesday" and var1 has values 49,1,20 for "Monday" , "Tuesday" , "Wednesday"
Here is the code :
test_data <- data.frame(
var0 = c(49, 5, 20),
var1 = c(49, 1, 10),
days = c("Monday" , "Tuesday" , "Wednesday"))
ggplot(test_data, days)
geom_line(y = var0, colour = "var0")
geom_line(y = var1, colour = "var1")
here are the errors :
> ggplot(test_data, days)
Error in inherits(mapping, "uneval") : object 'days' not found
> geom_line(y = var0, colour = "var0")
Error in do.call("layer", list(mapping = mapping, data = data, stat = stat, :
object 'var0' not found
> geom_line(y = var1, colour = "var1")
Error in do.call("layer", list(mapping = mapping, data = data, stat = stat, :
object 'var1' not found
Am I setting up the data correctly ?
Upvotes: 1
Views: 2353
Reputation: 98599
There were several mistakes in your code - x and y values should be put inside the aes()
call and then there was missing +
sign between ggplot()
and geom_line()
calls.
If you need to plot two groups of y values I would suggest, first, melt your data and then plot melted data frame. With melted data you will need only one geom_line()
call and you can set color for each line according to variable
that will be shown in legend.
library(reshape2)
test_data2<-melt(test_data,id.vars="days")
test_data2
days variable value
1 Monday var0 49
2 Tuesday var0 5
3 Wednesday var0 20
4 Monday var1 49
5 Tuesday var1 1
6 Wednesday var1 10
ggplot(test_data2,aes(days,value,color=variable,group=variable))+geom_line()
Upvotes: 4