Viridian468
Viridian468

Reputation: 11

Order in bar graph with ggplot changes after adding points

I'm trying to create a bar graph with ggplot including an error bar and points. So far I got everything I wanted but the order of my bars.

This is my code:

#Packages

install.packages("readxl")
library(readxl)

install.packages("ggplot2")
library(ggplot2)

install.packages("RColorBrewer")
library("RColorBrewer")

# Read data

IL10MWSAW <- read_excel("Daten Auswertung 3.xlsx", range = "F4:I20")
IL10MWSAW$Gruppe <- factor(IL10MWSAW$Gruppe, levels = c("K", "CA", "UDCA", "CDCA"))

#These are my mean values and the standard deviation for the different groups and time points
#I did these beforehand as I'm new to R and didn't want to do it here and become confused with it

IL10conc <- read_excel("Daten Auswertung 3.xlsx", range = "B4:D144")
IL10conc$Gruppe <- factor(IL10conc$Gruppe, levels = c("K", "CA", "UDCA", "CDCA"))

#This is my raw data with all the individual measurements

#Bargraph

ggplot() +
  geom_bar(data = IL10MWSAW[1:4,], aes(x = Gruppe, y = MW, fill = Gruppe), stat = "identity", position = "dodge") +
  geom_errorbar(data = IL10MWSAW[1:4,], aes(ymin = MW, ymax = MW + SAW, x = Gruppe, group = Gruppe), stat = "identity", position = "dodge") +
  geom_point(data = IL10conc[1:37,], aes(x = Gruppe, y = Konzentration, group = Gruppe), stat = "identity", position = position_dodge(width = 0.9)) +
  scale_fill_brewer(palette = "Paired") +
  labs(x = "Gruppe", y = "Konzentration [ng/ml]") +
  ylim(0, 0.25) +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5))

Here is my data for the first data set:

structure(list(Woche = c("Woche 0", "Woche 0", "Woche 0", "Woche 0", 
"Woche 4", "Woche 4", "Woche 4", "Woche 4", "Woche 8", "Woche 8", 
"Woche 8", "Woche 8", "Woche 12", "Woche 12", "Woche 12", "Woche 12"
), Gruppe = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L), levels = c("K", "CA", "UDCA", "CDCA"
), class = "factor"), MW = c(0.0466466666666667, 0.024473, 0.0131311111111111, 
0.02584375, 0.049498, 0.0474588888888889, 0.053888, 0.01251375, 
0.0697911111111111, 0.0911754545454545, 0.0165085714285714, 0.0533022222222222, 
0.0697911111111111, 0.0814909090909091, 0.01661625, 0.0272871428571429
), SAW = c(0.0671709706644172, 0.0209704088075449, 0.0106416568780952, 
0.0617515073124304, 0.0481452850235618, 0.055580371860137, 0.0642551637613663, 
0.00959255505587536, 0.0669266819819354, 0.0724393096824696, 
0.017981119198177, 0.0915089160652908, 0.0669266819819354, 0.0711136860884803, 
0.0290434910030261, 0.0331344205292551)), row.names = c(NA, -16L
), class = c("tbl_df", "tbl", "data.frame"))

And here is the data for the second data set:

structure(list(Woche = c("Woche 0", "Woche 0", "Woche 0", "Woche 0", 
"Woche 0", "Woche 0", "Woche 0", "Woche 0", "Woche 0", "Woche 0", 
"Woche 0", "Woche 0", "Woche 0", "Woche 0", "Woche 0", "Woche 0", 
"Woche 0", "Woche 0", "Woche 0", "Woche 0", "Woche 0", "Woche 0", 
"Woche 0", "Woche 0", "Woche 0", "Woche 0", "Woche 0", "Woche 0", 
"Woche 0", "Woche 0", "Woche 0", NA, "Woche 0", "Woche 0", "Woche 0", 
"Woche 0", "Woche 0"), Gruppe = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, NA, 4L, 4L, 4L, 4L, 4L
), levels = c("K", "CA", "UDCA", "CDCA"), class = "factor"), 
    Konzentration = c(0.05305, 0.00159, 0.04927, 0.21846, 0.00042, 
    0.01288, 0.03776, 0.02776, 0.01863, 0.00295, 0.02599, 0.05175, 
    0.02299, 0.04392, 0.00262, 0.00545, 0.00192, 0.05562, 0.03152, 
    0.00848, 0.01876, 0.00568, 0.03107, 0.02386, 0.00933, 0.01943, 
    0, 0.00157, 0.00307, 0.02214, 0.17756, NA, 7e-04, 0.00031, 
    0.00286, 0.00011, 0)), row.names = c(NA, -37L), class = c("tbl_df", 
"tbl", "data.frame"))

Now the problem is that eventhough I factored the variable "Gruppe" in the order I want for both my data sets, when I run the code it is in the wrong order and looks like this:

My plot

I tried looking to find the location of my problem and ran my code line for line to see when it changes. When I run only the ggplot with the bars and errorbars the order is correct.

So only this part (including all the code beforehand):

ggplot() +
  geom_bar(data = IL10MWSAW[1:4,], aes(x = Gruppe, y = MW, fill = Gruppe), stat = "identity", position = "dodge") +
  geom_errorbar(data = IL10MWSAW[1:4,], aes(ymin = MW, ymax = MW + SAW, x = Gruppe, group = Gruppe), stat = "identity", position = "dodge")

gives me this graph: Plot with only bar and errorbar

Next I tried changing the order in the factor function to see if there is an influence. If I changed it only for the first data set:

IL10MWSAW <- read_excel("Daten Auswertung 3.xlsx", range = "F4:I20")
IL10MWSAW$Gruppe <- factor(IL10MWSAW$Gruppe, levels = c("CA", "K", "UDCA", "CDCA"))

IL10conc <- read_excel("Daten Auswertung 3.xlsx", range = "B4:D144")
IL10conc$Gruppe <- factor(IL10conc$Gruppe, levels = c("K", "CA", "UDCA", "CDCA"))

and then run the whole code then the order of the bars didn't change but the order and the colours in the legend of the plot changed: Plot with changed order in factor function

If I changed it only for the second set:

IL10MWSAW <- read_excel("Daten Auswertung 3.xlsx", range = "F4:I20")
IL10MWSAW$Gruppe <- factor(IL10MWSAW$Gruppe, levels = c("K", "CA", "UDCA", "CDCA"))

IL10conc <- read_excel("Daten Auswertung 3.xlsx", range = "B4:D144")
IL10conc$Gruppe <- factor(IL10conc$Gruppe, levels = c("CA", "K", "UDCA", "CDCA"))

then there is no change and it looks the same as in the beginning.

Does anyone know how to get it in the correct order?

Thank you for your help!

Upvotes: 0

Views: 41

Answers (1)

Viridian468
Viridian468

Reputation: 11

I found my problem.

Apparantly the missing values in my second data set where a problem for R. When I included the line IL10conc <- na.omit(IL10conc) then everything worked like it was supposed to.

Thanks to everyone who commented a possible solution.

Upvotes: 1

Related Questions