bli12blu12
bli12blu12

Reputation: 357

Using ggplot2, getting fortify(data) error

I got the following dataframe:

 Date         Value1   Value2
 2001-05-01     20      -0.5

I use ggplot from the package ggplot2.

ggplot(df, aes(Date, Value1)) + geom_point(colour = "black")  + xlab("") + ylab("Name") + geom_smooth(method= "loess", colour = rgb(red=0.50, blue = 0.50, green = 0.50)) + scale_x_datetime(date_breaks = "6 month", date_minor_breaks = "3 month", date_labels = "%b-%Y") 

So this code untill here worked fine.

Then I came up with the idea to add another line which represents the data from my value2.

so I appended this code to the above one

+ geom_line(data = Value2, colour = "red")

and I get the following error message I cannot solve.

"Error in fortify(data) : Object 'Value2' not found

Anyone any ideas?

Thank you! :)

Upvotes: 0

Views: 4398

Answers (1)

J_F
J_F

Reputation: 10372

Try this approach: do your mapping of aes() not in the ggplot() command but in your geom_line() function for both of your values.

ggplot(df) + 
geom_point(aes(Date, Value1), colour = "black")  + 
xlab("") + 
ylab("Name") + 
geom_smooth(method= "loess", colour = rgb(red=0.50, blue = 0.50, green = 0.50)) + 
scale_x_datetime(date_breaks = "6 month", date_minor_breaks = "3 month", date_labels = "%b-%Y") + 

geom_line(mapping = aes(Date, Value2), colour = "red")

Upvotes: 1

Related Questions