geom_text not matching group aes - r

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)

Related

how to plot multiple boxplots from emmeans output

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)

Axis tick labels won't show up in ggplot facet

I am struggling to get my x-axis tick labels to show up as the day the sample was taken. I am also struggling with my grouping reordered, currently, it is showing up as Afternoon coming before Pre-Dawn, I would like Pre-Dawn to be first in order.
Data
http://www.sharecsv.com/s/f7079be36f5fc5035029ae105f96d560/VR_Sonde_Data_May_2017%20(1).csv
DO=read.csv("VR_Sonde_Data_May_2017 (1).csv")
DOmelt <- melt(DO, id.vars=c("Month", "Day", "TimeofDay"), measure.vars = c("AverageDO"))
ggplot(DOmelt, aes((x=Day), group=interaction(Month, TimeofDay), fill=TimeofDay)) +
geom_bar(aes(y=value), stat="identity", position=position_dodge()) +
facet_grid(~Month, scales = "free_x") +
ggtitle("Dissolved Oxygen in Ventura River") +
labs(subtitle = "2017") +
theme(plot.title = element_text(size=30, face="bold", vjust=2, hjust=.5), plot.subtitle = element_text(size=20, face="bold", vjust=2, hjust=.5))+
scale_x_discrete("day") +
scale_y_continuous(name ="Average Dissolved Oxygen")+
theme(axis.text.x =element_text(angle=90))
You can use the following code
library(tidyverse)
DOmelt %>%
arrange(AverageDO) %>%
mutate(TimeofDay = factor(TimeofDay, levels=c("Pre-Dawn", "Afternoon"))) %>%
ggplot(aes(x=Day, y=AverageDO, group=interaction(Month, TimeofDay), fill=TimeofDay)) +
geom_bar(position=position_dodge(), stat="identity") +
facet_grid(~Month, scales = "free_x") +
ggtitle("Dissolved Oxygen in Ventura River") +
labs(subtitle = "2017") +
theme(plot.title = element_text(size=30, face="bold", vjust=2, hjust=.5), plot.subtitle = element_text(size=20, face="bold", vjust=2, hjust=.5))+
xlab("Day") +
scale_y_continuous(name ="Average Dissolved Oxygen")+
theme(axis.text.x =element_text(angle=90))
Data
DOmelt = structure(list(Month = 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, 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("May", "September"), class = "factor"),
Day = c(11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L,
16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L,
22L, 22L, 23L, 23L, 24L, 24L, 25L, 6L, 6L, 7L, 7L, 8L, 8L,
9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L,
15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L), TimeofDay = structure(c(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, 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("Afternoon",
"Pre-Dawn"), class = "factor"), AverageDO = c(6.99, 12.24,
6.61, 12.05, 6.51, 11.94, 6.63, 12.12, 6.67, 12.28, 6.68,
12.14, 6.87, 11.94, 6.64, 10.77, 6.47, 9.3, 6.21, 10.71,
5.92, 10.95, 5.85, 11.46, 5.98, 11.31, 6.12, 10.27, 6.38,
6.61, 8.97, 6.88, 9.08, 7.01, 9.18, 7.2, 9.39, 7.25, 9.61,
6.97, 8.87, 6.77, 8.8, 6.88, 8.92, 7.1, 9.25, 7.34, 9.26,
7.44, 9.46, 7.59, 9.66, 7.74, 9.72, 7.77, 9.54, 7.71)), class = "data.frame", row.names = c(NA,
-58L))

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.

How to make create two y-axis labels with a grid of facets with a single x-axis label

I have been struggling with ggplot to display these plots how I would like. My data have 2 factors, quarter and species. Station will be on the x-axis, value on the y-axis, and the constituent will be used with the facet_wrap. I want quarter differentiated with shapes, and species with colors.
The issue is I'm trying to replicate a figure done in SigmaPlot. It is 4x4 grid of plots, with the first two rows of the first column are empty, to allow for the placement of the legend. My original plan was to have two separate facets made using facet-wrap, and combine those, however, this doesn't maintain the 4x4 arrangement, it transforms it into a 1x2, which ruins alignment of plots and shrinks the larger faceted grid.
My next thought was to create each plot individually, then arrange them in a grid using cowplot. This presents the plots how I'd like them arranged, but I can't figure out how to have two y-axis labels, due to different units. One label would be centered on the two leftmost plots, and one centered on the left of the next column of 4 plots.
I'm trying to use this code (just copy the example data below, and run):
library(ggplot)
library(gridExtra)
test.data1 <- test.data[1:95, ]
test.data2 <- test.data[96:111, ]
testplot1 <- ggplot(test.data1, aes(Station, value)) +
geom_point(aes(shape = factor(quarter), fill = Species)) +
scale_shape_manual(values = c(21, 22)) +
labs(x = "Station", y = "Unit a", shape = "Sampling Quarter", fill = "Species") +
theme(legend.position = "none", legend.title = element_blank()) +
guides(fill = guide_legend(override.aes = list(shape = 21), nrow = 2, byrow = TRUE), shape = guide_legend(nrow = 2, byrow = TRUE)) +
facet_wrap( ~ constituent, ncol = 3, scales = "free_y")
testplot2 <- ggplot(test.data2, aes(Station, value)) +
geom_point(aes(shape = factor(quarter), fill = Species))
scale_shape_manual(values = c(21, 22)) +
labs(x = "Station", y = "Unit b", shape = "Sampling Quarter", fill = "Species") +
theme(legend.position = "top", legend.title = element_blank()) +
guides(fill = guide_legend(override.aes = list(shape = 21), nrow = 2, byrow = TRUE), shape = guide_legend(nrow = 2, byrow = TRUE)) +
facet_wrap( ~ constituent, ncol = 1, scales = "free_y")
grid.arrange(testplot2, testplot1, ncol = 2)
Which generates this:
But I want it to be arranged like this, where the XX and YY plots from above are normalized in size with the other plots (this was done using individual plots, and using plot_grid):
Example data from a larger set:
test.data <- structure(list(Station = structure(c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 1L,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, 5L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L
), .Label = c("A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "XX", "YY"), class = "factor"), value = c(16,
35, 46, 23, 40, 19, 9, 50, 0.2, 1, 0.5698, 0.322, 1, 0.45,
0.322, 0.5, 16, 9, 6, 19, 14, 13, 16, 9, 0, 0.004, 0, 0.004,
1, 0.32, 1, 0.678, 0, 0.39, 0.23, 0, 0, 1.1, 0.5, 0.5, 9,
4.9, 7, 4.768, 9, 8.65, 4.768, 6.54, 195, 195, 46, 46, 124,
124, 218, 218, 2, 1, 1, 1, 1, 2, 1, 1, 0.1, 0.4, 0.22, 0.4,
0.22, 0.4, 0.22, 0.1, 0.99, 0.99, 1.2, 0.45, 0.765, 0.99,
0.99, 0.99, 0.99, 1.2, 4.3, 0.98, 0.99, 1.2, 1.2, 34, 34,
65, 98, 150, 34, 65, 65, 2, 0, 4, 1.3, 5, 3.3, 1.56, 1, 9,
0.36, 4, 4, 11, 2, 2.22, 11)), class = "data.frame", row.names = c(NA,
-111L))

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)")

Resources