Error: Discrete value supplied to continuous scale

R语言ggplot绘图时出现如标题错误

应该是数据有问题,str()检查一下数据

Error: Discrete value supplied to continuous scale_第1张图片

发现数据是chr,转换数据为numeric才可以

在一开始读入数据的时候,数据格式就不对,某些数据出现了“,”,所以读入Rstudio时就认为这个变量是chr型,修改这些异常数据,再次读入RstudioError: Discrete value supplied to continuous scale_第2张图片

#去掉Profitability变量中的%
MPHS2011.2$Profitability<-gsub('[%]', '', MPHS2011.2$Profitability)
#去掉数值中的","
MPHS2011.2$Worldwide.Gross<-gsub('[,]', '', MPHS2011.2$Worldwide.Gross)
MPHS2011.2$Number.of.Theatres.in.US.Opening.Weekend<-gsub('[,]', '', MPHS2011.2$Number.of.Theatres.in.US.Opening.Weekend)
MPHS2011.2$Box.Office.Average.per.US.Cinema..Opening.Weekend.<-gsub('[,]', '', MPHS2011.2$Box.Office.Average.per.US.Cinema..Opening.Weekend.)
#去掉无意义的"?"、"??"
MPHS2011.2$Rotten.Tomatoes..<-gsub('[?]', '', MPHS2011.2$Rotten.Tomatoes..)
MPHS2011.2$Audience..score..<-gsub('[?]', '', MPHS2011.2$Audience..score..)
MPHS2011.2$Foreign.Gross<-gsub('[??]', '', MPHS2011.2$Foreign.Gross)
MPHS2011.2$Foreign.Gross<-gsub('[?]', '', MPHS2011.2$Foreign.Gross)
MPHS2011.2[,16]<-""

 将数据读出,在写入Rstudio

write.csv(MPHS2011.2,"D:\\Desktop\\EDA\\EDAprojectdata\\MPHS_US_2011.csv")
MPHS2011.3<-read.csv("D:\\Desktop\\EDA\\EDAprojectdata\\MPHS_US_2011.csv",header = TRUE)

Error: Discrete value supplied to continuous scale_第3张图片

 画图

#折线图
p1<-ggplot(MPHS2011.3)+
  geom_line(aes(x=Film,y=`Domestic.Gross($m)`,group=1,color="国内市场"),stat = "identity") + 
  labs(x = "电影", y = "毛利润($m)", title = "国内、国外、全球市场收益对比") + 
	theme(axis.title = element_text(size = 8), axis.text = element_text(angle = 45, size = 2, color = 'grey'));p1
p2<-p1+geom_line(aes(x=Film,y=`Foreign.Gross($m)`,group=1,color="国外市场"),stat = "identity");p2
p3<-p2+geom_line(aes(x=Film,y=`Worldwide.Gross($m)`,group=1,color="全球市场"),stat = "identity");p3

Error: Discrete value supplied to continuous scale_第4张图片

 

你可能感兴趣的:(r语言,开发语言)