我正在使用ggplot2
rm(list=ls())
library(ggplot2)
library(scales)
library(grid)
library(gcookbook)
spi<-read.csv("netspill.csv",as.is=T)
attach(spi)
spi$date<-as.Date(spi$date)
str(spi)
datebreaks1<-seq(as.Date("2007-05-16"), as.Date("2013-05-31"), by="12 month")
'data.frame': 1848 obs. of 21 variables:
$ obs : int 257 258 259 260 261 262 263 264 265 266 ...
$ date : Date, format: "2007-05-16" "2007-05-17" "2007-05-18" ...
$ spillvol : num 19 18 18 17.4 17.5 ...
$ CDStoasset : chr "16.22776618" "15.81730867" "15.36503932" "14.270632" ...
$ assettocds : chr "19.62214133" "19.09211389" "18.49333545" "17.70890578" ...
$ cdskopsi : num 5.87 5.55 5.59 5.37 5.22 ...
和
CDStoasset
然后我的数据
"26.92311877" "26.48164709" "26.49961707" "26.59021979" "25.96024052" "25.94692576" "26.37937264" "27.11073195" "27.06050636" "26.91970508"
[1471] "29.00362056" "29.83671176" "29.55687587" "29.80806098" "29.42882282" "29.61759614" "29.57466413" "29.35304864" "29.36422448" "29.65128254"
[1481] "29.52697991" "29.85158673" "29.82680797" "30.22596306" "30.08997558" "29.76438051" "29.79329155" "29.89312089" "29.10694984" "28.75016179"
[1491] "26.95089819" "26.76647669" "26.73918817" "26.95267275" "27.58483418" "26.08107046" "26.19364923" "24.86188192" "25.75699619" "25.79200358"
[1501] "25.56374996" "25.70380996" "26.8145622" "25.98075843" "24.39756886" "23.10004291" "22.66858838" "21.68670369" "22.79964894" "18.67955464"
[1511] "18.33640825" "16.86925316" "17.25132444" "19.63740405" "19.93870049" "22.89525288" "20.10651533" "30.43668745" "32.57389424" "#N/A"
[1521] "#N/A" "#N/A" "37.81476543" "36.86642872" "37.66806016" "31.96648566" "31.26587858" "31.39916336" "31.86599313" "32.20460811"
包含N / A值。
cds<-ggplot(spi,aes(x=date,y=CDStoasset))+geom_line() + labs(x = "", y = "")+ scale_y_continuous(breaks=seq(0,100,10))
cds
然后发生错误:提供给连续比例的离散值:
并且没有显示图表。
我如何解决这个问题?
请帮帮我......
答案 0 :(得分:0)
这是答案 Eliminating NAs from a ggplot
您可以设置ggplot数据,如
ggplot(data = subset([data_name], !is.na([data_name]), ..... )
处理您的N / A值。
因此您可以更改代码,
ggplot(data =(spi, !is.na(CDStoasset) ),aes(x=date,y=CDStoasset))+
geom_line() +
labs(x = "", y = "") +
scale_y_continuous(breaks=seq(0,100,10))
但我不确定N / A处理问题是否存在问题
答案 1 :(得分:0)
您的y值为字符,但是您告诉ggplot使用连续的y缩放(这也是错误消息试图告诉您的内容)。如果运行spi$CDStoasset <- as.numeric(spi$CDStoasset)
,应该可以修复该错误。