I want to see the percentage of people based on their race from the age of 20 to 35 reported their educational status. In the next step I make shiny app. However, I got this error. ![enter image description here][1]
Please help me how to link this code to shiny app.
My question is how can I by changing sliderInput from the age of 20 to 35 , in each age knows how many people have high school, college degree, and bachelor based on their race.
Below you can see the coding of age, education and race.
# rename education
nlsy97$educstat1997<-Recode(nlsy97$R1205700, recodes="0='None';1:2='Hischool';3='college';4='bachelor';5:7='mastermore' ;-5=NA;-3=NA;-4=NA", as.factor=T)
#rename ages
nlsy97$age1<-Recode(nlsy97$R1194100, recodes="12=12;13=13;14=14;15=15;16=16=17=17;18=18;19=19;-5=NA",as.factor=F)
# recode race
nlsy97$race<-Recode(nlsy97$R1482600 , recodes="1='black';2='hispanic' ;4='white';else=NA", as.factor=T)
table(nlsy97$race)
In the next step, I have made the transitions.
myvars1<-c( "R0000100","R0536300", "R0536402","R1489700","R1489800", "gender","race","age1","age2","age3","age4","age5","age6","age7","age8","age9","age10","age11","age12","age13","age14","age15","age16","age17","educstat1997","educstat1998","educstat1999","educstat2000","educstat2001","educstat2002","educstat2003","educstat2004","educstat2005","educstat2006","educstat2007","educstat2008","educstat2009","educstat2010","educstat2011","educstat2013","educstat2015")
which(myvars1 %in% names(nlsy97))
sub<-nlsy97[,myvars1]
sub<-subset(sub,is.na(sub$age1)==F&is.na(sub$age2)==F&is.na(sub$age3)==F&is.na(sub$age4)==F&is.na(sub$age5)==F&is.na(sub$age6)==F&is.na(sub$age7)==F&is.na(sub$age8)==F&is.na(sub$age9)==F&is.na(sub$age10)==F&is.na(sub$age11)==F&is.na(sub$age12)==F&is.na(sub$age13)==F&is.na(sub$age14)==F&is.na(sub$age15)==F&is.na(sub$age16)==F&is.na(sub$age17)==F)
head(sub, n=5)
x.vertical<-reshape(sub, idvar="R0000100", varying=list(age1=c("age1", "age2", "age3","age4","age5","age6","age7","age8","age9","age10","age11","age12","age13","age14","age15","age16"), age2=c("age2", "age3","age4","age5","age6","age7","age8","age9","age10","age11","age12","age13","age14","age15","age16","age17"),educstat1=c("educstat1997","educstat1998","educstat1999","educstat2000","educstat2001","educstat2002","educstat2003","educstat2004","educstat2005","educstat2006","educstat2007","educstat2008","educstat2009","educstat2010","educstat2011","educstat2013"),educstat2=c("educstat1998","educstat1999","educstat2000","educstat2001","educstat2002","educstat2003","educstat2004","educstat2005","educstat2006","educstat2007","educstat2008","educstat2009","educstat2010","educstat2011","educstat2013","educstat2015")), times=1:16, direction="long", v.names=c( "agestart", "ageend","educstat1","educstat2") )
x.vertical<-x.vertical[order(x.vertical$R0000100, x.vertical$time),]
The last step is making shiny dashboard.
# STEP 1: copy an example Shiny app into app.R (or ui.R and server.R)
library(shiny)
library(tidyverse)
library(gapminder)
# User Interface
ui <- basicPage(
# STEP 3: Create an input widget here (e.g. sliderInput)
sliderInput("age", "Select Age:", animate = TRUE, # STEP 4: add animate = TRUE here
min = 20, max = 35, value = 25,
step = 1,
sep="" # so thousands are not separated with a comma (without this defaults to 1,952 - 2,007)
), #note this comma here - different to our usual R code
tabPanel("Plot", plotOutput(outputId = "myplot"))
)
x.vertical2<-x.vertical[complete.cases(x.vertical[, c("race","educstat2")]),]
sums<-as.data.frame(xtabs(~educstat2+race, x.vertical2))
# Server
server <- function(input, output) {
output$myplot <- renderPlot({
# STEP 2: copy your plot code here
x.vertical2 %>%
filter(agestart==input$agestart)
mutate(educstat2 = fct_relevel(educstat2,
"None", "Hischool", "c",
"college")) %>%
filter(is.na(educstat2)==F)%>%
group_by(race, educstat2)%>%
summarise(n = n())%>%
mutate(freq= n /sum(n))%>%
ggplot(aes(x = factor(educstat2),y=freq, fill= race)) +
geom_bar( stat="identity",position = "dodge") +theme_bw()
})
}
shinyApp(ui, server)
dput(head(nlsy97, 10))
structure(list(R0000100 = 1:10, R0536300 = c(2L, 1L, 2L, 2L,
1L, 2L, 1L, 2L, 1L, 1L), R0536401 = c(9L, 7L, 9L, 2L, 10L, 1L,
4L, 6L, 10L, 3L), R0536402 = c(1981L, 1982L, 1983L, 1981L, 1982L,
1982L, 1983L, 1981L, 1982L, 1984L), R1194100 = c(15L, 14L, 13L,
15L, 15L, 15L, 14L, 16L, 15L, 14L), R1205700 = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), R1235800 = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), R1482600 = c(4L, 2L, 2L, 2L, 2L, 2L, 2L,
4L, 4L, 4L), R2553500 = c(17L, 16L, 15L, 17L, 16L, 16L, 15L,
17L, 16L, 14L), R2564101 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L), R3876300 = c(18L, 17L, 16L, 18L, 17L, 17L, 16L, 18L,
17L, 15L), R3885701 = c(2L, 0L, 0L, 2L, 0L, 0L, 0L, 2L, 0L, 0L
), R5453700 = c(19L, 18L, 17L, 19L, 18L, 19L, 17L, 19L, 18L,
16L), R5464901 = c(2L, 2L, 0L, 2L, 2L, 2L, 0L, 2L, 2L, 0L), R7216000 = c(20L,
19L, 18L, 20L, 19L, 20L, 18L, 20L, 19L, 17L), R7228601 = c(2L,
2L, 2L, 2L, 2L, 2L, 0L, 2L, 2L, 0L), S1531400 = c(21L, 20L, 19L,
21L, 20L, 20L, 19L, 21L, 20L, 18L), S1542501 = c(2L, 2L, 2L,
2L, 2L, 2L, 0L, 2L, 2L, 2L), S2001000 = c(22L, 21L, 20L, 22L,
21L, 22L, -5L, 22L, 21L, 19L), S2012301 = c(4L, 2L, 3L, 2L, 2L,
2L, -5L, 4L, 2L, 2L), S3801100 = c(23L, 22L, 21L, 23L, 22L, 23L,
-5L, 23L, 22L, 20L), S3813801 = c(4L, 2L, 3L, 2L, 2L, 2L, -5L,
4L, 2L, 2L), S5401000 = c(24L, 23L, -5L, 24L, 23L, 24L, 22L,
24L, 23L, 21L), S5413400 = c(4L, 2L, -5L, 2L, 2L, 2L, 0L, 4L,
4L, 2L), S7501200 = c(25L, -5L, -5L, 25L, 24L, 25L, 23L, 25L,
24L, 22L), S7514300 = c(4L, -5L, -5L, 2L, 2L, 2L, 0L, 4L, 4L,
4L), T0008500 = c(26L, -5L, -5L, 26L, 25L, 25L, -5L, 26L, 25L,
23L), T0014700 = c(4L, -5L, -5L, 2L, 2L, 2L, -5L, 4L, 4L, 4L),
T2011100 = c(27L, 26L, -5L, 27L, 26L, 26L, -5L, -5L, 26L,
24L), T2016800 = c(4L, 2L, -5L, 2L, 2L, 2L, -5L, -5L, 4L,
4L), T3601500 = c(28L, 27L, 26L, 28L, 26L, 27L, 26L, 28L,
27L, 25L), T3607100 = c(4L, 2L, 3L, 2L, 2L, 2L, 1L, 5L, 4L,
4L), T5201400 = c(29L, 28L, -5L, 29L, 28L, 28L, 27L, -5L,
28L, -5L), T5207400 = c(4L, 2L, -5L, 2L, 2L, 2L, 1L, -5L,
4L, -5L), T5207500 = c(4L, 2L, -5L, 2L, 2L, 2L, 1L, -5L,
4L, -5L), T6651300 = c(29L, 29L, 28L, 30L, 29L, 29L, 28L,
30L, 29L, -5L), T6657200 = c(4L, 2L, 3L, 2L, 2L, 2L, 1L,
5L, 5L, -5L), T6657300 = c(4L, 2L, 3L, 2L, 2L, 2L, 1L, 5L,
5L, -5L), T8123600 = c(32L, 31L, 30L, 32L, 31L, 31L, -5L,
-5L, 31L, -5L), T8129600 = c(4L, 2L, 3L, 2L, 2L, 2L, -5L,
-5L, 5L, -5L), T8129700 = c(4L, 2L, 3L, 2L, 2L, 2L, -5L,
-5L, 5L, -5L), U0001800 = c(34L, 33L, -5L, 34L, 33L, 34L,
32L, 34L, 33L, -5L), U0009400 = c(4L, 2L, -5L, 2L, 2L, 2L,
1L, 5L, 5L, -5L), U1838500 = c(-5L, 35L, 34L, 36L, 35L, 35L,
-5L, -5L, 35L, 34L), weight = c(607550L, 0L, 0L, 261156L,
450091L, 367309L, 0L, 0L, 618091L, 0L)), row.names = c(NA,
10L), class = "data.frame")
I am trying to run lme4 package in R. I have 10 Lines in total with four plants for each line in each of the two replications. But some of the plants died and there are some missing values. Weight is the response variable. Here are some lines from the data:
Line Rep Weight PLANT
Line 1 1 NA 1
Line 1 1 NA 2
Line 1 1 NA 3
Line 1 1 NA 4
Line 2 1 26 1
Line 2 1 26 2
Line 2 1 26 3
Line 2 1 27 4
Line 1 2 26 1
Line 1 2 28 2
Line 1 2 26 3
Line 1 2 25 4
Line 2 2 24 1
Line 2 2 26 2
Line 2 2 25 3
Line 2 2 NA 4
I want to run linear mixed model using lme4 package so I tried running:
lme4 <- lmer(Weight ~ 1 + (1|Rep:Plant), data=Data)
But I got an error:
boundary (singular) fit: see ?isSingular
> dput(Data)
structure(list(Line = c("Line 1", "Line 1", "Line 1", "Line 1",
"Line 2", "Line 2", "Line 2", "Line 2", "Line 1", "Line 1", "Line 1",
"Line 1", "Line 2", "Line 2", "Line 2", "Line 2"), Rep = c(1,
1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2), Weight = c(NA,
NA, NA, NA, 26, 26, 26, 27, 26, 28, 26, 25, 24, 26, 25, NA),
PLANT = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)), row.names = c(NA,
-16L), class = c("tbl_df", "tbl", "data.frame"))
I am using it for the first time and I am not sure about the error. I will appreciate any help!
Your model did fit, but it generated that warning because your random effects are very small. You can read more about this in this post or the help page
Let us look at your data:
ggplot(Data,aes(x=PLANT,y=Weight,col=Rep)) + geom_jitter() + geom_boxplot(alpha=0.2) + facet_wrap(~Rep)
The effects of PLANT and in combination with Rep is extremely small. Let's look at the fitted model:
fit = lmer(Weight ~ 1 + (1|PLANT:Rep),data=Data)
boundary (singular) fit: see ?isSingular
ranef(fit)
$`PLANT:Rep`
(Intercept)
1:1 0
1:2 0
2:1 0
2:2 0
3:1 0
3:2 0
4:1 0
4:2 0
This is exactly what happened. So we can try to account for some other effects and we still see very small coefficients:
fit = lmer(Weight ~ Line + (1|Rep:PLANT),data=Data)
ranef(fit)
$`Rep:PLANT`
(Intercept)
1:1 1.397563e-19
1:2 2.811371e-19
1:3 8.112169e-20
1:4 1.813251e-19
2:1 -1.725964e-19
2:2 -2.463986e-20
2:3 -2.027357e-19
2:4 -2.833681e-19
The takehome message is, there's no really systematic effect coming from PLANT, so you don't need to specify a highly complicated model, do something like:
fit = lmer(Weight ~ Line + (1|Rep),data=Data)
The data in case anyone is interested:
Data = structure(list(Line = structure(c(1L, 1L, 1L, 1L, 12L, 12L, 12L,
12L, 23L, 23L, 23L, 23L, 34L, 34L, 34L, 34L, 45L, 45L, 45L, 45L,
56L, 56L, 56L, 56L, 65L, 65L, 65L, 65L, 66L, 66L, 66L, 66L, 67L,
67L, 67L, 67L, 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, 13L,
13L, 13L, 13L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 16L, 16L,
16L, 16L, 8L, 8L, 8L, 8L, 66L, 66L, 66L, 66L, 17L, 17L, 17L,
17L, 18L, 18L, 18L, 18L, 9L, 9L, 9L, 9L, 19L, 19L, 19L, 19L,
20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 24L,
24L, 24L, 24L, 25L, 25L, 25L, 25L, 2L, 2L, 2L, 2L, 26L, 26L,
26L, 26L, 27L, 27L, 27L, 27L, 10L, 10L, 10L, 10L, 28L, 28L, 28L,
28L, 29L, 29L, 29L, 29L, 30L, 30L, 30L, 30L, 31L, 31L, 31L, 31L,
67L, 67L, 67L, 67L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 33L,
33L, 33L, 33L, 35L, 35L, 35L, 35L, 36L, 36L, 36L, 36L, 37L, 37L,
37L, 37L, 38L, 38L, 38L, 38L, 39L, 39L, 39L, 39L, 40L, 40L, 40L,
40L, 25L, 25L, 25L, 25L, 19L, 19L, 19L, 19L, 24L, 24L, 24L, 24L,
41L, 41L, 41L, 41L, 42L, 42L, 42L, 42L, 30L, 30L, 30L, 30L, 43L,
43L, 43L, 43L, 44L, 44L, 44L, 44L, 22L, 22L, 22L, 22L, 46L, 46L,
46L, 46L, 47L, 47L, 47L, 47L, 17L, 17L, 17L, 17L, 48L, 48L, 48L,
48L, 49L, 49L, 49L, 49L, 27L, 27L, 27L, 27L, 23L, 23L, 23L, 23L,
50L, 50L, 50L, 50L, 51L, 51L, 51L, 51L, 52L, 52L, 52L, 52L, 41L,
41L, 41L, 41L, 7L, 7L, 7L, 7L, 46L, 46L, 46L, 46L, 11L, 11L,
11L, 11L, 33L, 33L, 33L, 33L, 53L, 53L, 53L, 53L, 54L, 54L, 54L,
54L, 13L, 13L, 13L, 13L, 38L, 38L, 38L, 38L, 4L, 4L, 4L, 4L,
37L, 37L, 37L, 37L, 55L, 55L, 55L, 55L, 57L, 57L, 57L, 57L, 44L,
44L, 44L, 44L, 58L, 58L, 58L, 58L, 59L, 59L, 59L, 59L, 12L, 12L,
12L, 12L, 47L, 47L, 47L, 47L, 48L, 48L, 48L, 48L, 60L, 60L, 60L,
60L, 21L, 21L, 21L, 21L, 18L, 18L, 18L, 18L, 28L, 28L, 28L, 28L,
26L, 26L, 26L, 26L, 61L, 61L, 61L, 61L, 31L, 31L, 31L, 31L, 59L,
59L, 59L, 59L, 52L, 52L, 52L, 52L, 29L, 29L, 29L, 29L, 62L, 62L,
62L, 62L, 63L, 63L, 63L, 63L, 54L, 54L, 54L, 54L, 55L, 55L, 55L,
55L, 53L, 53L, 53L, 53L, 51L, 51L, 51L, 51L, 50L, 50L, 50L, 50L,
64L, 64L, 64L, 64L, 20L, 20L, 20L, 20L, 58L, 58L, 58L, 58L, 16L,
16L, 16L, 16L, 57L, 57L, 57L, 57L, 14L, 14L, 14L, 14L, 63L, 63L,
63L, 63L, 64L, 64L, 64L, 64L, 61L, 61L, 61L, 61L, 36L, 36L, 36L,
36L, 40L, 40L, 40L, 40L, 6L, 6L, 6L, 6L, 39L, 39L, 39L, 39L,
45L, 45L, 45L, 45L, 15L, 15L, 15L, 15L, 1L, 1L, 1L, 1L, 42L,
42L, 42L, 42L, 43L, 43L, 43L, 43L, 65L, 65L, 65L, 65L, 49L, 49L,
49L, 49L, 56L, 56L, 56L, 56L, 3L, 3L, 3L, 3L, 62L, 62L, 62L,
62L, 35L, 35L, 35L, 35L, 5L, 5L, 5L, 5L, 60L, 60L, 60L, 60L,
34L, 34L, 34L, 34L), .Label = c("Line1", "Line10", "Line11",
"Line12", "Line13", "Line14", "Line15", "Line16", "Line17", "Line18",
"Line19", "Line2", "Line20", "Line21", "Line22", "Line23", "Line24",
"Line25", "Line26", "Line27", "Line28", "Line29", "Line3", "Line30",
"Line31", "Line32", "Line33", "Line34", "Line35", "Line36", "Line37",
"Line38", "Line39", "Line4", "Line40", "Line41", "Line42", "Line43",
"Line44", "Line45", "Line46", "Line47", "Line48", "Line49", "Line5",
"Line50", "Line51", "Line52", "Line53", "Line54", "Line55", "Line56",
"Line57", "Line58", "Line59", "Line6", "Line60", "Line61", "Line62",
"Line63", "Line64", "Line65", "Line66", "Line67", "Line7", "Line8",
"Line9"), class = "factor"), Rep = 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L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L, 25L, 26L, 26L, 27L, NA, NA, 29L, 29L, 27L, 29L, 26L,
NA, NA, NA, 26L, 20L, 23L, 27L, NA, NA, NA, NA, 32L, 32L, 30L,
30L, 20L, 20L, 20L, 19L, 22L, 21L, 22L, 22L, 24L, 23L, 23L, 25L,
20L, 25L, NA, NA, 27L, 26L, NA, NA, NA, NA, NA, NA, 30L, 28L,
NA, NA, 25L, 26L, 27L, 26L, NA, NA, NA, NA, 20L, 19L, NA, NA,
19L, 27L, 26L, 29L, 26L, 29L, 31L, 29L, 25L, 25L, 24L, 25L, 26L,
25L, 26L, 26L, 25L, 24L, 24L, 28L, 22L, 26L, 24L, 28L, 29L, 30L,
26L, NA, NA, NA, NA, NA, 26L, 24L, 24L, 24L, NA, NA, NA, NA,
NA, NA, NA, NA, 30L, 30L, 30L, 31L, 24L, 25L, 28L, 22L, 28L,
31L, 30L, NA, 31L, 30L, 29L, 25L, 25L, 22L, 24L, 20L, 30L, 30L,
30L, 29L, 26L, 32L, 28L, 29L, 20L, 15L, 15L, 11L, 25L, 24L, 24L,
24L, 26L, 29L, 31L, 30L, 24L, 28L, 20L, 22L, 29L, 26L, 26L, 28L,
27L, 27L, 27L, 26L, 21L, 22L, 21L, NA, 28L, 29L, 24L, 24L, 28L,
29L, 28L, 27L, 28L, 29L, 27L, 29L, NA, NA, NA, NA, 22L, 26L,
21L, 21L, 26L, 30L, 28L, 30L, 27L, 26L, 28L, 26L, 25L, 25L, 26L,
26L, 27L, 26L, 23L, 29L, NA, NA, NA, NA, 27L, 23L, 29L, 23L,
28L, 29L, 28L, 26L, 20L, NA, NA, NA, 28L, 23L, 26L, 21L, 28L,
26L, 26L, 29L, 20L, 27L, 20L, 26L, 29L, 26L, 28L, 28L, 30L, 27L,
NA, NA, 26L, 21L, 26L, 25L, 27L, 26L, 27L, 24L, 25L, 20L, 21L,
20L, 25L, 25L, 31L, 24L, 29L, 28L, 31L, 27L, 25L, 28L, 26L, 26L,
NA, NA, NA, NA, 24L, 25L, 23L, 27L, 20L, 26L, 25L, 25L, 29L,
28L, 29L, 29L, 26L, 27L, 25L, 28L, NA, NA, NA, NA, 26L, 28L,
NA, NA, 21L, 20L, 31L, 25L, 31L, 28L, 30L, 29L, 23L, 25L, 24L,
28L, 25L, 22L, 25L, 25L, 28L, 29L, 28L, 29L, 26L, 24L, 25L, 26L,
29L, 27L, NA, NA, 26L, 29L, 29L, 30L, 25L, 24L, 25L, 24L, 28L,
25L, 29L, 28L, 24L, 24L, 24L, 24L, 28L, 30L, 27L, 27L, 26L, 25L,
25L, 25L, 25L, 25L, 28L, 25L, 25L, 30L, 28L, 25L, 22L, 24L, 25L,
24L, NA, NA, NA, NA, 5L, 7L, 4L, 5L, 21L, 20L, 22L, 24L, 25L,
27L, 25L, 28L, 32L, 31L, NA, NA, 19L, 26L, 20L, NA, 26L, 26L,
30L, 25L, 28L, 31L, 30L, 26L, 5L, 8L, 4L, 8L, 25L, 25L, 28L,
25L, 28L, 28L, 27L, 26L, 30L, 27L, 27L, 24L, 32L, 29L, 31L, 25L,
30L, 30L, 27L, 28L, 16L, 20L, 16L, 21L, 25L, 22L, 25L, 20L, 24L,
25L, 18L, 25L, 25L, 26L, 29L, 29L, 21L, 20L, 22L, 21L, 19L, 22L,
19L, 21L, 28L, 25L, 26L, 24L, 28L, 26L, 24L, 25L, NA, NA, NA,
NA, 25L, NA, NA, NA, 23L, 21L, 19L, 23L, 25L, 24L, 25L, NA, 22L,
30L, 29L, 26L, 25L, 25L, 24L, 24L), PLANT = structure(c(1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2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14", "22", "23"
), class = "factor")), row.names = c(NA, -536L), class = "data.frame")
I have made a facet plot below using the following command:
ggplot(data, aes(factor(Length),logFC)),
+ geom_boxplot(fill = "grey90"),
+ coord_cartesian(ylim=c(-5,5)) + facet_grid(X~Modification)
Is there a way to compute p-values for each boxplot and add them as geom_text above each boxplot. I want to compute a t-test and compare against y=0.
My data looks like this:
X Length logFC Modification
Daub 26 -0.7307060811 NTA
Daub 22 -0.3325621272 NTA
Daub 22 -2.0579390395 NTA
Daub 25 2.7199391457 NTA
Daub 23 -0.0009869389 NTA
Daub 25 -0.3318842493 NTA
...
My error message:
> data <- structure(list(Experiment = 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, 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("Daub", "Marie",
+ "Meister"), class = "factor"), Length = c(26L, 22L, 22L, 25L,
+ 23L, 25L, 23L, 25L, 24L, 23L, 24L, 26L, 24L, 21L, 20L, 21L, 22L,
+ 22L, 21L, 21L, 21L, 22L, 21L, 22L, 21L, 21L, 20L, 20L, 21L, 25L,
+ 20L, 22L, 24L, 22L, 23L, 24L, 23L, 23L, 22L, 22L, 22L, 22L, 21L,
+ 19L, 21L, 20L, 20L, 20L, 19L, 19L, 19L, 22L, 23L, 23L, 22L, 23L,
+ 22L, 20L, 21L, 24L, 24L, 24L, 25L, 24L, 21L, 20L, 23L, 23L, 20L,
+ 23L, 23L, 24L, 20L, 21L, 22L, 24L, 23L, 22L, 23L, 22L, 23L, 23L,
+ 19L, 21L, 23L, 24L, 22L, 23L, 23L, 21L, 22L, 20L, 22L, 23L, 25L,
+ 22L, 22L, 23L, 22L, 23L, 25L, 25L, 24L, 24L, 23L, 22L, 22L, 25L,
+ 23L, 24L, 23L, 23L, 22L, 22L, 25L, 23L, 22L, 25L, 21L, 19L, 21L,
+ 23L, 22L, 22L, 20L, 20L, 20L, 23L, 22L, 21L, 21L, 23L, 23L, 23L,
+ 21L, 25L, 23L, 24L, 24L, 23L, 23L, 23L, 21L, 22L, 21L, 21L, 23L,
+ 23L, 22L, 22L, 21L, 22L, 22L, 25L, 24L, 24L, 22L, 24L, 24L, 23L,
+ 22L, 21L, 22L, 23L, 20L, 22L, 23L, 24L, 25L, 24L, 25L, 22L, 23L,
+ 24L, 21L, 25L, 23L, 19L, 21L, 21L, 22L, 20L, 21L, 18L, 20L, 20L,
+ 21L, 20L, 23L, 19L, 19L, 22L, 22L, 22L, 22L, 22L, 21L, 22L, 24L,
+ 20L, 21L, 22L, 22L, 21L, 21L, 21L, 21L, 21L, 23L, 23L, 23L, 25L,
+ 25L, 25L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 25L, 25L), logFC = c(-0.7307060811,
+ -0.3325621272, -2.0579390395, 2.7199391457, -0.0009869389, -0.3318842493,
+ -2.1922199037, -1.8907961065, -1.9059255014, -0.2815081355, -0.2040330335,
+ 3.661469505, 0.6489955587, -0.0261245467, -1.4312409441, -1.1199604078,
+ -1.6528592355, -2.8208936451, -0.7207549269, -1.6528592355, -1.2540377475,
+ -2.1088724443, -2.1088724443, -1.5556550771, -1.5556550771, -0.2899601367,
+ 0.36449851, -1.7787723427, -1.5556550771, -1.5556550771, -1.5556550771,
+ -2.1092566794, 0.0417776477, -3.0768675589, -4.2573082637, -1.5556550771,
+ -1.8493703566, -0.7310899725, -2.8201262449, -0.7203706918, -2.1088724443,
+ -3.5714106365, -1.5556550771, -1.2144625017, 1.6608916211, -0.3147141406,
+ 1.2344697053, 1.2303596917, 1.2138067782, 0.9409846988, 0.5270928206,
+ -1.0435216994, -1.4320081419, -1.1644217165, -1.1478237529, -0.9941196613,
+ 0.0762668692, 1.0076747803, 0.0679302699, -0.4852244221, 0.7792467457,
+ 0.4902414285, 1.6172022872, 0.5270928206, -1.5403877099, -0.3322684844,
+ 0.0965099283, 0.8067662712, -0.3322684844, -1.2928579903, 0.6067208763,
+ 0.0247576412, -0.0291609233, -0.4737578429, 0.0743062433, 0.1126554177,
+ -0.0156954476, 1.1069888258, -0.956482117, -0.2829742145, 0.8511530937,
+ -0.1571780266, -1.2033199926, -1.1883052896, -0.0619556757, -0.7813018565,
+ 2.2467468049, 2.8382841074, 0.5658773933, -0.4461699001, -0.7409548873,
+ -0.992979577, -1.0966445642, -0.8035321174, 0.4586171366, -0.2760821893,
+ 0.0585422656, 0.0328935437, 0.3858231436, -0.4374188039, 1.1166538873,
+ -1.6539303789, 0.2027459981, -0.2193112677, -0.3939953745, -1.6726108643,
+ 1.1518720793, 2.2517568637, -0.561147283, -2.1625509666, -1.65562751,
+ -0.9048469063, -1.0759388341, 0.4938537603, 1.8754485108, -1.5944759871,
+ 1.0688499798, 2.6559945275, -1.908097968, -1.9214219995, -2.9675169126,
+ 0.0365892303, -0.8345258687, -1.0535567925, -2.0036191122, -1.6843791204,
+ -2.5554312825, -1.5778268888, -1.576142107, -0.9398408101, 2.4453250675,
+ -1.5434092122, -0.794414515, -0.6200158513, 0.5556353409, -1.0772272444,
+ -0.8720587283, -0.8082062813, -0.7353916189, 0.1072543637, 0.5658773933,
+ 0.13043531, -0.0154958912, -0.868710614, -0.1922496916, 1.0682890388,
+ -1.673413308, -0.9581901784, -1.9575141988, -1.8973257122, 1.4967046965,
+ -2.456068976, -1.4577030552, -4.2692094743, -1.9124787897, -1.4993411082,
+ -0.6409837734, 0.6369441273, -0.9960964825, -5.9703084924, -1.97960268,
+ -1.2422870608, -1.5170124157, -1.9021683731, 3.4029417731, 0.1812972171,
+ -1.6370149729, -1.749015407, -2.1677341592, -1.4942545905, -1.1137758818,
+ -1.2428452903, -1.3014446584, 0.0287537402, -0.8721416458, -2.4062762035,
+ -4.0278899462, -2.2229120764, -1.5950383235, -3.6098212725, -2.5979636046,
+ 0.3631424981, 1.1377073609, 0.5151459494, 0.0640542096, -0.7715375264,
+ -1.0361077101, -0.2462753448, -2.3058140776, -0.0847179004, -0.518970228,
+ 0.8519432911, 1.9516260022, -0.5706154628, 1.240812729, 0.336736001,
+ 2.2509464232, -0.322918086, -4.4019571741, -0.5618441487, 3.4700721641,
+ -3.9220135953, -2.1968879291, -0.1362995026, 2.164094913, -1.0688563363,
+ 0.4302583643, 2.6411096027, -3.020513717, -1.5395519303, -2.2219591633,
+ -3.8891956255, 0.9602784132, -0.6470571429, 1.853151793, -0.3271268741,
+ -0.9870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", "t5-u"), class = "factor")), .Names = c("Experiment",
+ "Length", "logFC", "Modification"), class = "data.frame", row.names = c(NA,
+ -223L))
> library(dplyr)
> pvalues <- data %>% group_by(Experiment, Modification, Length) %>%
+ filter(n() > 1) %>%
+ summarize(p.value = (t.test(logFC, mu = 0)$p.value))
Error in t.test(logFC, mu = 0) : object 'logFC' not found
You can do this by summarizing the data into a table of p-values. This can be done using dplyr:
library(dplyr)
pvalues <- data %>% group_by(Experiment, Modification, Length) %>%
filter(n() > 1) %>%
summarize(p.value = (t.test(logFC, mu = 0)$p.value))
(The line filter(n() > 1) is to get rid of any groups of size 1, for which a p-value cannot be calculated). This produces a table that looks like:
# Experiment Modification Length p.value
# 1 Daub NTA 22 0.3980043
# 2 Daub NTA 23 0.3535590
# 3 Daub NTA 24 0.5831962
# 4 Daub NTA 25 0.9137644
# 5 Daub NTA 26 0.6254004
# 6 Daub t3-d 20 0.1493108
Now you can add that text to your plot using a geom_text layer, choosing some y such as y = 3:
library(ggplot2)
ggplot(data, aes(factor(Length),logFC)) + geom_boxplot(fill = "grey90") +
coord_cartesian(ylim=c(-5,5)) + facet_grid(Experiment~Modification) +
geom_text(aes(y = 3, label = p.value), data = pvalues, size = 1)
You will probably have to manipulate the size (and possibly angle) of your geom_text to make the plot readable. Note also that since you are performing many tests, you should probably look at the adjusted p-values rather than the raw p-values. You can compute that column with
pvalues <- pvalues %>% mutate(p.adjusted = p.adjust(p.value, method = "bonferroni"))
The function format.pval will also come in handy, especially if some of your p-values are close to 0.