Anouar
Anouar

Reputation: 301

Problem of the order of visualizing the data in R

Hello I am trying to visualize some data in form of a Boxplot using R. However, the data is shown in a wrong order in the abscissa x. I tried to fix that anhand this function scale_x_discrete(limits=c("Fallow", "Mustard", "Mix4", "Mix12")), this resolve the problem partially because some small letters (a, ac, bc, c) are not correcly ordered. The correct order is (a, ab, bc, c).

This is the code for the plot:

library(tidyverse)
library(lme4)
library(emmeans)
library(multcomp)

# set theme for ggplot
theme_set(theme_bw())
theme_myBW <- theme(axis.title.x = element_text(size = 10, color = "black"), 
                    axis.title.y = element_text(angle = 90, vjust = 1.5, size = 10, color = "black"),
                    axis.text.x = element_text(size = 10, color = "black"), 
                    axis.text.y = element_text(size = 10, color = "black"), 
                    axis.ticks =element_line(colour="black"),
                    strip.text.x = element_text(size = 10, color = "black"),
                    strip.background = element_blank(),
                    panel.border =element_rect(colour="black", fill=NA), 
                    panel.grid.major = element_blank(),
                    panel.grid.minor = element_blank(),
                    plot.title = element_text(size = 12, hjust=0.5),
                    #legend.position=c(0.0,1.0), 
                    #legend.justification=c(0,1), 
                    legend.text = element_text(size = 10),
                    legend.text.align=0,
                    legend.title =  element_text(size = 10), 
                    legend.key = element_rect(colour="white", fill = "white"),
                    legend.key.size = unit(5, "mm"),
                    legend.background = element_blank())

# set vector with colors and label
COL <- c("Fallow" = "slategray", "Mustard" = "red3" , "Mix4" = "orchid3", "Mix12"= "orange4")
SHP <- c("Fallow"=21,"Mustard"=22,"Mix4"=23, "Mix12"=24)


# generate data from csv file
data <- read.csv2("data.csv", as.is=T)
data$NEE <- as.numeric(data$NEE)


lm_NEE <- lmer(NEE ~ cc_variant + (1|Date), data=data)
df_NEE <- cld(emmeans(lm_NEE, specs ="cc_variant"), Letters=letters, sort=FALSE)

# Plot for BFS
ggplot(data, aes(x= cc_variant, y=NEE, fill= cc_variant))+
  geom_boxplot()+
  scale_fill_manual(values = COL, guide=FALSE)+
  geom_text(data= df_NEE ,aes(y=-600,x=cc_variant, label=.group))+
  labs(x="Catch crop variant",  y=expression("NEE (mg CO"[2]~"- C"~m^{-2}~h^{-1}~")"), fill="")+
  theme_myBW+scale_x_discrete(limits=c("Fallow", "Mustard", "Mix4", "Mix12"))

This is the displayed figure:

Displayed plot

And this is how the plot should be displayed:

Should be displayed plot

The data in csv is written in this way:

cc_variant;Date;NEE
Fallow;18.10.2016;52.31861
Fallow;19.10.2016;36.75274
Fallow;24.10.2016;34.59082
Mix4;18.10.2016;-516.86837
Mix12;18.10.2016;-617.11000
Mustard;18.10.2016;-182.24568
Mix4;19.10.2016;-102.63776
Mix12;19.10.2016;-431.55887
Mustard;19.10.2016;-139.04121
Mustard;24.10.2016;-114.09939
Mix12;24.10.2016;-400.21260
Mix4;24.10.2016;-175.33208

Upvotes: 0

Views: 137

Answers (1)

Duck
Duck

Reputation: 39595

Try with this change on your code. You can compute the position for the labels using aggregate() on your data and then merge with the object you use in geom_text() adjusting the position by some scalar (let's say 10). Here the code (I have updated as I did not notice that the issue was about the axis labels. Many thanks and credit to @Ben):

library(tidyverse)
library(lme4)
library(emmeans)
library(multcomp)
# generate data from csv file
data <- read.csv2("data.csv", as.is=T)
data$NEE <- as.numeric(data$NEE)
data$cc_variant<-factor(data$cc_variant, levels = c("Fallow", "Mustard", "Mix4", "Mix12"))
#Model
lm_NEE <- lmer(NEE ~ cc_variant + (1|Date), data=data)
df_NEE <- cld(emmeans(lm_NEE, specs ="cc_variant"), Letters=letters, sort=FALSE)
#Compute Position
Pos <- aggregate(NEE~cc_variant,data,min)
# Plot for BFS
ggplot(data, aes(x= cc_variant, y=NEE, fill= cc_variant))+
  geom_boxplot()+
  scale_fill_manual(values = COL, guide=FALSE)+
  geom_text(data= merge(df_NEE,Pos) ,
            aes(y=NEE-10,x=cc_variant, label=.group))+
  labs(x="Catch crop variant",  y=expression("NEE (mg CO"[2]~"- C"~m^{-2}~h^{-1}~")"), fill="")+
  theme_myBW+scale_x_discrete(limits=c("Fallow", "Mustard", "Mix4", "Mix12"))

Output:

enter image description here

Upvotes: 2

Related Questions