Storyline over the last part of the barplot

I have a barrel to which the second half should conform to this formula y~axexp(-b*x^2). Now I want to build the entire barplot and display the installed model on the last part of the panel, because it is held only for this part. However, I can not find a way to display a line graph only in the second half. If I just do something like


submitted=c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 3L, 2L, 1L, 1L, 4L, 
3L, 2L, 11L, 6L, 2L, 16L, 7L, 17L, 36L, 27L, 39L, 41L, 33L, 42L, 
66L, 92L, 138L, 189L, 249L, 665L, 224L, 309L, 247L, 641L, 777L, 
671L, 532L, 749L, 506L, 315L, 292L, 281L, 130L, 137L, 91L, 40L, 
27L, 34L, 19L, 1L)
x=seq(0:(length(submitted)-1))
y1=rs$submitted[30:(length(submitted)-1)]
x1=seq(0:(length(y1)-1))
fit1=nls(y1~a*x1*exp(-b*x1^2),start=list(a=500,b=.01),trace=TRUE)
barplot(submitted,names.arg=x, las=2, cex.axis=0.8, cex=0.8)
lines(predict(fit1))

The string is displayed, but in the wrong position. So how can I control where the line is drawn?

+3
source share
2 answers

, , , , x, . x- , barplot:

dat <- 1:5                   # fake data for barplot
fit <- dat+rnorm(5, sd=0.1)  # fake fitted values

bp <- barplot(dat)           # draw plot and capture x-coordinates
lines(bp, fit)               # add line

Edit:
. , idx, , :

x <- 0:(length(submitted)-1) 
idx <- 30:(length(submitted)-1)  # the part of the data to be modeled
y1 <- submitted[idx] 
x1 <- idx-30 
fit1 <- nls(y1~a*x1*exp(-b*x1^2),start=list(a=500,b=.01),trace=TRUE) 
# capture the midpoints from the barplot
bp <- barplot(submitted,names.arg=x, las=2, cex.axis=0.8, cex=0.8) 
# subset the midpoints to the range of the fit
lines(bp[idx], predict(fit1)) 

( , seq(0:n) 0:n, 0 n.)

+3

Aniko . submitted, .

:

y1 <- submitted[30:(length(submitted)-1)]
x1 <- seq(length(y1))

seq() . . x- barplot(), Aniko. x , as.vector(), .

fit1 <- nls(y1~a*x1*exp(-b*x1^2),start=list(a=500,b=.01),trace=TRUE)
bar <- barplot(submitted, las=2, cex.axis=0.8, cex=0.8)
bar2 <- as.vector(bar)

bar2, , , . , lines() x ​​ , y-. , length(). , :

lines(x = bar2[30:(length(bar2)-1)], y = predict(fit1))
0

Source: https://habr.com/ru/post/1733026/


All Articles