Home > Blockchain >  Plot the name.arg above the chart instead of the default value below
Plot the name.arg above the chart instead of the default value below

Time:11-01

I´ve been trying to change the name of the variables in the barplot() from the bottom of the chart to the top, but didn't find how to do it so far.

The code I have is as follows:

    matrix(c(1, 0, 0, 0, 0.9375, 0.0625, 0, 0, 1, 0, 0, 0, 0.8125, 0.0625, 0.125, 0, 0.75, 0.1875, 0, 0.0625, 0.625, 0.125, 0.875, 0.0625, 0.5, 0.125, 0.125,0.25), 4, 7)
    barplot(matriz_plot,  col=c("red","white"), col.axis="blue", border="black",ylim =c(0, 4))       

enter image description here

Also, I have trouble to plot the bars as they should be. In my plot, variables 1, 2, 3, and 4 are stacked, but I would like to show them at their level. The plot I am looking for, is rather this one: enter image description here

Thanks a lot for your help!

CodePudding user response:

You can do it this way:

mat <- apply(matriz_plot, 2, function(x) c(rbind(x, 1-x)))
bp <- barplot(mat, col=c("red", "white"), col.axis="blue", 
              border="black", ylim=c(0, 4))       
mtext(paste0("S", seq_along(bp)), at=bp, col="blue", line=.5)

barplot

The first line (apply) computes the reciprocal for each value in your matrix and merges it with the original values (by columns) so that you can simply use barplot to plot it.

  • Related