-1

This is my script:

library(readxl)
library(writexl)
library(ggplot2)
library(car)
library(writexl)
library(xlsx)

df1 <- read_excel("C:/Users/seger_rjymtxc/Desktop/Statistiek/Map1.xlsx")

df1a <- df1[df1$Voederbeurt == "Voederbeurt 2",]
df1b <- df1[df1$Voederbeurt == "Voederbeurt 4",]

# Voederbeurt 2
uniekDiernummera <- unique(df1a$Diernummer) 
print(uniekDiernummera)
length(uniekDiernummera) # 14
maxdagen <- 390

df2a <- matrix(0,length(uniekDiernummera),maxdagen) 
for (j in 1:length(uniekDiernummera)){
  dierdata <- which(df1a$Diernummer==uniekDiernummera[j])
  for (k in 1:maxdagen){
    leeftijdVoor <- df1a$Leeftijd[dierdata[max(which(df1a$Leeftijd[dierdata]<k))]]
    leeftijdNa <- df1a$Leeftijd[dierdata[min(which(df1a$Leeftijd[dierdata]>=k))]]
    if (!(is.na(leeftijdNa)|is.na(leeftijdVoor))){
      df2a[j,k] <- (df1a$Gewicht[(df1a$Diernummer==uniekDiernummera[j])&(df1a$Leeftijd==leeftijdNa)]-
                      df1a$Gewicht[(df1a$Diernummer==uniekDiernummera[j])&      (df1a$Leeftijd==leeftijdVoor)])/(leeftijdNa-leeftijdVoor)
    } else {
      df2a[j,k] <- NA
    }
  }
}

rownames(df2a) <- c(uniekDiernummera)

matplot(t(df2a),type="l")

periode <- rbind(c(1,15,29,43,57,71,121,181,241,301),
                 c(14,28,42,56,70,120,180,240,300,390))

df3a <- matrix(0,nrow(df2a),ncol(periode))

for (i in 1:ncol(periode)){
  df3a[,i] <- rowMeans(df2a[,c(periode[1,i]:periode[2,i])],na.rm=TRUE)
}

rownames(df3a) <- c(uniekDiernummera)

matplot(t(df3a),type="l")

colnames (df3a) <- c("Periode 0-14","Periode 15-28","Periode 29-42","Periode 43-56",
                     "Periode 57-70", "Periode 71-120","Periode 121-180","Periode 181-240","Periode 241-300","Periode 301-390")

# voederbeurt 4
uniekDiernummerb <- unique(df1b$Diernummer)
print(uniekDiernummerb)
maxdagen <- 390

df2b <- matrix(0,length(uniekDiernummerb),maxdagen)
for (j in 1:length(uniekDiernummerb)){
  dierdata <- which(df1b$Diernummer==uniekDiernummerb[j])
  for (k in 1:maxdagen){
    leeftijdVoor <- df1b$Leeftijd[dierdata[max(which(df1b$Leeftijd[dierdata]<k))]]
    leeftijdNa <- df1b$Leeftijd[dierdata[min(which(df1b$Leeftijd[dierdata]>=k))]]
    if (!(is.na(leeftijdNa)|is.na(leeftijdVoor))){
      df2b[j,k] <- (df1b$Gewicht[(df1b$Diernummer==uniekDiernummerb[j])&(df1b$Leeftijd==leeftijdNa)]-
                  df1b$Gewicht[(df1b$Diernummer==uniekDiernummerb[j])&    (df1b$Leeftijd==leeftijdVoor)])/(leeftijdNa-leeftijdVoor)
    } else {
      df2b[j,k] <- NA
    }
  }
}


matplot(t(df2b),type="l")

periode <- rbind(c(1,15,29,43,57,71,121,181,241,301),
             c(14,28,42,56,70,120,180,240,300,390))

df3b <- matrix(0,nrow(df2b),ncol(periode))

for (i in 1:ncol(periode)){
  df3b[,i] <- rowMeans(df2b[,c(periode[1,i]:periode[2,i])],na.rm=TRUE)
}

matplot(t(df3b),type="l")

rownames(df2b) <- c(uniekDiernummerb)
rownames(df3b) <- c(uniekDiernummerb)

colnames (df3b) <- c("Periode 0-14","Periode 15-28","Periode 29-42","Periode 43-56",
                 "Periode 57-70", "Periode 71-120","Periode 121-180","Periode 181-240","Periode 241-300","Periode 301-390")

