Rilcon42
Rilcon42

Reputation: 9763

Columns not grouping correctly when plotted in ggplot

I was following the accepted SO answer to get a line graph with 2 lines over multiple years. Can someone explain what I am doing wrong?

temp<-structure(list(Years = c(1950, 1955, 1960, 1965, 1970, 1975, 
                               1980, 1985, 1990, 1995, 2000, 2002, 2005, 2007, 2009, 2011, 2013, 
                               2015, 2017), X1878_8TF = c(4, 5, 10, 20, 15, 22.5, 80, 70, 47.5, 
                                                          47.5, 65, 125, 125, 150, 150, 140, 140, 160, 170), X7_TF_rev_1878 = c(3, 
                                                                                                                                3, 3, 3.75, 6.5, 17, 65, 57.5, 30, 25, 35, 50, 60, 75, 85, 80, 
                                                                                                                                80, 85, 90)), .Names = c("Years", "X1878_8TF", "X7_TF_rev_1878"
                                                                                                                                ), class = "data.frame", row.names = c(NA, -19L))


df <- melt(temp ,  id.vars = 'Years', variable.name = 'coin_name')
ggplot(df, aes(Years,value)) + 
  geom_line(aes(colour = rainbow(nrow(df))))+
  ggtitle("blah")

This is what I get:

enter image description here

Upvotes: 1

Views: 90

Answers (1)

Lucy
Lucy

Reputation: 991

If you just want two lines, I think you want something like:

ggplot(df, aes(Years,value)) +
 geom_line(aes(color = coin_name)) +
 ggtitle("blah")

As you have it, it's trying to make a separate line for every point in your dataset.

Upvotes: 2

Related Questions