how to plot multiple boxplots from emmeans output - r

How can I plot with ggplot2 multiple boxplots (in the same figure) to illustrate my model's pairwise comparisons?
my model and pairwise comparisons with emmeans:
mod1 <- lmer(CONT_Y ~ MY_GROUP * YEAR + (1|ID), data = dfModels)
group <- emmeans(mod1,~ MY_GROUP|YEAR)
year <- emmeans(mod1,~YEAR|MY_GROUP)
my_pairs <- data.frame(group_p) %>% full_join(data.frame(year_p))
my_pairs
contrast YEAR MY_GROUP estimate SE df t.ratio p.value
1 L1 - L2 2020 <NA> -0.91 0.53 60 -1.73 0.09
2 L1 - L2 2021 <NA> -0.31 0.53 60 -0.59 0.56
3 YEAR2020 - YEAR2021 <NA> G1 -1.14 0.53 60 -2.16 0.03
4 YEAR2020 - YEAR2021 <NA> G2 -0.54 0.53 60 -1.02 0.31
Desired output: something like this
How can I plot these tests with multiple boxplots in ggplot2 ?
data (and more details on the model and on how I obtained the above effects here):
data <- structure(list(PARTICIPANTS = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L,
10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 17L,
17L, 17L, 17L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 20L, 20L,
20L, 20L, 21L, 21L, 21L, 21L), CONT_Y = c(19.44, 20.07, 19.21,
16.35, 11.37, 12.82, 19.42, 18.94, 19.59, 20.01, 19.7, 17.92,
18.78, 19.21, 19.27, 18.46, 19.52, 20.02, 16.19, 19.97, 13.83,
15.93, 14.79, 21.55, 18.8, 19.42, 19.27, 19.37, 17.14, 14.45,
17.63, 20.01, 20.28, 17.93, 19.36, 20.15, 16.06, 17.04, 19.16,
20.1, 16.44, 18.39, 18.01, 19.05, 18.04, 19.69, 19.61, 16.88,
19.02, 20.42, 18.27, 18.43, 18.08, 17.1, 19.98, 19.43, 19.71,
19.93, 20.11, 18.41, 20.31, 20.1, 20.38, 20.29, 13.6, 18.92,
19.05, 19.13, 17.75, 19.15, 20.19, 18.3, 19.43, 19.8, 19.83,
19.53, 16.14, 21.14, 17.37, 18.73, 16.51, 17.51, 17.06, 19.42
), CATEGORIES = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("A",
"B"), class = "factor"), MY_GROUP = structure(c(1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), .Label = c("G1", "G2"), class = "factor")), row.names = c(NA,
-84L), class = c("tbl_df", "tbl", "data.frame"))
### rename column:
data <- data %>% rename(., YEAR = CATEGORIES)

Related

geom_text not matching group aes

