如何绘制因子和数字?

时间:2015-09-24 17:33:05

标签: r ggplot2

year  val
1966  1
1967  22
1968  55
1969  57

这里,年份是水平的因素

val是数字

如何在x轴上绘制年份,在y轴绘制val?

ggplot(data=df,aes(x="year",y="val"))+
geom_line()+
ggsave('test.png')

实际数据集

df <- structure(list(year = structure(1:47, .Label = c("1936", "1947", 
    "1948", "1950", "1952", "1954", "1957", "1959", "1961", "1972", 
    "1973", "1975", "1979", "1980", "1981", "1982", "1983", "1984", 
    "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", 
    "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", 
    "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", 
    "2009", "2010", "2011", "2012", "2013"), class = "factor"), val = c(1L, 
    2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 8L, 2L, 4L, 1L, 1L, 3L, 43L, 
    146L, 108L, 68L, 200L, 414L, 367L, 977L, 1996L, 1768L, 1875L, 
    3235L, 1966L, 2244L, 3500L, 2479L, 3536L, 2062L, 1687L, 2563L, 
    2576L, 3884L, 3969L, 3101L, 2810L, 2941L, 4207L, 3353L, 2596L, 
    4485L, 1801L, 3286L)), .Names = c("year", "val"), row.names = c(NA, 
    -47L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

编辑:在OP的问题中有数据后

将年份更改为数字

df$year<-as.numeric(as.character(df$year))

现在,情节

library(ggplot2)
ggplot(data=df, aes(x=year, y=val))+geom_line()

enter image description here