I am running a linear mixed model for a dataset
Developmental trajectory of 2 groups- assessed at 3 timepoints on a number of different measures. Predictor variables are age and maternal education. I am using nlme and ggplot2 packages.
Here is my final model (GM_RAW is the dependent variable)
Model_5<-lme(GM_RAW~timepoint*Group+age+Maternal_Education, data=dat,
random=~timepoint|ID,method="ML", na.action=na.omit,control=list(opt="optim"))
summary(Model_5)
I have plotted individual trajectories based on the raw data but I want to add the information from the predicted lines from the model for my 2 groups.
I have tried various suggestions posted on stack overflow but none seem to work
dat$gmpred<-predict(Model_5)
GrossMotor<-ggplot(dat,aes(x=as.numeric(timepoint),y=GM_RAW,colour=Group))+
geom_point()+ geom_line(aes(group=ID))
GrossMotor + geom_line(data=dat, aes(y=gmpred))
I want to show the intercept and slope for my 2 groups on the final graph
some sample data
"","ID","timepoint","Maternal_Education","age","Group","GM_RAW"
"1",3012,"5 months",NA,188,"Typical Group",10
"2",3089,"5 months",NA,182,"Typical Group",9
"3",3012,"10 months",NA,328,"Typical Group",13
"4",3004,"5 months","Tertiary postgraduate",163,"Typical Group",6
"5",3008,"5 months","Tertiary postgraduate",171,"Typical Group",4
"6",3023,"5 months","Tertiary postgraduate",170,"Typical Group",8
"7",3063,"5 months","Tertiary postgraduate",181,"Typical Group",10
"8",3071,"5 months","Tertiary postgraduate",151,"Typical Group",8
"9",3074,"5 months","Tertiary postgraduate",183,"Typical Group",9
"10",3075,"5 months","Tertiary postgraduate",165,"Typical Group",9
"11",3087,"5 months","Tertiary postgraduate",172,"Typical Group",6
"12",3104,"5 months","Tertiary postgraduate",180,"Typical Group",7
"13",3115,"5 months","Tertiary postgraduate",199,"Typical Group",8
"14",3142,"5 months","Tertiary postgraduate",201,"Typical Group",9
"15",3161,"5 months","Tertiary postgraduate",189,"Typical Group",7
"16",3162,"5 months","Tertiary postgraduate",201,"Typical Group",8
"17",4002,"5 months","Tertiary postgraduate",202,"NF1",8
"18",4024,"5 months","Tertiary postgraduate",167,"NF1",8
"19",3004,"10 months","Tertiary postgraduate",315,"Typical Group",9
"20",3008,"10 months","Tertiary postgraduate",341,"Typical Group",9
"21",3023,"10 months","Tertiary postgraduate",358,"Typical Group",14
"22",3063,"10 months","Tertiary postgraduate",293,"Typical Group",17
"23",3071,"10 months","Tertiary postgraduate",302,"Typical Group",12
"24",3074,"10 months","Tertiary postgraduate",333,"Typical Group",12
"25",3075,"10 months","Tertiary postgraduate",318,"Typical Group",11
"26",3078,"10 months","Tertiary postgraduate",304,"Typical Group",9
"27",3087,"10 months","Tertiary postgraduate",335,"Typical Group",13
"28",3104,"10 months","Tertiary postgraduate",294,"Typical Group",10
"29",3115,"10 months","Tertiary postgraduate",305,"Typical Group",11
"30",3142,"10 months","Tertiary postgraduate",327,"Typical Group",11
"31",3161,"10 months","Tertiary postgraduate",328,"Typical Group",12
"32",3162,"10 months","Tertiary postgraduate",333,"Typical Group",10
"33",4002,"10 months","Tertiary postgraduate",335,"NF1",11
"34",4009,"10 months","Tertiary postgraduate",320,"NF1",11
"35",4024,"10 months","Tertiary postgraduate",351,"NF1",13
"36",3004,"14 months","Tertiary postgraduate",438,"Typical Group",21
"37",3008,"14 months","Tertiary postgraduate",460,"Typical Group",9
"38",3023,"14 months","Tertiary postgraduate",471,"Typical Group",18
"39",3063,"14 months","Tertiary postgraduate",445,"Typical Group",20
I think the problem is the way you are constructing your model. Based on your data, I had to take away some covariates in order to make the model work. Also, if you include a random effect for "ID", you will have different intercepts for each ID, and thus for each timepoint you will have several y values in your prediction column. ggplot does link them, but of course it looks totally crooked.
Thus, I removed the ID as a random effect and you will see that the plot works.
library(tidyverse)
foo <- foo %>% mutate(timepoint = str_replace(timepoint, " months", ""))
Model_5 <- nlme::lme(GM_RAW ~ Group,
data = foo,
random = ~ 1 | timepoint, method = "ML", na.action = na.omit
)
foo$gmpred <- predict(Model_5)
ggplot(foo, aes(x = as.numeric(timepoint), y = GM_RAW, colour = Group)) +
geom_point() +
geom_line(aes(group = ID)) +
geom_line(aes(y = gmpred, group = Group, linetype = Group), color = "black")
If you really need to add ID as a random effect, you will need to change the grouping variable for the geom_line, I use interaction()
Model_5 <- nlme::lme(GM_RAW ~ Group,
data = foo,
random = ~ timepoint | ID, method = "ML", na.action = na.omit
)
foo$gmpred <- predict(Model_5)
ggplot(foo, aes(x = as.numeric(timepoint), y = GM_RAW, colour = Group)) +
geom_point() +
geom_line(aes(group = ID)) +
geom_line(aes(y = gmpred, group = interaction(Group,ID), linetype = Group), color = "black")
Created on 2020-04-18 by the reprex package (v0.3.0)
data
foo <- readr::read_csv(",ID,timepoint,Maternal_Education,age,Group,GM_RAW
1,3012,5 months,NA,188,Typical Group,10
2,3089,5 months,NA,182,Typical Group,9
3,3012,10 months,NA,328,Typical Group,13
4,3004,5 months,Tertiary postgraduate,163,Typical Group,6
5,3008,5 months,Tertiary postgraduate,171,Typical Group,4
6,3023,5 months,Tertiary postgraduate,170,Typical Group,8
7,3063,5 months,Tertiary postgraduate,181,Typical Group,10
8,3071,5 months,Tertiary postgraduate,151,Typical Group,8
9,3074,5 months,Tertiary postgraduate,183,Typical Group,9
10,3075,5 months,Tertiary postgraduate,165,Typical Group,9
11,3087,5 months,Tertiary postgraduate,172,Typical Group,6
12,3104,5 months,Tertiary postgraduate,180,Typical Group,7
13,3115,5 months,Tertiary postgraduate,199,Typical Group,8
14,3142,5 months,Tertiary postgraduate,201,Typical Group,9
15,3161,5 months,Tertiary postgraduate,189,Typical Group,7
16,3162,5 months,Tertiary postgraduate,201,Typical Group,8
17,4002,5 months,Tertiary postgraduate,202,NF1,8
18,4024,5 months,Tertiary postgraduate,167,NF1,8
19,3004,10 months,Tertiary postgraduate,315,Typical Group,9
20,3008,10 months,Tertiary postgraduate,341,Typical Group,9
21,3023,10 months,Tertiary postgraduate,358,Typical Group,14
22,3063,10 months,Tertiary postgraduate,293,Typical Group,17
23,3071,10 months,Tertiary postgraduate,302,Typical Group,12
24,3074,10 months,Tertiary postgraduate,333,Typical Group,12
25,3075,10 months,Tertiary postgraduate,318,Typical Group,11
26,3078,10 months,Tertiary postgraduate,304,Typical Group,9
27,3087,10 months,Tertiary postgraduate,335,Typical Group,13
28,3104,10 months,Tertiary postgraduate,294,Typical Group,10
29,3115,10 months,Tertiary postgraduate,305,Typical Group,11
30,3142,10 months,Tertiary postgraduate,327,Typical Group,11
31,3161,10 months,Tertiary postgraduate,328,Typical Group,12
32,3162,10 months,Tertiary postgraduate,333,Typical Group,10
33,4002,10 months,Tertiary postgraduate,335,NF1,11
34,4009,10 months,Tertiary postgraduate,320,NF1,11
35,4024,10 months,Tertiary postgraduate,351,NF1,13
36,3004,14 months,Tertiary postgraduate,438,Typical Group,21
37,3008,14 months,Tertiary postgraduate,460,Typical Group,9
38,3023,14 months,Tertiary postgraduate,471,Typical Group,18
39,3063,14 months,Tertiary postgraduate,445,Typical Group,20")
#> Warning: Missing column names filled in: 'X1' [1]
Related
In ggplot2/stat_summary, how to add the median value as label to plot ? Thanks!
library(ggplot2)
d <- ggplot(mtcars, aes(cyl, mpg)) + geom_point()
d + stat_summary(fun = "median", colour = "red", size = 2, geom = "point")
One potential option is to use after_stat() to get the labels, i.e.
library(ggplot2)
d <- ggplot(mtcars, aes(cyl, mpg)) +
geom_point()
d + stat_summary(fun = "median", colour = "red", size = 4,
geom = "text", aes(label = after_stat(y)),
position = position_nudge(x = 0.25))
Created on 2022-05-16 by the reprex package (v2.0.1)
I want to write axis ticks in small capital using ggtext::element_markdown(). However, an attempt like <span class='font-variant: small-caps'>small capital here!</span> is in vain. Then, how should I achieve that effect?
MWE
library(tidyverse)
tribble(
~ f1, ~ f2, ~ mean,
"a", "SBJ", 1212,
"a", "OBJ", 1313,
"p", "SBJ", 1515,
"p", "OBJ", 1616
) |>
mutate(
f2 = fct_relevel(
f2,
c(
"SBJ",
"OBJ"
)
)
) |>
ggplot(
aes(
x = f2,
y = mean,
fill = f1
)
) +
scale_x_discrete(
labels = c(
"NP <span class='font-variant: small-caps'>sbj</span>",
"NP <span class='font-variant: small-caps'>obj</span>"
)
) +
geom_col(
position = 'dodge',
size = 1
) +
theme(
axis.text.x = ggtext::element_markdown()
)
Unfortunately the font-variant property is not supported by ggtext. According to the [docs] only (https://wilkelab.org/ggtext/articles/introduction.html):
The CSS properties color, font-size, and font-family are currently supported.
Hence achieving your desired result requires some manual work by converting your strings to uppercase and setting a small font size via ggtext.
BTW: The style is set via style not class.
ggplot(
df,
aes(
x = f2,
y = mean,
fill = f1
)
) +
scale_x_discrete(
labels = c(
glue::glue("NP <span style='font-size: 6pt;'>{toupper('sbj')}</span>"),
glue::glue("NP <span style='font-size: 6pt;'>{toupper('obj')}</span>")
)
) +
geom_col(
position = "dodge",
size = 1
) +
theme(
axis.text.x = ggtext::element_markdown()
)
Given this data here:
p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl)
g <- ggplot_gtable(ggplot_build(p))
strip_both <- which(grepl('strip-', g$layout$name))
fills <- c("red","green","blue","yellow","red","green","blue","yellow")
k <- 1
for (i in strip_both) {
j <- which(grepl('rect', g$grobs[[i]]$grobs[[1]]$childrenOrder))
g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
k <- k+1
}
grid.draw(g)
I want to add a legend for the colors of the facets:
as shown here
One option to achieve your desired result would be with an auxiliary geom_point layer which draws nothing but is only used to map a variable with your desired four categories on the fill aes. Doing so will automatically add a fill legend for which we could set your desired colors using scale_fill_manual. Additionally I switched the key_glyph for the point layer to draw_key_rectto mimic your desired style for the legend keys and added na.rm to silent the warning about removed NAs:
library(ggplot2)
library(grid)
p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl) +
geom_point(data = data.frame(x = NA_real_, y = NA_real_, fill = c("AB", "D", "FF", "v")),
aes(x = x, y = y, fill = fill), na.rm = TRUE, key_glyph = "rect") +
scale_fill_manual(values = c("AB" = "red", D = "yellow", FF = "blue", v = "green"), name = NULL) +
theme(legend.position = "bottom")
g <- ggplot_gtable(ggplot_build(p))
strip_both <- which(grepl('strip-', g$layout$name))
fills <- c("red","green","blue","yellow","red","green","blue","yellow")
k <- 1
for (i in strip_both) {
j <- which(grepl('rect', g$grobs[[i]]$grobs[[1]]$childrenOrder))
g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
k <- k+1
}
grid.draw(g)
I am trying to change one of the labels in my facet grid to contain some subscript text. I have been looking on stack overflow but none of the solutions work/I cannot understand how others are functioning so cannot apply them to my own scenario
This is my current ggplot function:
FacetGridTest <- tss_profiles %>% group_by(pulldown, Condition, replicate, category) %>%
group_by(pulldown,Condition,category,region_bin, region) %>%
mutate(meannone = mean(none)) %>%
ungroup() %>%
mutate(cond_cat=factor(cond_cat, levels=c("IG Target","IG Non-Target","MH Target","PH Target","MH Non-Target","PH Non-Target"))) %>%
mutate(category=factor(category, levels=c("Target","Non-Target"))) %>%
ggplot() + aes(x=Bin, y=meannone, col=Condition, group=paste(pulldown, Condition, replicate, category)) +
geom_line() +
facet_grid(pulldown~category, scale="free_x") +
theme_bw(base_size = 10) +
theme(legend.text.align = 0) +
labs(y=expression(paste("Read Counts")),
x=expression(paste("Bin"))) +
scale_colour_discrete(name = "Condition", labels = c("A","B","C")) +
geom_vline(xintercept=6000, linetype="dotted") +
annotate("text", x = 1000, y = 8800, label = "TSS", size=3) +
annotate("text", x = 11000, y = 8800, label = "TTS", size=3)
I want to change the "Non-Target" label to "J2L2 Non-Target20+"
Based on what I have seen on stack overflow I have tried doing:
vnames <- list('Target' = 'Target',
'Non-Target' = expression(paste("J" ["2"], "L" ["2"], " Non-Target" ["20"], "+")))
bnames <- list('A' = 'A','B' = 'B')
plot_labeller <- function(variable,value){
if (variable=='category') {
return(vnames[value])
} else if (variable=='pulldown') {
return(bnames[value])
} else {
return(as.character(value))
}
}
tss_profiles %>% group_by(pulldown, Condition, replicate, category) %>%
group_by(pulldown,Condition,category,region_bin, region) %>%
mutate(meannone = mean(none)) %>%
ungroup() %>%
mutate(cond_cat=factor(cond_cat, levels=c("IG Target","IG Non-Target","MH Target","PH Target","MH Non-Target","PH Non-Target"))) %>%
mutate(category=factor(category, levels=c("Target","Non-Target"))) %>%
ggplot() + aes(x=Bin, y=meannone, col=Condition, group=paste(pulldown, Condition, replicate, category)) +
geom_line() +
facet_grid(pulldown~category, scale="free_x", plot_labeller) +
theme_bw(base_size = 10) +
theme(legend.text.align = 0) +
labs(y=expression(paste("Read Counts")),
x=expression(paste("Bin"))) +
scale_colour_discrete(name = "Condition", labels = c("A","B","C")) +
geom_vline(xintercept=6000, linetype="dotted") +
annotate("text", x = 1000, y = 8800, label = "TSS", size=3) +
annotate("text", x = 11000, y = 8800, label = "TTS", size=3)
But this has not worked. I am probably misunderstanding how labellers work. Any help would be much appreciated.
I tried to run both codes but got error that object 'tss_profiles' not found.
Therefore I can just hypothesise that you should remove quotes from the subscript text ([20] instead of what you have - ["20"]).
I'm rather new at using GIMP's script-fu, and I'm working on a script to go through a full folder of .tif image files, resize them to having a dimension a maximum size of 1200 pixels while maintaining the proportions of the image. It then will save the file as a .png filetype.
The problem I've encountered and can't seem to find an answer for is:
Error: (: 1) car: argument 1 must be: pair
From what I can tell, it's saying that I'm trying to find the first entry of a list that doesn't have contents, but I don't see any further instances where this would be an issue. I've been pouring over numerous sites for help on this and tinkering with the script for around a while now, so I think it's time to ask for help. Unfortunately GIMP's documentation isn't very robust from what I can tell. Am I missing a car() somewhere that's still causing this error or is it referring to something more ambiguous now?
Thanks in advance...
(script-fu-register
"batchresize"
"Batch Resize"
"Resizes all images in folder to the desired maximum size and saves as .png"
"name"
"(c) 2017"
"March 2017"
""
SF-VALUE "Maximum Dimension" "1200"
SF-STRING "Target Folder" "/Scripts/Input/*.tif"
)
(script-fu-menu-register "batchresize" "<Image>/File/Batch"
)
(define
(batchresize maximum targetfolder
)
(let*
(
(filelist
(car
(file-glob targetfolder 1
)
)
)
(width 1200
)
(height 1200
)
)
(while
(not
(null? filelist
)
)
(let*
(
(filename
(car filelist
)
)
(image
(car
(gimp-file-load RUN-NONINTERACTIVE filename filename
)
)
)
(drawable
(car
(gimp-image-get-active-layer image
)
)
)
)
(Set! width car(gimp-image-width)
)
(Set! height car(gimp-image-height)
)
(if
(> height width
)
(set! proportion
(/ width height
)
)
(set! height maximum
)
(set! width
(* maximum proportion
)
)
(if
(< height width
)
(set! proportion
(/ height width
)
)
(set! width maximum
)
(set! height
(* maximum proportion
)
)
(if
(= height width
)
(set! height maximum
)
(set! width maximum
)
)
(gimp-image-scale-full image width height INTERPOLATION-CUBIC
)
(file-png-save RUN-NONINTERACTIVE image drawable filename filename 1 0 0 0 0 0 0
)
)
(gimp-image-delete image
)
)
(set! filelist
(cdr filelist
)
)
)
)
)
)