# Regarding factoring in R

``````insert_every_n <- function(x, every_n, what = NA) {
n0 <- length(x); n1 <- (n0 - 1L) %/% every_n
every_n <- every_n + 1L
full_seq <- seq_len(n0 + n1)
offset <- (full_seq - 1L) %/% every_n
pos <- which(full_seq %% every_n == 0L)
`[<-`(x[full_seq - offset], pos, what)
}

pat_seq <- 1:15
var_seq <- 6:8
values <- c(4, 2, 2, 3, 8, 7, sample.int(9, 39, replace = T)) # put here values for each (Pat, IT) pair
colors <- c(
"pink2", "yellow3", "burlywood4",
"aquamarine4", "chocolate3", "cornflowerblue",
"brown2", "darkolivegreen1", "darkorchid1",
"firebrick4", "darkslategray", "gray",
"indianred4", "lightgoldenrod4", "ivory3"
)

df <- data.frame(
Patient = paste0("Pat_", rep(pat_seq, each = length(var_seq))),
Var = paste0("IT-", rep(var_seq, length(pat_seq))),
value = values,
col = rep(colors, each = length(var_seq))
)

df\$PatientVar <- with(df, paste(Patient, Var, sep = "_"))
df\$PatientVar <- with(df, factor(PatientVar, levels = PatientVar)) # here we keep the order "as is"

ggplot(data=df,aes(x=PatientVar,y=value,fill=col,group=Patient))+
geom_bar(stat="identity",width=0.9,position=position_dodge(width = 0.9))+
geom_hline(aes(yintercept=70),color="Red")+
labs(y="Display(%)",x="")+
scale_x_discrete(limits = insert_every_n(levels(df\$PatientVar), length(var_seq), ""))+
theme(axis.title.x = element_blank(),
axis.text.x = element_text(size=12,angle=0,vjust = 0.5,face = c( 'bold')),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())+
theme(axis.ticks=element_line(colour = "black"),
panel.border =  element_rect(colour = "black", fill=NA, size=0.5),
panel.background = element_blank(),
axis.title.y  = element_text(size=15,angle=0,vjust = 0.5),
axis.text.y=element_text(size=12,angle=0,vjust = 0.5))+
theme(legend.position = "none")+ scale_fill_identity()+
scale_y_continuous(breaks = seq(0, 9, 1),limits = c(0, 9),expand = c(0, 0)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))
``````

Output

Update

Assume that your dataframe looks like this

``````> df
Var Expected Pat_1 Pat_2 Pat_3 Pat_4 Pat_5 Pat_6 Pat_7 Pat_8 Pat_9 Pat_10 Pat_11 Pat_12 Pat_13 Pat_14 Pat_15
1 IT-6        2     4     7     1     1     1     1     1     7     9      9      7      8      1      6      4
2 IT-7        3     7     8     3     4     9     7     8     1     4      4      9      9      6      9      2
3 IT-8        4     8     9     7     2     7     8     2     8     8      3      5      1      5      7      5
``````

We first need to do some transformation

``````library(dplyr)
library(tidyr)

df1 <-
df %>%
pivot_longer(starts_with("Pat"), "Patient", values_to = "Real") %>%
pivot_longer(c("Expected", "Real"), "group") %>%
arrange(
factor(Patient, unique(Patient)),
factor(Var, unique(Var)),
factor(group, unique(group))
) %>%
mutate(
PatientVar = paste(Patient, Var, sep = "_"),
PatientVar = factor(PatientVar, levels = unique(PatientVar))
)
``````

The resultant dataframe (df1) looks like this

``````> df1
# A tibble: 90 x 5
Var   Patient group    value PatientVar
<chr> <chr>   <chr>    <dbl> <fct>
1 IT-6  Pat_1   Expected     2 Pat_1_IT-6
2 IT-6  Pat_1   Real         4 Pat_1_IT-6
3 IT-7  Pat_1   Expected     3 Pat_1_IT-7
4 IT-7  Pat_1   Real         7 Pat_1_IT-7
5 IT-8  Pat_1   Expected     4 Pat_1_IT-8
6 IT-8  Pat_1   Real         8 Pat_1_IT-8
7 IT-6  Pat_2   Expected     2 Pat_2_IT-6
8 IT-6  Pat_2   Real         7 Pat_2_IT-6
9 IT-7  Pat_2   Expected     3 Pat_2_IT-7
10 IT-7  Pat_2   Real         8 Pat_2_IT-7
# ... with 80 more rows
``````

Then use the following code to ggplot

``````insert_every_n <- function(x, every_n, what = NA) {
n0 <- length(x); n1 <- (n0 - 1L) %/% every_n
every_n <- every_n + 1L
full_seq <- seq_len(n0 + n1)
offset <- (full_seq - 1L) %/% every_n
pos <- which(full_seq %% every_n == 0L)
`[<-`(x[full_seq - offset], pos, what)
}

colors <- c(
"pink2", "red3", "burlywood4",
"aquamarine4", "chocolate3", "cornflowerblue",
"brown2", "darkolivegreen1", "darkorchid1",
"firebrick4", "darkslategray", "gray",
"indianred4", "lightgoldenrod4", "ivory3"
)

ggplot(df1, aes(x = PatientVar, y = value, fill = Patient, group = group, alpha = group)) +
geom_bar(, stat = "identity", width = 0.9, position = position_dodge(width = 0.9)) +
geom_hline(aes(yintercept=70),color="Red")+
labs(y="Display(%)", x="", alpha = element_blank())+
guides(fill = FALSE) +
scale_x_discrete(limits = insert_every_n(levels(df1\$PatientVar), length(unique(df1\$Var)), ""))+
theme(axis.title.x = element_blank(),
axis.text.x = element_text(size=12,angle=0,vjust = 0.5,face = c( 'bold')),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())+
theme(axis.ticks=element_line(colour = "black"),
panel.border =  element_rect(colour = "black", fill=NA, size=0.5),
panel.background = element_blank(),
axis.title.y  = element_text(size=15,angle=0,vjust = 0.5),
axis.text.y=element_text(size=12,angle=0,vjust = 0.5))+
scale_y_continuous(breaks = seq(0, 9, 1),limits = c(0, 9),expand = c(0, 0)) +
scale_fill_manual(values = colors) +
scale_alpha_discrete(range = c(.5, 1)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))
``````

Output