在ggplot中绘制时,列未正确分组

时间:2017-10-26 23:07:20

标签: r ggplot2

我关注the accepted SO answer以获得多年来有2行的折线图。有人可以解释我做错了吗?

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")

这就是我得到的:

enter image description here

1 个答案:

答案 0 :(得分:2)

如果你只想要两行,我想你想要这样的东西:

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

正如您所知,它正在尝试为数据集中的每个点创建一个单独的行。