I'm pretty sure this is a silly question, but I've been stuck with it for a while now. I want to match the geom_text() to my means from stat_summary according to My_group
the plot:
my code:
### getting model's predictions:
mod1 <- lmer(MY_CONT ~ YEAR * GROUP_2 + (1|ID), data = data, REML = FALSE)
###
data$predictions <- predict(mod1)
### put model's predictions in a sep df:
dfPred <- data %>% group_by(YEAR, MY_GROUP) %>% rstatix::get_summary_stats(predictions)
### check it:
dfPred %>% select(YEAR, MY_GROUP, variable, n, mean)
# A tibble: 4 x 5
YEAR MY_GROUP variable n mean
<fct> <fct> <chr> <dbl> <dbl>
1 A G1 predictions 21 17.6
2 A G2 predictions 21 18.5
3 B G1 predictions 21 18.8
4 B G2 predictions 21 19.1
### the model:
data %>%
mutate_if(is.numeric, round, 2) %>%
ggplot(., aes(x = YEAR, y = predictions)) +
stat_boxplot(aes(x = YEAR, fill = MY_GROUP), geom = "errorbar",
width = 0.15, position = position_dodge(.75)) +
geom_boxplot(aes(fill = MY_GROUP),
outlier.colour = "lightgrey",
outlier.shape = 19,
outlier.size= 2, notch = T) +
geom_text(data = dfPred,
aes(label = round(mean, 2),
y = round(mean, 2) + 0.8)) +
stat_summary(aes(group = MY_GROUP),
fun = mean, geom = "point",
shape = 20, size= 3, color= "black",
position = position_dodge(.75))
Warning message:
In stat_boxplot(aes(x = YEAR, fill = MY_GROUP), geom = "errorbar", :
Ignoring unknown aesthetics: fill
My Question:
How can I put the means' labels according to their group ? (MY_GROUP) ?
side question 1 : I get the fill warning, but it doesn't work without fill
side question 2 : The bars are showing the standard error aroung the means, right? not the 95%
data (and more details on the model and on how I obtained the above effects here):
data <- structure(list(PARTICIPANTS = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L,
10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 17L,
17L, 17L, 17L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 20L, 20L,
20L, 20L, 21L, 21L, 21L, 21L), CONT_Y = c(19.44, 20.07, 19.21,
16.35, 11.37, 12.82, 19.42, 18.94, 19.59, 20.01, 19.7, 17.92,
18.78, 19.21, 19.27, 18.46, 19.52, 20.02, 16.19, 19.97, 13.83,
15.93, 14.79, 21.55, 18.8, 19.42, 19.27, 19.37, 17.14, 14.45,
17.63, 20.01, 20.28, 17.93, 19.36, 20.15, 16.06, 17.04, 19.16,
20.1, 16.44, 18.39, 18.01, 19.05, 18.04, 19.69, 19.61, 16.88,
19.02, 20.42, 18.27, 18.43, 18.08, 17.1, 19.98, 19.43, 19.71,
19.93, 20.11, 18.41, 20.31, 20.1, 20.38, 20.29, 13.6, 18.92,
19.05, 19.13, 17.75, 19.15, 20.19, 18.3, 19.43, 19.8, 19.83,
19.53, 16.14, 21.14, 17.37, 18.73, 16.51, 17.51, 17.06, 19.42
), CATEGORIES = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("A",
"B"), class = "factor"), MY_GROUP = structure(c(1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), .Label = c("G1", "G2"), class = "factor")), row.names = c(NA,
-84L), class = c("tbl_df", "tbl", "data.frame"))
### rename column:
data <- data %>% rename(., YEAR = CATEGORIES)

Multi level pie chart ggplot: Label overlap and legend