## V2
class(df3a)
class(df1)
df3a_table=as.data.frame(df3a)
df3a_table[,11]=row.names(df3a_table)
df3a_table%>%dplyr::select(-c("Periode 301-390"))%>%pivot_longer(cols = c("Periode 0-14","Periode 15-28","Periode 29-42","Periode 43-56","Periode 57-70","Periode 71-120","Periode 121-180","Periode 181-240","Periode 241-300"),
                                                             names_to = 'Periode',
                                                             values_to = 'Waarden') -> vb2
library(dplyr)

vb2 %>%
  group_by(Periode) %>%
  summarise(min = min(na.exclude(Waarden)), max =   max(na.exclude(Waarden)),gem=mean(na.exclude(Waarden)),sdd=sd(na.exclude(Waarden)))->vb_data

plot=ggplot(vb2,aes(as.character(Periode),as.numeric((Waarden))))
plot+geom_boxplot()

##V4
df3b_table=as.data.frame(df3b)
df3b_table[,11]=row.names(df3b_table)
df3b_table%>%dplyr::select(-c("Periode 301-390"))%>%pivot_longer(cols = c("Periode 0-14","Periode 15-28","Periode 29-42","Periode 43-56","Periode 57-70","Periode 71-120","Periode 121-180","Periode 181-240","Periode 241-300"),
                                                             names_to = 'Periode',
                                                             values_to = 'Waarden') -> vb4
library(dplyr)

vb4 %>%
  group_by(Periode) %>%
  summarise(min = min(na.exclude(Waarden)), max =    max(na.exclude(Waarden)),gem=mean(na.exclude(Waarden)),sdd=sd(na.exclude(Waarden)))->vb4_data

plot=ggplot(vb4,aes(as.character(Periode),as.numeric((Waarden))))
plot+geom_boxplot()

vb2$voederbeurt="2"
vb4$voederbeurt="4"
vb=rbind(vb2,vb4)
plot=ggplot(vb,aes(x=as.character(Periode),y=as.numeric((Waarden)),fill=voederbeurt))
plot+geom_boxplot()

You can see the boxplot in the picture. But I would like to put the values ​​on the x-axis in order.

Periode 0-14 Periode 15-28 Periode 29-42 ....

instead of

Periode 0-14 Periode 121-180 Periode 15-28 ...

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • Welcome to SO, Liane Segers! (1) Questions on SO (and more-so in the [tag:r] tag) should really be reproducible, self-contained, and not humongous. Since you're asking about plotting something, we don't need to know the full journey of getting to the data (most likely ... rarely it's necessary in order to redirect efforts), typically we need sample data and plotting code. Since we don't have access to your xlsx, please provide sample data (`dput(head(x,20))` or similar) or use a public dataset (e.g., `mtcars`, `iris`, `ggplot2::diamonds`) so that we can try your code. – r2evans Feb 11 '23 at 19:03
  • (2) You mentioned an image, but I see nothing in the question. While you are [edit]ing your question to reduce the code (_please_), try again to attach the image. You're new, so you cannot yet make it visible in the question, that's okay, somebody can then verify it and make it visible for you, it's something we do :-). (3) For good discussion on question reproducibility, see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. Thanks! – r2evans Feb 11 '23 at 19:04
  • 4
    Given the question title, though, I suggest one of the following questions (with answers) will give you what you need: https://stackoverflow.com/q/3253641/3358272, https://stackoverflow.com/q/12774210/3358272, https://stackoverflow.com/q/18401931/3358272; ordering with groups https://stackoverflow.com/q/44350031/3358272. ***In short***: almost every question on SO that tags [tag:ggplot2] and asks to change the order of axis/legend labels is resolved by the use of `factor`s. – r2evans Feb 11 '23 at 19:05
  • Does this answer your question? [ggplot2, Ordering y axis](https://stackoverflow.com/questions/18401931/ggplot2-ordering-y-axis) – Andy Baxter Feb 11 '23 at 23:04

1 Answers1

0

Eum I think this might work:

ggplot(vb,aes(x=factor(as.character(Periode),levels=c("Periode 0-14", "Periode 15-28", "Periode 29-42", ....)),y=as.numeric((Waarden)),fill=voederbeurt))

Mark
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 22 '23 at 21:02