0
votes

I have a data frame tbl with 252 obs of 8 variables.

> str(CE0008)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   252 obs. of  8 variables:
 $ Period start time: POSIXct, format: "2018-02-28" "2018-02-28" "2018-02-28" "2018-02-28" ...
 $ WBTS name        : chr  "CE0008" "CE0008" "CE0008" "CE0008" ...
 $ WBTS ID          : num  27 27 27 27 27 27 27 27 27 27 ...
 $ WCEL name        : chr  "CE0008U09C3" "CE0008U21B1" "CE0008U21B3" "CE0008U21C2" ...
 $ WCEL ID          : num  33 2 22 13 32 3 23 1 11 31 ...
 $ PRACHDelayRange  : num  4 4 4 4 4 4 4 4 4 4 ...
 $ class            : Ord.factor w/ 21 levels "Class 0"<"Class 1"<..: 1 1 1 1 1 1 1 1 1 1 ...
 $ count            : num  22177 37507 37580 24066 6029 ...

I wish to produce a bar plot where the x axis contains the class variable where class variables in ordered from Class 0 through to Class 20. The height of the bars is given by the count variable and the bars are coloured by WCEL name.

From the str of my data frame tbl the class variable is an ord. factor but for some reason when I plot the data the order doesn't carry over and similarly with fill of the colour by 'WCEL name'. Any pointers would be greatly appreciated.

ggplot(data = CE0008, aes(x = class, y = count, fill = 'WCEL name')) + geom_col()

enter image description here

1
I don't think ggplot2 can use ordered factors. So your solution would either to be unclass and then convert the variable to a regular factor (setting the levels in the order that you want), or set the order using ggplot2::scale_x_discrete() . See this SO questionJacob Nelson
Have a look hereKamil

1 Answers

-1
votes

data$class <- factor(data$class, as.character(data$class))

your ggplot call goes here