I have the following dataset:
data <- structure(list(Year = structure(c(1L, 2L, 1L, 2L, 2L, 1L, 3L, 1L, 3L, 1L, 2L, 2L, 3L, 2L, 3L,
1L, 3L, 2L, 2L, 2L, 3L, 1L, 2L, 3L, 3L, 3L, 2L, 1L, 3L, 1L,
1L, 2L, 1L, 2L, 3L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 3L,
3L, 2L, 3L, 2L, 1L, 1L, 2L, 2L, 1L),
.Label = c("2013", "2014", "2015"),
class = "factor"),
Place = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L),
.Label = c("Inside", "Outside"),
class = "factor"),
Variable = structure(c(6L, 4L, 8L, 13L, 16L, 11L, 12L, 13L, 4L, 10L, 10L, 11L,
1L, 3L, 13L, 7L, 11L, 7L, 6L, 2L, 6L, 1L, 1L, 7L, 5L,
3L, 14L, 3L, 14L, 2L, 9L, 6L, 6L, 9L, 2L, 5L, 9L, 5L,
9L, 9L, 15L, 1L, 13L, 3L, 6L, 3L, 3L, 9L, 15L, 1L, 13L,
1L, 13L, 15L),
.Label = c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8",
"Y1", "Y2", "Y3", "Y4", "Y5", "Y6", "Y7", "Y8"),
class = "factor"),
Group = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L,
2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L),
.Label = c("Var1", "Var2"),
class = "factor"),
Percent = c(0.2, 0.3, 0.4, 0.5, 0.5, 0.6, 0.7, 0.7, 1.3, 1.6, 1.9, 1.9, 2.3, 2.7,
2.9, 3.4, 3.7, 4.5, 4.7, 5.3, 5.7, 6.2, 7.6, 7.9, 10.6, 10.7, 12.5,
13.3, 14.4, 15.4, 15.8, 16.9, 17.7, 19.6, 20.5, 24.8, 25.3, 30.4, 31,
36.8, 41.6, 43.9, 43.9, 44.2, 45.4, 51.8, 52.8, 56.1, 57.4, 68.9, 68.9,
80.4, 80.4, 81.5)),
class = "data.frame", row.names = c(NA, -54L))
I would really like to display the data in a a multilevel like this:
I tried it by doing:
library(ggplot2)
ggplot(data, aes(x = Group, y = Percent, fill = Variable)) +
geom_bar(stat = "identity", position = "fill") +
facet_grid(Year ~ Place) +
geom_text(aes(label= paste(Percent, "%", Variable)) ,
position = position_fill(0.9), size = 3)+
coord_polar(theta = "y")
But because some percentages are very low, the layers overlap. I would like to either place the labels outside like the example if that's possible.
I have looked at the other forum topics, but because my data is structured in a different way I wasn't able to translate that for me. The other problem is, is that this is just an example but my data input is actually dependent on he Shiny input. So fixing specific angles for this example also doesn't work.
I would be very grateful if anyone could help me.

ordered logistic regression with a four way interaction in R

