R Lattice Bar: Choose the order of the bars?

My data is as follows:

service,rating_1,rating_2,rating_3,rating_4,rating_5
renew_patent,0,0,1,2,11
apply_benefit,21,20,121,828,1744
apply_employment_tribunal,0,0,0,0,0

I want R to print me a histogram for each row, and the columns as columns of the histogram.

I still have this:

require(lattice)
data <- read.csv("test.csv", header = TRUE)
colors = c('red', 'orange', 'yellow', 'blue', 'green')
barchart(rating_1+rating_2+rating_3+rating_4+rating_5 ~ service, data=data, 
  auto.key=list(space='right'), scales=list(x=list(rot=45)), 
  ylab="Percentage of total", col=colors)

It works, but it prints a histogram with stripes in alphabetical order, and not in the order specified in the CSV file.

How can I change this so that the bars are in order in the CSV file first renew_patent?

+4
source share
1 answer

You need to specify the order of the levels in the data itself, then it barchartwill do what you want.

One option is to run the code, for example:

data$service <- factor(data$service, levels=unique( as.character(data$service) ) )

before calling barchart.

+6
source

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


All Articles