R语言ggplot2行向左移动

时间:2015-08-05 23:28:48

标签: r ggplot2

[![R语言ggplot2行左移] [1]] [1]

条形图正确地以x轴值0,1,2等为中心但是线条错误地向左移动了一个完整单位(即-1,0,1等)我做错了什么?

w <- data.frame(Funding<-"Fully Insured",Region="North East",claim_count=rnbinom(1000, 300.503572818, mu= 0.5739467))
x <- data.frame(Funding<-"Fully Insured",Region="South East",claim_count=rnbinom(1000, 1000, mu= 0.70000000))
y <- data.frame(Funding<-"Self Insured",Region="North East",claim_count=rnbinom(1000, 400, mu= 0.80000000))
z <- data.frame(Funding<-"Self Insured",Region="South East",claim_count=rnbinom(1000, 700, mu= 1.70000000))
names(w)<-c("Funding","Region","claim_count")
names(x)<-c("Funding","Region","claim_count")
names(y)<-c("Funding","Region","claim_count")
names(z)<-c("Funding","Region","claim_count")
my_df <- rbind(w,x,y,z)
my_df2<-as.data.frame(table(my_df))    

xmin<-0
xmax<-max(as.numeric(my_df2$claim_count))
seq_claim_count<-xmin:xmax    

ww <- data.frame(Funding<-"Fully Insured",Region="North East",claim_count=seq_claim_count)
xx <- data.frame(Funding<-"Fully Insured",Region="South East",claim_count=seq_claim_count)
yy <- data.frame(Funding<-"Self Insured",Region="North East" ,claim_count=seq_claim_count)
zz <- data.frame(Funding<-"Self Insured",Region="South East" ,claim_count=seq_claim_count)
names(ww)<-c("Funding","Region","claim_count")
names(xx)<-c("Funding","Region","claim_count")
names(yy)<-c("Funding","Region","claim_count")
names(zz)<-c("Funding","Region","claim_count")
ww$Freq<-dnbinom(seq_claim_count, 300.503572818, mu= 0.5739467)*1000
xx$Freq<-dnbinom(seq_claim_count, 1000, mu= 0.70000000)*1000 
yy$Freq<-dnbinom(seq_claim_count, 400, mu= 0.80000000)*1000
zz$Freq<-dnbinom(seq_claim_count, 700, mu= 1.70000000)*1000
my_df <- rbind(ww,xx,yy,zz)
predicted<-my_df

library(ggplot2) 
#Colors taken from http://colorbrewer2.org/
sp<-ggplot(my_df2,aes(x=claim_count,y=Freq,group=Funding))+geom_histogram(colour="#f1a340",fill="#f1a340",stat="identity")
sg<-sp+labs(y="Number of Insureds")
pp<-sg+geom_line(data=predicted, size=1,colour="#998ec3",linetype="solid") 
pp+geom_point(data=predicted, size=1,colour="#998ec3") + facet_grid(Region~Funding,scales = "free", shrink=TRUE)

1 个答案:

答案 0 :(得分:0)

您正在混合数据类型。在my_df2中,claim_count是一个因素,但predicted$claim_count是一个整数。

predicted$claim_count = as.factor(predicted$claim_count)

在你的情节之前运行上面一行,你会没事的。