I have been trying to perform an ordered logistic regression with a four-way interaction and get the error message:
Error in Design(eval.parent(m)) : interaction term not second or
third order
The design is:
Dependent variable:
Developmental stage - 5 levels numbered 1-5
Independent variables:
Age - 4 different age points for which data was collected;
Ancestry - 2 categories numbered 1 & 2;
Rearing environment - 2 categories numbered 1 & 2;
Current environment - 2 categories numbered 1 & 2
As there is the potential for pseudo-replication the model is run clustered by "Colony".
As such, the code that I have been trying to use is:
library(rms)
Data$Ancestry <- factor(Data$Ancestry)
Data$Rearing <- factor(Data$Rearing)
Data$Queenless <- factor(Data$Queenless)
m <- lrm(Level ~ Age * Ancestry * Rearing * Queenless, x=T, y=T, dat = Data)
robcov(m, cluster = Data$Colony)
I'm assuming that the error message means that lrm doesn't support a 4-way interaction. Is there another way of doing this? I'm having no luck searching for alternate solutions online and I know I had problems trying to cluster using the polr function.
Many thanks for your help.
Here is my data:
structure(list(Bee.Age = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L), Colony = structure(c(1L, 1L, 1L, 1L, 5L, 5L,
5L, 5L, 2L, 2L, 2L, 2L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 9L, 9L,
9L, 9L, 2L, 2L, 2L, 2L, 10L, 10L, 10L, 10L, 3L, 3L, 3L, 3L, 7L,
7L, 7L, 7L, 4L, 4L, 4L, 4L, 8L, 8L, 8L, 8L, 3L, 3L, 3L, 3L, 7L,
7L, 7L, 7L, 4L, 4L, 4L, 4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1L, 4L, 3L, 4L,
2L, 5L, 5L, 4L, 3L, 5L, 4L, 1L, 5L, 5L, 5L, 5L, 4L, 5L, 5L,
5L, 5L, 2L, 5L, 4L, 4L, 5L, 3L, 5L, 4L, 4L, 5L, 4L, 5L, 2L,
4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L), X = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
)), .Names = c("Bee.Age", "Colony", "Ancestry", "Queenless",
"Rearing", "LevelOA", "X"), row.names = c(NA, -320L), class = "data.frame")
This is only a partial answer.
If you need to fit an ordinal model with clustered data, you can use the ordinal package:
Process data:
library(ordinal)
Data <- transform(Data,
Ancestry=factor(Ancestry),
Rearing=factor(Rearing),
Queenless=factor(Queenless),
LevelOA=ordered(LevelOA))
The only special point here is that the response variable needs to be an ordered factor (ordered()).
There are two versions of clustered cumulative-link models in ordinal (you can read about their options in the package's help files):
c1 <- clmm(LevelOA ~ Bee.Age*Ancestry*Rearing*Queenless + (1|Colony),
dat = Data)
c2 <- clmm2(ordered(LevelOA) ~ Bee.Age*Ancestry*Rearing*Queenless,
random = Colony, data= Data, Hess=TRUE)
Both of these give estimates, but not standard deviations - I think the model is too complex and something is going wrong with the SD computation, but it would take more work (maybe a lot more) to figure it out. The estimated among-colony variance is also nearly/effectively zero with both models (std. dev approx. 2.7e-5), which implies that As a preliminary, you can use MASS::polr to fit the model without clustering:
c0 <- polr(LevelOA ~ Bee.Age * Ancestry * Rearing * Queenless,
dat = Data)
because the estimated among-colony variance is zero, this gives the same coefficient estimates as the ordinal functions.

Plotting lmer model without covariance matrix

I am trying to plot a number of lmer models for a paper. I had to simplify the random effect structure by dropping the correlation between the random slopes and intercept (Barr et al., 2013). However, when I try to plot using the sjp.lmer funtion, I get the following error:
Error in array(NA, c(J, K)) : 'dims' cannot be of length 0
In addition: Warning message:
In ranef.merMod(object, condVar = TRUE) :
conditional variances not currently available via ranef when there are multiple terms per factor
Is there a potential work-around for this? Any help would be greatly appreciated.
Hi Ben,
Here is some of the data I am working with:
> dput(df)
structure(list(Subject = c(1L, 2L, 3L, 5L, 6L, 6L, 6L, 7L, 7L,
7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 11L, 11L, 11L, 12L, 12L,
13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 16L, 17L, 17L, 17L, 18L,
18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L, 23L,
24L, 24L, 25L, 25L, 25L, 26L, 26L, 26L, 27L, 27L, 28L, 28L, 29L,
29L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L,
54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L,
67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L,
80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L,
93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L,
105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L,
116L), A = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1",
"2"), class = "factor"), B = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L), .Label = c("1", "2", "3"), class = "factor"), C = c(9.58,
9.75, 15, 10.75, 13.3, 14.42, 15.5, 9.25, 10.33, 11.33, 9.55,
11, 11.92, 14.25, 15.5, 16.42, 14.92, 16.17, 10.83, 11.92, 12.92,
7.5, 8.5, 10.33, 11.25, 13.08, 13.83, 14.92, 15.92, 9.58, 14.83,
11.92, 8.33, 9.5, 10.5, 6.8, 7.92, 9, 13.5, 10.92, 10, 11, 13,
15.58, 12.92, 11.8, 5.75, 6.75, 7.83, 11.12, 12.25, 12.08, 13.08,
14.58, 8.08, 9.17, 10.67, 10.6, 12.67, 7.83, 8.83, 9.67, 10.58,
11.75, 7, 17.17, 11.25, 13.75, 11.83, 16.92, 8.83, 7.07, 7.83,
15.08, 15.83, 16.67, 18.87, 11.92, 12.83, 7.83, 12.33, 10, 11.08,
12.08, 15.67, 11.75, 15, 14.308, 15.9064, 16.161, 16.9578, 8.90197,
16.2897, 9.05805, 10.5969, 5.15334, 9.1046, 14.1019, 18.9736,
10.9447, 14.5455, 16.172, 6.65389, 11.3171, 12.2864, 17.9929,
10.5778, 16.9195, 7.6, 7.8, 7.2, 16.7, 17, 16.5, 17, 15.1, 16,
16.4, 13.8, 13.8, 14.5, 16.1, 15.8, 15, 14.1, 15, 14.7, 15, 14.5,
10.8, 11.4, 11.3, 10.9, 11.2, 9.3, 10.8, 9.7, 8, 8.2, 8.2, 17.5,
12.6, 11.6, 10.8, 11.8, 12.3, 16.3, 17.1, 9.626283368, 14.6,
13.7), D = structure(c(2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1",
"2"), class = "factor"), Frontal_FA = c(0.4186705, 0.4151535,
0.4349945, 0.4003705, 0.403488, 0.407451, 0.3997135, 0.38826,
0.3742275, 0.3851655, 0.3730715, 0.3825115, 0.3698805, 0.395406,
0.39831, 0.4462415, 0.413532, 0.419088, 0.4373975, 0.4633915,
0.4411375, 0.3545255, 0.389322, 0.349402, 0.352029, 0.367792,
0.365298, 0.3790775, 0.379298, 0.36231, 0.3632755, 0.357868,
0.3764865, 0.3726645, 0.351422, 0.3353255, 0.334196, 0.3462365,
0.367369, 0.3745925, 0.3610755, 0.360576, 0.357035, 0.3554905,
0.3745615, 0.38828, 0.3293275, 0.3246945, 0.3555345, 0.375563,
0.38116, 0.387508, 0.357707, 0.413193, 0.3658075, 0.3776355,
0.362678, 0.3824945, 0.3771, 0.375347, 0.362468, 0.367618, 0.3630925,
0.3763995, 0.359458, 0.3982755, 0.3834765, 0.386135, 0.3691575,
0.388099, 0.350435, 0.3629045, 0.3456775, 0.4404815, 0.4554165,
0.425763, 0.4491515, 0.461206, 0.453745, 0.4501255, 0.4451875,
0.4369835, 0.456838, 0.437759, 0.4377635, 0.44434, 0.4436615,
0.437532, 0.4335325, 0.4407995, 0.470447, 0.4458525, 0.440322,
0.4570775, 0.4410335, 0.436045, 0.4721345, 0.4734515, 0.4373905,
0.4139465, 0.440213, 0.440281, 0.425746, 0.454377, 0.4457435,
0.488561, 0.4393565, 0.4610565, 0.3562055, 0.381041, 0.353253,
0.4265975, 0.4069595, 0.40092, 0.4261365, 0.429605, 0.425479,
0.4331755, 0.3981285, 0.4206245, 0.3798475, 0.3704155, 0.395192,
0.404436, 0.4148915, 0.416144, 0.384652, 0.3916045, 0.41005,
0.3940605, 0.3926085, 0.383909, 0.391792, 0.372398, 0.3531025,
0.414441, 0.404335, 0.3682095, 0.359976, 0.376681, 0.4173705,
0.3492685, 0.397057, 0.3940605, 0.398825, 0.3707115, 0.400228,
0.3946595, 0.4278775, 0.384037, 0.43577)), .Names = c("Subject",
"A", "B", "C", "D", "Frontal_FA"), class = "data.frame", row.names = c(NA,
-151L))
Here is the code that I am running
lmer fit
FA <- lmer(Frontal_FA ~ poly(C) + A + B + D + (poly(C)||Subject), data = df)
plot lmer fit
sjp.lmer(FA)
Thanks for your help.
sjp.lmer, by default, plots the random effects of a model. However, it plots random effects (BLUPs) with confidence intervals, using the arm:se.ranef function. This function causes the first error message you get:
arm::se.ranef(FA)
> Error in array(NA, c(J, K)) : 'dims' cannot be of length 0
Then, the se.ranef functions calls the lme4::ranef function with argument condVar = TRUE, which is not yet implemented for specific conditions (like yours) in lme4. Hence you get the additional warning
In ranef.merMod(object, condVar = TRUE) :
conditional variances not currently available via ranef when there are multiple terms per factor
If you are especially interested in plotting the random effects, you could use the lme4-implemented dotplot-function:
lattice::dotplot(ranef(FA))
If you are interested in any other plot type (fixed effects, marginal effects, predictions, ...), see ?sjp.lmer or some examples at his page.
Edit
If you don't mind installing from GitHub (devtools::install_github("sjPlot/devel"), I have committed a small update, so you can use show.ci = FALSE to avoid computing confidence intervals for random effects:
sjp.lmer(FA, type = "re", show.ci = F, sort.est = "(Intercept)")

