Stacked Barplot with Line in R

I want to make the following graph of a folded line with a linear curve.

enter image description here

But why does this following code not work? What is the right way to do this?

x<-c(0,1,2,3,4,5,6,7,8,9,10,11,12); # To plot line emp_dens <- c(0.107,0.184,0.205,0.185,0.138,0.091,0.049,0.023,0.01,0.0028,0.0012,0.00023,0.00013); dat_dens <- as.matrix(cbind(x,emp_dens)); # To plot stack bar dens_all_k <- c(1.15e-01, 1.89e-01, 2.05e-01, 1.82e-01,1.36e-01,8.68e-02,4.71e-02,2.21e-02,9.17e-03,3.37e-03,1.11e-03,3.37e-04,9.31e-05) # Each k0..5 compose the stack # For example # dens_k0[1] + .... dens_k5[1] ~= dens_all_k[1] dens_k0 <-c(2.52e-02,8.38e-02,1.38e-01, 1.53e-01,1.27e-01,8.44e-02, 4.66e-02, 2.21e-02, 9.16e-03, 3.37e-03,1.11e-03, 3.37e-04, 9.31e-05) dens_k1 <- c(6.75e-02, 8.91e-02, 5.86e-02, 2.51e-02, 8.59e-03,2.25e-03, 4.90e-04,9.35e-05, 1.55e-05,2.21e-06,2.99e-07, 3.55e-08,3.92e-09) dens_k2 <- c(1.70e-02,1.64e-02,7.95e-03, 2.56e-03,6.20e-04,1.20e-04, 1.93e-05, 2.67e-06, 3.23e-07,3.47e-08,3.36e-09, 2.95e-10,2.38e-11) dens_k3 <- c(0.005124596,0,0,0,0,0,0, 0, 0, 0, 0,0,0) dens_k4 <- c(0.0004270497, 0, 0,0,0, 0, 0, 0, 0,0,0, 0, 0) dens_k5 <- c(2.760725e-05, 0, 0, 0,0,0, 0, 0, 0, 0,0, 0,0) barplot(cbind(0:max(x),dens_all_k),xlim=c(0,max(x)),ylim=c(0,max(emp_dens)),,space=0.1,lwd=5,xlab="Value of X",ylab="Densities",font.main=1); lines(dat_dens,lty=1,col="red"); 
+6
source share
2 answers

dens_all_k contains only a summary summary. If you are looking for a barcode in the picture, you need to provide all the information k. Try the following code.

 dens_kall<-rbind(dens_k0,dens_k1,dens_k2,dens_k3,dens_k4,dens_k5) ltext<-c("K0","K1","K2","K3","K4","K5") colnames(dens_kall)<-0:12 barplot(height=dens_kall,xlim=c(0,max(x)),ylim=c(0,max(emp_dens)),,space=0.1,lwd=5,xlab="Value of X",ylab="Densities",font.main=1 ,legend.text =ltext, args.legend = list(x = "topright") ); lines(dat_dens,lty=1,col="red"); 

Output signal enter image description here

+2
source

In barplot, you are not passing a matrix with your x and y values, but just your heights. It does not make sense that you are doing all the elements of dens_k0 and never use them. Try it ...

 barplot(rbind(dens_k0, dens_k1, dens_k2, dens_k3, dens_k4, dens_k5), xlim=c(0,max(x)), ylim=c(0,max(emp_dens)), space=0.1, lwd=5, xlab="Value of X", ylab="Densities", font.main=1) lines(emp_dens,lty=1,col="red") 

Carefully study what I transferred there for future use. You probably need to set the argument of the barplot legend to make it readable. Even when the last few densitites are very small.

An easy way to add a legend is to simply make a few small changes to what you have.

 barplot(rbind(dens_k0, dens_k1, dens_k2, dens_k3, dens_k4, dens_k5, 0), col = c(grey(exp((0:5)/5) / exp(1)), 'red'), xlim=c(0,max(x)), ylim=c(0,max(emp_dens)), space=0.1, lwd=5, xlab="Value of X", ylab="Densities", font.main=1, legend.text = c('k0', 'k1', 'k2', 'k3', 'k4', 'k5', 'dens curve')) 
+1
source

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


All Articles