Problem with reordering levels of data and making barplots.

Hi, R noob here with a question on how to reorder levels of your data, and additionally how make a barplot using that reordered data.

The data is quite simple: I have 4 conditions (Control, 5 w/v%, 10 w/v% and 15 w/v%) and 3 replicates per condition.

Condition Percentage
Control 98
Control 103,297302
Control 98,36626952
5 w/v % 13,98229573
5 w/v % 16,73766467
5 w/v % 24,49963936
10 w/v % 12,78267422
10 w/v % 18,00019486
10 w/v % 11,25992538
15 w/v % 13,97593758
15 w/v % 12,2639796
15 w/v % 3,216802747

I load in my data:

data <- read.csv("DataLS174T.csv", sep = ";", dec = ",")

I check how my data looks, this is the output (looks fine):

> data
   Condition Percentage
1    Control  98.000000
2    Control 103.297302
3    Control  98.366270
4   5 w/v %   13.982296
5   5 w/v %   16.737665
6   5 w/v %   24.499639
7   10 w/v %  12.782674
8   10 w/v %  18.000195
9   10 w/v %  11.259925
10  15 w/v %  13.975938
11  15 w/v %  12.263980
12  15 w/v %   3.216803

Then I look at the levels:

levels(data$Condition)

And I get the following in console output:

> levels(data$Condition)
[1] "10 w/v %" "15 w/v %" "5 w/v % " "Control"

I want to reorder my levels so that they are in the correct order which I do by using:

data$Condition <- factor(data$Condition, levels=c("Control", "5 w/v %", "10 w/v %", "15 w/v %"))

It seems to work, I check the levels again and the output gives:

> levels(data$Condition)
[1] "Control"  "5 w/v %"  "10 w/v %" "15 w/v %"

I then make the graph again using the same code, and all of a sudden I get:

my data has also changed:

> data
   Condition Percentage
1    Control  98.000000
2    Control 103.297302
3    Control  98.366270
4       <NA>  13.982296
5       <NA>  16.737665
6       <NA>  24.499639
7   10 w/v %  12.782674
8   10 w/v %  18.000195
9   10 w/v %  11.259925
10  15 w/v %  13.975938
11  15 w/v %  12.263980
12  15 w/v %   3.216803

I truly do not know how I can fix this problem. It's weird to me that the 5 w/v% category just disappears.
Any advice?

In the data it looks like there is a trailing space, where your defined levels do not.

You can either put a space in the levels definition, define the levels using the data, e.g. factor(data$Condition,levels = sort(unique(data$Condition))) or modify the original input data.

stringr::str_trim and stringr::str_squish are both helpful functions for approaching white space in text.

Indeed, I totally missed the extra space there. It's working now, thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.