Ordered and Sorted ggplot Bar Graph

I am trying to create a bar graph of an interaction between 'Treatment' and 'Day' from my data set. I want 'Treatment' to be sorted in the following order: 'Before', 'During', and 'After' and I would also like 'Day' 1 and 2 'Before', 'During', and 'After' next to each other, rather than having each 'Day' grouped together. Here is my data set:
structure(list(Name = structure(c(9L, 9L, 5L, 5L, 14L, 14L, 15L,
15L, 1L, 1L, 7L, 7L, 12L, 12L, 2L, 2L, 11L, 11L, 16L, 16L, 13L,
13L, 6L, 6L, 8L, 8L, 3L, 3L, 4L, 4L, 10L, 10L, 9L, 9L, 5L, 5L,
14L, 14L, 15L, 15L, 1L, 1L, 7L, 7L, 12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1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("After",
"Before", "During"), class = "factor"), Behavior = c(3040L,
3219L, 2378L, 3529L, 3135L, 3702L, 2868L, 3035L, 1614L, 833L,
1255L, 1224L, 1569L, 1433L, 1567L, 1448L, 1197L, 1619L, 1543L,
1309L, 1738L, 1378L, 1362L, 1603L, 1109L, 1073L, 570L, 822L,
815L, 1166L, 0L, 835L, 3060L, 2815L, 2875L, 3394L, 3009L,
3274L, 2350L, 2455L, 972L, 815L, 983L, 1033L, 1520L, 1477L,
1461L, 1395L, 1242L, 1368L, 1637L, 1275L, 1646L, 1546L, 1064L,
971L, 596L, 1461L, 1276L, 1272L, 872L, 1026L, 1249L, 592L,
2277L, 3022L, 2997L, 3495L, 2591L, 3447L, 2479L, 2701L, 175L,
452L, 1287L, 1355L, 1252L, 1501L, 1371L, 1147L, 1465L, 1003L,
1510L, 0L, 1744L, 1026L, 959L, 898L, 1529L, 1092L, 417L,
289L, 40L, 762L, 1495L, 896L)), .Names = c("Name", "Day",
"Treatment", "Behavior"), class = "data.frame", row.names = c(NA,
-96L))
Here is the script I am using to produce my current figure:
ggplot(perch1, aes(x = interaction(Treatment,Day), y = Behavior, fill = factor(Day)))
An alternative would be to use facet wrap to plot each treatment in a separate panel.
ggplot(df, aes(x = factor(Day), y = Behavior, fill = factor(Day))) +
geom_bar(stat = 'identity') + facet_wrap(~Treatment, nrow = 1)
This won't label your interaction on the X-axis, but instead puts it in the panel title.
One option is to just add a new column to your data.frame and manually specify the order of the levels, rather than messing around with interaction:
library(ggplot2)
##
df$Group <- factor(
paste0(df$Day," : ",df$Treatment),
paste0(rep(1:2,3)," : ",
rep(c("Before","During","After"),each=2)))
##
R> ggplot(
df,
aes(x=Group,
y=Behavior,
fill=factor(Day)))+
geom_bar(stat='identity')+
scale_x_discrete(
labels=paste0("Day ",levels(df$Group)))

Resources