Bob
Bob

Reputation: 27

Barplot in R - How to divide the plot into separate parts by showing them in different colors

I am a newbie to R. I would like to create a barplot which is visually divided into different parts.

My data looks like the following:

"1.0";"0.0";"1.0";"2.0";"710";"12500"

first four numbers give the number of parts that need to be ordered from the two parts list below. The fifth number gives the result sum of the first part, the sixth then the result sum of the second part.

part 1: 10;50;100;300
part 2: 500;1000;2000;5000;

this is how it is calculated.

1 * 10 + 0 * 50 + 1 * 100 + 2 * 300 = 710 ;
1 * 500 + 0 * 1000 + 1 * 2000 + 2 * 5000 = 12500

So what I now want to plot is for example the value 12500, but I want to visually divide this value into the different part (stacked bars) like two five thousands, then one two thousand then one five hundred -> the bar should consist of these parts which can visually be seen or marked with the value (would be nice to have different colors for each part in the part)

How can I do it? Folks, I did my homework, I searched a lot and did try it on my own, but couldn't achieve what I want.

Upvotes: 1

Views: 1523

Answers (1)

mzuba
mzuba

Reputation: 1275

daten <- matrix(c(10,50,100,300,500,1000,2000,5000),ncol=2) 
multiplier <- c(1,0,1,2)
barplot(daten*multiplier)

To display bar segments in reverse order, you need to rearrange the rows in the daten*multiplier array:

barplot((daten*multiplier)[nrow(daten):1,])

Upvotes: 2

Related Questions