在ggplot2中绘制折线图

时间:2014-04-01 11:07:47

标签: r graph plot ggplot2

我试图使用下面的数据和代码在R.3.0.2上使用ggplot2绘制折线图,​​但它返回错误geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?而没有任何输出。我怎么解决这个问题我试过this

month <- c("01", "02", "03", "04", "05", "06", "07", "08" ,"09","10", "11" ,"12",NA)
yr.count <- c(357.500000 ,301.785714, 317.142857 ,283.071429 ,332.500000 ,333.285714 ,354.285714, 308.357143 ,272.142857 ,273.214286, 312.571429 ,337.714286,5.92857)
month.mean2 <- data.frame(month=month, yr.count=yr.count)
ggplot() + geom_line(data=month.mean2, 
                     aes(x=month, y=as.numeric(yr.count)), 
                     colour='red') 

1 个答案:

答案 0 :(得分:1)

通常,使用轴上的字符意味着每个唯一值有多个值,并且需要进行某种聚合。您可以通过设置group = 1

来强制连接线条
month <- c("01", "02", "03", "04", "05", "06", "07", "08" ,"09","10", "11" ,"12",NA)
yr.count <- c(357.500000 ,301.785714, 317.142857 ,283.071429 ,332.500000 ,333.285714 ,354.285714, 308.357143 ,272.142857 ,273.214286, 312.571429 ,337.714286,5.92857)
month.mean2 <- data.frame(month=month, yr.count=yr.count)
ggplot() + geom_line(data=month.mean2, 
                     aes(x=month, y=as.numeric(yr.count), group = 1), 
                     colour='red')

enter image description here