I have an issue that is related to this one, but was unable to come to a solution for mine.
I have a reactive ggplot that I would like to update using a check box based on group data.
Currently, when I have ONE box selected, the data displays correctly. If I select more than one check box, I lose data points. See pictures below. I think I have to change the way I'm filtering my data and use droplevels somewhere but not sure how to integrate that (I'm new to shiny!). Any suggestions are appreciated!
WHOC_Sum_CMJ <- structure(list(Athlete = structure(c(1L, 1L, 1L, 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, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L), .Label = c("Athlete 1", "Athlete 10",
"Athlete 11", "Athlete 12", "Athlete 13", "Athlete 14", "Athlete 2",
"Athlete 3", "Athlete 4", "Athlete 5", "Athlete 6", "Athlete 7",
"Athlete 8", "Athlete 9"), class = "factor"), Date = structure(c(1L,
4L, 5L, 1L, 3L, 5L, 7L, 2L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L,
5L, 7L, 1L, 3L, 6L, 7L, 2L, 4L, 5L, 8L, 1L, 3L, 5L, 7L, 1L, 3L,
5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L,
6L, 7L, 1L, 3L, 5L, 7L), .Label = c("2020-01-06", "2020-01-07",
"2020-01-13", "2020-01-14", "2020-01-21", "2020-01-23", "2020-01-27",
"2020-01-28"), class = "factor"), Position = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("DEF", "FWD", "GOALIE"), class = "factor"),
Program = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("Navy", "Red", "RTP", "White"), class = "factor"),
mRSI = c(0.36, 0.38, 0.42, 0.46, 0.46, 0.47, 0.48, 0.31,
0.3, 0.24, 0.3, 0.29, 0.26, 0.28, 0.28, 0.36, 0.35, 0.43,
0.43, 0.28, 0.31, 0.28, 0.3, 0.33, 0.36, 0.35, 0.37, 0.37,
0.36, 0.37, 0.36, 0.3, 0.36, 0.34, 0.37, 0.26, 0.28, 0.34,
0.3, 0.39, 0.4, 0.43, 0.43, 0.43, 0.47, 0.46, 0.48, 0.34,
0.36, 0.33, 0.37, 0.28, 0.28, 0.34, 0.33), SystemWeight = c(617.21,
612.4, 620.45, 672.08, 682.23, 670.5, 663.41, 517.33, 515.23,
511.62, 517.85, 697.55, 703.92, 689.43, 691.33, 859.06, 845.9,
850.97, 851.84, 655.79, 665.09, 673.91, 667.92, 626.78, 632.92,
634.52, 624.88, 637.55, 645.6, 648.78, 646.64, 558.03, 563.23,
569.58, 560.95, 693.63, 695.54, 684.37, 684.58, 641.18, 660.8,
663.95, 660, 594.92, 596.97, 591.36, 585.64, 522.35, 518.17,
530.95, 523.5, 780.65, 789.81, 775.84, 775.48), FTCT = c(0.61,
0.62, 0.67, 0.74, 0.75, 0.77, 0.77, 0.54, 0.55, 0.44, 0.53,
0.53, 0.49, 0.53, 0.56, 0.6, 0.58, 0.68, 0.68, 0.53, 0.57,
0.54, 0.55, 0.61, 0.63, 0.64, 0.65, 0.59, 0.58, 0.59, 0.59,
0.51, 0.59, 0.59, 0.59, 0.53, 0.57, 0.63, 0.59, 0.76, 0.76,
0.79, 0.78, 0.67, 0.72, 0.72, 0.74, 0.63, 0.65, 0.61, 0.63,
0.49, 0.5, 0.53, 0.57), JumpHeight_cm = c(28.97, 29.78, 31.43,
35.83, 35.41, 36.59, 36.92, 27.56, 26.11, 26.15, 26.82, 26.15,
25.08, 24.98, 24.62, 29.39, 30.17, 32.42, 32.56, 26.6, 27.25,
25.58, 27.88, 29.17, 31.58, 28.48, 31.24, 33.73, 32.78, 33.09,
33.43, 29.73, 31.91, 30.65, 32.98, 24.15, 24.24, 27.57, 25.44,
26.68, 26.39, 27.43, 28.87, 35.44, 36.29, 35.71, 36.06, 26.79,
27.76, 26.82, 29.71, 28.69, 26.9, 31.12, 29.77), EJH = c(17.6,
18.58, 21.11, 26.66, 26.69, 28.08, 28.38, 14.99, 14.39, 11.41,
14.33, 13.8, 12.34, 13.29, 13.67, 17.58, 17.5, 22.03, 22.19,
14.03, 15.59, 13.92, 15.39, 17.7, 19.75, 18.37, 20.3, 19.99,
18.9, 19.62, 19.61, 15.09, 18.8, 18.18, 19.6, 12.78, 13.87,
17.28, 15.06, 20.44, 20.12, 21.74, 22.52, 23.8, 26.25, 25.68,
26.73, 16.99, 18.13, 16.42, 18.82, 14.09, 13.43, 16.61, 16.9
), Weight = c(62.94, 62.45, 63.27, 68.54, 69.57, 68.38, 67.65,
52.76, 52.54, 52.17, 52.81, 71.13, 71.78, 70.31, 70.5, 87.61,
86.26, 86.78, 86.87, 66.88, 67.82, 68.72, 68.11, 63.92, 64.54,
64.71, 63.72, 65.02, 65.84, 66.16, 65.94, 56.91, 57.44, 58.09,
57.2, 70.74, 70.93, 69.79, 69.81, 65.39, 67.39, 67.71, 67.31,
60.67, 60.88, 60.31, 59.72, 53.27, 52.84, 54.15, 53.39, 79.61,
80.54, 79.12, 79.08)), class = "data.frame", row.names = c(NA,
-55L))
```
checkboxGroupInput("Program", label = "Program", choices = unique(WHOC_Sum_CMJ$Program), selected = "Red", inline = TRUE)
# (Note: for the code I cut out some of the styling to make it more readable. That's why it looks different than the pictures).
```
```
renderPlot({
f <- WHOC_Sum_CMJ %>%
select(Date, Athlete, JumpHeight_cm, Program)%>%
filter(Program == input$Program)
p <- ggplot(f)+
geom_line(aes(x=Date, y=JumpHeight_cm, colour = Athlete))+
geom_point(aes(x=Date, y=JumpHeight_cm, colour = Athlete))+
theme_bw() +
labs(title = "Team Jump Height",
x = "Date",
y = "Jump Height (cm)")+
scale_x_date(limits = c(min = min(WHOC_Sum_CMJ$Date), max = max(WHOC_Sum_CMJ$Date)), labels = date_format("%m/%d"),
date_breaks = "2 weeks", expand = c(.08,0))+
guides(col = guide_legend(nrow = 3))+
geom_text_repel(data= subset(f, Date == min(Date)), aes(x=Date, y=JumpHeight_cm,label = unique(Athlete)),
force = .1,
nudge_x = -2,
direction = "y",
hjust = 1,
)
p
})
The issue in your code indeed is based on the filter call. You'll need to use %in%instead of ==, when filtering a vector of statements. Please see the following:
---
title: "Test"
output: flexdashboard::flex_dashboard
runtime: shiny
---
```{r global, include=FALSE}
library(ggplot2)
library(dplyr)
library(scales)
library(ggrepel)
WHOC_Sum_CMJ <- structure(list(Athlete = structure(c(1L, 1L, 1L, 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, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L), .Label = c("Athlete 1", "Athlete 10",
"Athlete 11", "Athlete 12", "Athlete 13", "Athlete 14", "Athlete 2",
"Athlete 3", "Athlete 4", "Athlete 5", "Athlete 6", "Athlete 7",
"Athlete 8", "Athlete 9"), class = "factor"), Date = structure(c(1L,
4L, 5L, 1L, 3L, 5L, 7L, 2L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L,
5L, 7L, 1L, 3L, 6L, 7L, 2L, 4L, 5L, 8L, 1L, 3L, 5L, 7L, 1L, 3L,
5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L,
6L, 7L, 1L, 3L, 5L, 7L), .Label = c("2020-01-06", "2020-01-07",
"2020-01-13", "2020-01-14", "2020-01-21", "2020-01-23", "2020-01-27",
"2020-01-28"), class = "factor"), Position = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("DEF", "FWD", "GOALIE"), class = "factor"),
Program = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("Navy", "Red", "RTP", "White"), class = "factor"),
mRSI = c(0.36, 0.38, 0.42, 0.46, 0.46, 0.47, 0.48, 0.31,
0.3, 0.24, 0.3, 0.29, 0.26, 0.28, 0.28, 0.36, 0.35, 0.43,
0.43, 0.28, 0.31, 0.28, 0.3, 0.33, 0.36, 0.35, 0.37, 0.37,
0.36, 0.37, 0.36, 0.3, 0.36, 0.34, 0.37, 0.26, 0.28, 0.34,
0.3, 0.39, 0.4, 0.43, 0.43, 0.43, 0.47, 0.46, 0.48, 0.34,
0.36, 0.33, 0.37, 0.28, 0.28, 0.34, 0.33), SystemWeight = c(617.21,
612.4, 620.45, 672.08, 682.23, 670.5, 663.41, 517.33, 515.23,
511.62, 517.85, 697.55, 703.92, 689.43, 691.33, 859.06, 845.9,
850.97, 851.84, 655.79, 665.09, 673.91, 667.92, 626.78, 632.92,
634.52, 624.88, 637.55, 645.6, 648.78, 646.64, 558.03, 563.23,
569.58, 560.95, 693.63, 695.54, 684.37, 684.58, 641.18, 660.8,
663.95, 660, 594.92, 596.97, 591.36, 585.64, 522.35, 518.17,
530.95, 523.5, 780.65, 789.81, 775.84, 775.48), FTCT = c(0.61,
0.62, 0.67, 0.74, 0.75, 0.77, 0.77, 0.54, 0.55, 0.44, 0.53,
0.53, 0.49, 0.53, 0.56, 0.6, 0.58, 0.68, 0.68, 0.53, 0.57,
0.54, 0.55, 0.61, 0.63, 0.64, 0.65, 0.59, 0.58, 0.59, 0.59,
0.51, 0.59, 0.59, 0.59, 0.53, 0.57, 0.63, 0.59, 0.76, 0.76,
0.79, 0.78, 0.67, 0.72, 0.72, 0.74, 0.63, 0.65, 0.61, 0.63,
0.49, 0.5, 0.53, 0.57), JumpHeight_cm = c(28.97, 29.78, 31.43,
35.83, 35.41, 36.59, 36.92, 27.56, 26.11, 26.15, 26.82, 26.15,
25.08, 24.98, 24.62, 29.39, 30.17, 32.42, 32.56, 26.6, 27.25,
25.58, 27.88, 29.17, 31.58, 28.48, 31.24, 33.73, 32.78, 33.09,
33.43, 29.73, 31.91, 30.65, 32.98, 24.15, 24.24, 27.57, 25.44,
26.68, 26.39, 27.43, 28.87, 35.44, 36.29, 35.71, 36.06, 26.79,
27.76, 26.82, 29.71, 28.69, 26.9, 31.12, 29.77), EJH = c(17.6,
18.58, 21.11, 26.66, 26.69, 28.08, 28.38, 14.99, 14.39, 11.41,
14.33, 13.8, 12.34, 13.29, 13.67, 17.58, 17.5, 22.03, 22.19,
14.03, 15.59, 13.92, 15.39, 17.7, 19.75, 18.37, 20.3, 19.99,
18.9, 19.62, 19.61, 15.09, 18.8, 18.18, 19.6, 12.78, 13.87,
17.28, 15.06, 20.44, 20.12, 21.74, 22.52, 23.8, 26.25, 25.68,
26.73, 16.99, 18.13, 16.42, 18.82, 14.09, 13.43, 16.61, 16.9
), Weight = c(62.94, 62.45, 63.27, 68.54, 69.57, 68.38, 67.65,
52.76, 52.54, 52.17, 52.81, 71.13, 71.78, 70.31, 70.5, 87.61,
86.26, 86.78, 86.87, 66.88, 67.82, 68.72, 68.11, 63.92, 64.54,
64.71, 63.72, 65.02, 65.84, 66.16, 65.94, 56.91, 57.44, 58.09,
57.2, 70.74, 70.93, 69.79, 69.81, 65.39, 67.39, 67.71, 67.31,
60.67, 60.88, 60.31, 59.72, 53.27, 52.84, 54.15, 53.39, 79.61,
80.54, 79.12, 79.08)), class = "data.frame", row.names = c(NA,
-55L))
WHOC_Sum_CMJ$Date <- as.Date(WHOC_Sum_CMJ$Date)
```
Column {.sidebar}
-----------------------------------------------------------------------
```{r}
checkboxGroupInput("Program", label = "Program", choices = unique(WHOC_Sum_CMJ$Program), selected = "Red", inline = TRUE)
# (Note: for the code I cut out some of the styling to make it more readable. That's why it looks different than the pictures).
```
Column
-----------------------------------------------------------------------
```{r}
renderPlot({
f <- WHOC_Sum_CMJ %>%
dplyr::select(Date, Athlete, JumpHeight_cm, Program) %>%
filter(Program %in% input$Program)
p <- ggplot(f) +
geom_line(aes(x=Date, y=JumpHeight_cm, colour = Athlete)) +
geom_point(aes(x=Date, y=JumpHeight_cm, colour = Athlete)) +
theme_bw() +
labs(title = "Team Jump Height",
x = "Date",
y = "Jump Height (cm)") +
scale_x_date(limits = c(min = min(WHOC_Sum_CMJ$Date), max = max(WHOC_Sum_CMJ$Date)), labels = date_format("%m/%d"),
date_breaks = "2 weeks", expand = c(.08,0)) +
guides(col = guide_legend(nrow = 3)) +
geom_text_repel(data= subset(f, Date == min(Date)), aes(x=Date, y=JumpHeight_cm,label = unique(Athlete)),
force = .1,
nudge_x = -2,
direction = "y",
hjust = 1,
)
p
})
```
Related
I have tried to follow this post to calculate a weighted proportion and standard error. However, the answer provided did not have a lot of explanation so I was unsure if my calculations were correct.
I would love confirmation that what I've done is indeed correct, or alternate ways to achieve my desired outcome if incorrect?
# Test data
test <- structure(list(koala = 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, 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), .Label = "Gendry", class = "factor"),
koala.pres = 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, 3L, 2L, 2L, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 3L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 2L, 3L,
2L, 2L, 3L, 2L, 1L, 1L, 1L, 3L, 3L, 2L, 3L, 2L, 2L, 1L, 1L,
1L, 1L), .Label = c("Absent", "Day", "Night"), class = "factor"),
habitat = 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, 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, 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("Exposed Sandstone Scribbly Gum", "Sheltered sandstone Blue leafed stringybark forest",
"Transitional Shale Dry Ironbark Forest"), class = "factor"),
tree.sp = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 7L, 7L, 7L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 12L, 12L, 13L, 13L,
13L, 13L, 13L, 14L, 14L, 14L, 14L, 2L, 2L, 2L, 7L, 7L, 7L,
9L, 13L, 1L, 1L, 1L, 2L, 2L, 10L, 11L, 11L, 13L, 13L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 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, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L), .Label = c("A. littoralis",
"C. gummifera", "E. amplifolia", "E. beyeriana", "E. crebra",
"E. fibrosa", "E. globoidea", "E. longifolia", "E. oblonga",
"E. piperita", "E. punctata", "E. resinifera", "E. sclerophylla",
"E. sieberi"), class = "factor"), cbh = c(0.76, 0.98, 0.42,
0.34, 0.4, 0.44, 0.45, 0.47, 0.66, 0.59, 0.99, 0.43, 0.35,
0.36, 0.4, 0.46, 0.52, 0.49, 0.4, 1.56, 1.26, 0.83, 1.1,
1.22, 1.04, 1.04, 1.08, 1.7, 1.35, 1.89, 0.88, 0.63, 1.26,
0.45, 1.2, 1.33, 0.41, 1.22, 0.75, 0.32, 0.52, 0.6, 1.37,
1.51, 1.29, 0.51, 0.46, 0.44, 2.35, 1.68, 1.24, 0.58, 0.53,
0.69, 0.45, 0.5, 0.5, 0.51, 1.46, 1.23, 0.32, 1.47, 2.27,
0.41, 0.59, 0.61, 0.83, 0.56, 0.41, 0.47, 0.6, 0.35, 1.91,
0.65, 0.52, 1.41, 0.95, 0.91, 1.51, 1.08, 0.95, 0.52, 1.7,
0.76, 1.03, 0.88, 1.45, 1.81, 0.4, 0.39, 0.34, 0.35, 0.89,
0.8, 1.1, 1.77, 0.52, 1.23, 0.49, 0.46, 2.27, 0.41, 1.4,
0.58, 0.66, 0.41, 0.44, 0.87, 0.51, 0.57, 0.78, 1.18, 1.41,
1.13, 1, 1.48, 1.48, 0.4, 1.8, 0.78, 0.82, 1.23, 1.51, 3.82,
0.51, 1.59, 0.95, 1.04, 1.98, 1.3, 0.88, 0.52, 1, 1.27, NA,
1.07, 0.35, 1.33, 0.45, 0.63, 0.45, 0.32, 0.56, 0.68, 1.67,
1.3, 1.83, 0.58, 0.56, 0.44, 0.9, 0.99, 0.59, 0.63, 2.53,
1.33, 2.1, 0.91, 1.24, 1.13, 1.22, 1.64, 2.35, 1.07, 1.27,
1.4, 1.88, 0.56, 1.86, 1.3, 1.97, 0.92, 1.23, 0.34, 0.8),
dbh = c(0.2419, 0.3119, 0.1337, 0.1082, 0.1273, 0.1401, 0.1432,
0.1496, 0.2101, 0.1878, 0.3151, 0.1369, 0.1114, 0.1146, 0.1273,
0.1464, 0.1655, 0.156, 0.1273, 0.4966, 0.4011, 0.2642, 0.3501,
0.3883, 0.331, 0.331, 0.3438, 0.5411, 0.4297, 0.6016, 0.2801,
0.2005, 0.4011, 0.1432, 0.382, 0.4234, 0.1305, 0.3883, 0.2387,
0.1019, 0.1655, 0.191, 0.4361, 0.4806, 0.4106, 0.1623, 0.1464,
0.1401, 0.748, 0.5348, 0.3947, 0.1846, 0.1687, 0.2196, 0.1432,
0.1592, 0.1592, 0.1623, 0.4647, 0.3915, 0.1019, 0.4679, 0.7226,
0.1305, 0.1878, 0.1942, 0.2642, 0.1783, 0.1305, 0.1496, 0.191,
0.1114, 0.608, 0.2069, 0.1655, 0.4488, 0.3024, 0.2897, 0.4806,
0.3438, 0.3024, 0.1655, 0.5411, 0.2419, 0.3279, 0.2801, 0.4615,
0.5761, 0.1273, 0.1241, 0.1082, 0.1114, 0.2833, 0.2546, 0.3501,
0.5634, 0.1655, 0.3915, 0.156, 0.1464, 0.7226, 0.1305, 0.4456,
0.1846, 0.2101, 0.1305, 0.1401, 0.2769, 0.1623, 0.1814, 0.2483,
0.3756, 0.4488, 0.3597, 0.3183, 0.4711, 0.4711, 0.1273, 0.573,
0.2483, 0.261, 0.3915, 0.4806, 1.2159, 0.1623, 0.5061, 0.3024,
0.331, 0.6303, 0.4138, 0.2801, 0.1655, 0.3183, 0.4043, NA,
0.3406, 0.1114, 0.4234, 0.1432, 0.2005, 0.1432, 0.1019, 0.1783,
0.2165, 0.5316, 0.4138, 0.5825, 0.1846, 0.1783, 0.1401, 0.2865,
0.3151, 0.1878, 0.2005, 0.8053, 0.4234, 0.6685, 0.2897, 0.3947,
0.3597, 0.3883, 0.522, 0.748, 0.3406, 0.4043, 0.4456, 0.5984,
0.1783, 0.5921, 0.4138, 0.6271, 0.2928, 0.3915, 0.1082, 0.2546
), tree.hgt = c(11.2, 9, 9.2, 6.8, 6.2, 6, 6, 6.3, 12.2,
12, 16.5, 7.4, 6.2, 9.8, 9.7, 6, 9, 7.8, 9.2, 16.6, 16.6,
13.8, 14.5, 8.4, 14.2, 15.6, 15.8, 17.8, 14.2, 17.2, 11.6,
11, 16.2, 10.6, 16.2, 14.2, 7.2, 10.2, 12.4, 9.2, 8, 16,
16.8, 15.4, 15.2, 6.6, 6.8, 7.8, 16.3, 17, 12.4, 10.8, 11,
12, 8, 9, 11.2, 14.4, 14.4, 10, 7, 15.6, 18, 6.8, 9, 6, 9.4,
10, 8.2, 8.4, 9, 6, 18.8, 12.2, 7.2, 9.4, 19.2, 14.8, 21.4,
17.4, 17.8, 11.8, 17.8, 13, 14, 14.4, 16.7, 18, 7, 7.2, 5.5,
9.2, 9.6, 14, 16, 19.2, 11, 15.5, 7.2, 9, 19.5, 7.2, 23,
17.6, 11.8, 7.2, 7.5, 14, 11.6, 9.3, 16.8, 16.6, 15, 18.6,
22.8, 20, 19.8, 9, 18.2, 14, 19.2, 16.4, 19.8, 5.8, 11.8,
17.6, 17.8, 14.6, 17.6, 16.9, 16.3, 10.8, 17.8, 17, 20, 15,
8.4, 20.6, 9.2, 14, 8.5, 8.2, 11.2, 6.6, 18.4, 18.4, 21,
9.8, 9.2, 9, 15.2, 17.2, 10.4, 8.8, 19.2, 19, 25, 14.9, 19,
17.8, 11.3, 20, 23, 12, 17.9, 17.9, 15.2, 8, 17, 13, 14,
18, 19.4, 5.4, 16), rel.abu.tree.in.hr = c(18.7, 18.7, 18.7,
18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7,
18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 17.6, 17.6, 17.6, 4.78,
4.78, 4.78, 4.78, 4.78, 4.78, 4.78, 4.78, 4.78, 4.78, 4.78,
4.78, 0.74, 0.74, 2.7, 2.7, 2.7, 2.7, 2.7, 1.47, 1.47, 1.47,
1.47, 18.7, 18.7, 18.7, 17.6, 17.6, 17.6, 4.78, 2.7, 0.78,
0.78, 0.78, 18.7, 18.7, 0.26, 3.4, 3.4, 2.7, 2.7, 0.78, 0.78,
18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7, 18.7,
18.7, 0.004, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19,
9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19, 9.19,
9.19, 9.19, 9.19, 14.7, 14.7, 14.7, 14.7, 14.7, 14.7, 14.7,
14.7, 14.7, 14.7, 14.7, 14.7, 14.7, 14.7, 14.7, 17.6, 17.6,
17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6,
17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6, 17.6,
17.6, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53,
16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53,
16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 16.53, 3.4,
3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 3.4, 0.74, 0.74,
0.74, 0.74), prop.hab.class.in.hr = c(18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 18.42105263, 18.42105263, 18.42105263, 18.42105263,
18.42105263, 2.631578947, 2.631578947, 2.631578947, 2.631578947,
2.631578947, 2.631578947, 2.631578947, 2.631578947, 2.631578947,
2.631578947, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842, 78.94736842, 78.94736842,
78.94736842, 78.94736842, 78.94736842), k.pres = 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, 2L, 2L, 2L, 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, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"),
rel_abun = c(344.473684181, 344.473684181, 344.473684181,
344.473684181, 344.473684181, 344.473684181, 344.473684181,
344.473684181, 344.473684181, 344.473684181, 344.473684181,
344.473684181, 344.473684181, 344.473684181, 344.473684181,
344.473684181, 344.473684181, 344.473684181, 344.473684181,
324.210526288, 324.210526288, 324.210526288, 88.0526315714,
88.0526315714, 88.0526315714, 88.0526315714, 88.0526315714,
88.0526315714, 88.0526315714, 88.0526315714, 88.0526315714,
88.0526315714, 88.0526315714, 88.0526315714, 13.6315789462,
13.6315789462, 49.736842101, 49.736842101, 49.736842101,
49.736842101, 49.736842101, 27.0789473661, 27.0789473661,
27.0789473661, 27.0789473661, 344.473684181, 344.473684181,
344.473684181, 324.210526288, 324.210526288, 324.210526288,
88.0526315714, 49.736842101, 2.05263157866, 2.05263157866,
2.05263157866, 49.2105263089, 49.2105263089, 0.68421052622,
8.9473684198, 8.9473684198, 7.1052631569, 7.1052631569, 61.5789473676,
61.5789473676, 1476.315789454, 1476.315789454, 1476.315789454,
1476.315789454, 1476.315789454, 1476.315789454, 1476.315789454,
1476.315789454, 1476.315789454, 1476.315789454, 1476.315789454,
0.31578947368, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 725.5263157798, 725.5263157798,
725.5263157798, 725.5263157798, 1160.526315774, 1160.526315774,
1160.526315774, 1160.526315774, 1160.526315774, 1160.526315774,
1160.526315774, 1160.526315774, 1160.526315774, 1160.526315774,
1160.526315774, 1160.526315774, 1160.526315774, 1160.526315774,
1160.526315774, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1389.473684192, 1389.473684192, 1389.473684192, 1389.473684192,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 1304.9999999826, 1304.9999999826, 1304.9999999826,
1304.9999999826, 268.421052628, 268.421052628, 268.421052628,
268.421052628, 268.421052628, 268.421052628, 268.421052628,
268.421052628, 268.421052628, 268.421052628, 58.4210526308,
58.4210526308, 58.4210526308, 58.4210526308)), row.names = c(NA,
-175L), class = "data.frame")
# Calculate a weighted proportion for test$tree.sp
# Weighting variable is test$rel.abu.tree.in.hr
# Calculate weighted proportion
library(survey)
dsurvey <- svydesign(ids = ~1, data = test, weights = ~rel.abu.tree.in.hr)
wpct <- data.frame(svymean(~tree.sp, design = dsurvey))
Outcome of above
wpct
mean SE
tree.spA. littoralis 1.830415e-03 8.345005e-04
tree.spC. gummifera 3.071812e-01 4.180415e-02
tree.spE. amplifolia 1.877349e-06 1.889682e-06
tree.spE. beyeriana 4.744530e-02 1.424895e-02
tree.spE. crebra 4.313209e-02 1.359431e-02
tree.spE. fibrosa 1.034889e-01 2.547820e-02
tree.spE. globoidea 2.395497e-01 3.825613e-02
tree.spE. longifolia 1.939536e-01 3.472975e-02
tree.spE. oblonga 2.916462e-02 8.264006e-03
tree.spE. piperita 1.220277e-04 1.228147e-04
tree.spE. punctata 1.914896e-02 5.681831e-03
tree.spE. resinifera 2.083857e-03 8.701564e-04
tree.spE. sclerophylla 1.013768e-02 3.663735e-03
tree.spE. sieberi 2.759703e-03 1.400363e-03
I'm trying to make a graph similar to this one, however when I try to add a line connecting the before and after species, only one vertical line per species appears. I added the "pair" column to identify each point in the before and after for each species.
Can anyone identify where the error is in my script? Thanks!
structure(list(species = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("1diphylla", "2calycioides", "3duckeana"
), class = "factor"), ID = c("p20", "p15", "p23", "p24", "p25",
"p26", "p3", "p5", "p7", "p8", "p9", "p10", "p11", "p14", "p13",
"p58", "p59", "p42", "p60", "p43", "p46", "p57", "p47", "p55",
"p31", "p34", "p33", "p41", "p38", "p39", "p90", "p83", "p65",
"p76", "p61", "p62", "p78", "p70", "p85", "p82", "p87", "p88",
"p89", "p63", "p79", "p15", "p20", "p23", "p24", "p25", "p26",
"p3", "p5", "p7", "p8", "p9", "p10", "p11", "p13", "p14", "p34",
"p33", "p41", "p38", "p39", "p58", "p59", "p42", "p60", "p43",
"p46", "p57", "p47", "p55", "p31", "p82", "p85", "p65", "p76",
"p61", "p62", "p78", "p70", "p63", "p79", "p83", "p90", "p87",
"p88", "p89"), trat = structure(c(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, 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), .Label = c("Controle", "Dano"), class = "factor"),
pair = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L,
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L, 45L), vol_p_ul = c(0.15, 0.13, 0, 0,
0.05, 0.3, 0.52, 0.19, 0.21, 0.02, 0.07, 0.11, 0, 0, 0.06,
0, 1.79, 0.21, 1.99, 3.23, 0.32, 0, 0.03, 3.94, 2.41, 5.46,
3.35, 1.83, 2.09, 4.75, 1.1, 0.31, 0.42, 1.43, 1.08, 0.24,
0.14, 1.23, 5.25, 0.67, 2.46, 0.35, 0.69, 0.49, 0.15, 0.11,
0.09, 0.07, 0, 0, 0, 0.13, 0, 0.42, 0.09, 0.03, 0.02, 0.07,
0.03, 0.05, 0, 2.33, 1.25, 0.69, 0.67, 0.13, 0.53, 0.02,
0.37, 1.78, 0.3, 0.02, 0.3, 0.74, 0.33, 0.59, 0.57, 0, 0.32,
0.51, 0.14, 0, 1.42, 0.2, 0.14, 0.38, 0, 0.36, 0, 0.24),
vol_f_ul = c(0.010714286, 0.01, 0, 0, 0.002173913, 0.015789474,
0.086666667, 0.027142857, 0.023333333, 0.002222222, 0.01,
0.008461538, 0, 0, 0.008571429, 0, 0.298333333, 0.019090909,
0.284285714, 0.538333333, 0.08, 0, 0.004285714, 0.4925, 0.241,
0.78, 0.67, 0.1525, 0.139333333, 0.527777778, 0.275, 0.062,
0.14, 0.089375, 0.063529412, 0.015, 0.01, 0.136666667, 0.22826087,
0.134, 0.091111111, 0.029166667, 0.115, 0.040833333, 0.011538462,
0.008461538, 0.006428571, 0.01, 0, 0, 0, 0.021666667, 0,
0.046666667, 0.01, 0.004285714, 0.001538462, 0.0175, 0.002,
0.007142857, 0, 0.466, 0.104166667, 0.046, 0.074444444, 0.010833333,
0.088333333, 0.001818182, 0.052857143, 0.296666667, 0.075,
0.003333333, 0.042857143, 0.0925, 0.033, 0.118, 0.024782609,
0, 0.02, 0.03, 0.00875, 0, 0.157777778, 0.016666667, 0.010769231,
0.076, 0, 0.013333333, 0, 0.04), brix = c(2.34, 5.34, NA,
NA, NA, 4.34, 2.34, 2.34, 3.84, NA, 8.84, 5.34, NA, NA, 11.34,
NA, 5.42, 3.42, 4.42, 4.92, 5.42, NA, NA, 7.42, 7.42, 6.42,
5.42, 4.42, 5.42, 5.42, 3.84, 3.84, 2.84, 4.34, 5.34, 4.34,
6.84, 8.84, 5.34, 3.34, 4.84, 5.34, 4.84, 7.34, 7.84, NA,
1.84, NA, NA, NA, NA, 1.84, NA, 2.84, 1.84, NA, NA, 2.84,
NA, NA, NA, 4.92, 6.42, 4.92, 4.42, NA, 3.42, NA, 4.42, 4.42,
3.92, NA, 2.42, 5.42, 1.92, 3.34, 10.34, NA, 2.34, 3.84,
3.34, NA, 5.34, 2.34, 4.84, 2.34, NA, 4.84, NA, 5.84), mg_acucar = c(0.024506123,
0.053963963, NA, NA, NA, 0.044027683, 0.024506123, 0.024506123,
0.039103418, NA, 0.089662318, 0.053963963, NA, NA, 0.116038643,
NA, 0.054763919, 0.034989639, 0.044818279, 0.049776474, 0.054763919,
NA, NA, 0.075006199, 0.075006199, 0.064826559, 0.054763919,
0.044818279, 0.054763919, 0.054763919, 0.039103418, 0.039103418,
0.029342638, 0.044027683, 0.053963963, 0.044027683, 0.069087758,
0.089662318, 0.053963963, 0.034208403, 0.048981198, 0.053963963,
0.048981198, 0.074187523, 0.079316538, NA, 0.019698858, NA,
NA, NA, NA, 0.019698858, NA, 0.029342638, 0.019698858, NA,
NA, 0.029342638, NA, NA, NA, 0.049776474, 0.064826559, 0.049776474,
0.044818279, NA, 0.034989639, NA, 0.044818279, 0.044818279,
0.039889334, NA, 0.025277999, 0.054763919, 0.020466054, 0.034208403,
0.105400363, NA, 0.024506123, 0.039103418, 0.034208403, NA,
0.053963963, 0.024506123, 0.048981198, 0.024506123, NA, 0.048981198,
NA, 0.058975978), mg_totais_p = c(0.003675918, 0.007015315,
NA, NA, NA, 0.013208305, 0.012743184, 0.004656163, 0.008211718,
NA, 0.006276362, 0.005936036, NA, NA, 0.006962319, NA, 0.098027416,
0.007347824, 0.089188376, 0.160778012, 0.017524454, NA, NA,
0.295524426, 0.180764941, 0.353953014, 0.18345913, 0.082017451,
0.114456592, 0.260128617, 0.043013759, 0.012122059, 0.012323908,
0.062959586, 0.05828108, 0.010566644, 0.009672286, 0.110284651,
0.283310804, 0.02291963, 0.120493746, 0.018887387, 0.033797026,
0.036351886, 0.011897481, NA, 0.001772897, NA, NA, NA, NA,
0.002560851, NA, 0.012323908, 0.001772897, NA, NA, 0.002053985,
NA, NA, NA, 0.115979185, 0.081033199, 0.034345767, 0.030028247,
NA, 0.018544509, NA, 0.016582763, 0.079776537, 0.0119668,
NA, 0.0075834, 0.0405253, 0.006753798, 0.020182958, 0.060078207,
NA, 0.007841959, 0.019942743, 0.004789176, NA, 0.076628827,
0.004901225, 0.006857368, 0.009312327, NA, 0.017633231, NA,
0.014154235), mg_totais_f = c(0.000262566, 0.00053964, NA,
NA, NA, 0.000695174, 0.002123864, 0.000665166, 0.000912413,
0.000896623, 0.000456618, NA, NA, NA, 0.000994617, NA, 0.016337903,
0.000667984, 0.012741197, 0.026796335, 0.004381114, NA, NA,
0.036940553, 0.018076494, 0.050564716, 0.036691826, 0.006834788,
0.007630439, 0.02890318, 0.01075344, 0.002424412, 0.004107969,
0.003934974, 0.003428299, 0.000660415, 0.000690878, 0.01225385,
0.012317861, 0.004583926, 0.004462731, 0.001573949, 0.005632838,
0.003029324, 0.000915191, NA, 0.000126636, NA, NA, NA, NA,
0.000426809, NA, 0.001369323, 0.000196989, NA, NA, 0.000513496,
NA, NA, NA, 0.023195837, 0.006752767, 0.002289718, 0.003336472,
NA, 0.003090751, NA, 0.002368966, 0.01329609, 0.0029917,
NA, 0.001083343, 0.005065663, 0.00067538, 0.004036592, 0.002612096,
NA, 0.000490122, 0.001173103, 0.000299324, NA, 0.008514314,
0.000408435, 0.00052749, 0.001862465, NA, 0.000653083, NA,
0.002359039)), row.names = c(NA, -90L), class = "data.frame")
ggplot(dat,aes(x= species, y= vol_p_ul, fill=trat)) +
geom_boxplot() +
geom_point(aes(fill=trat),position=position_jitterdodge(0.2), alpha=0.8)+
geom_line(aes(group = pair), alpha = 0.6, colour = "black")+
theme_bw() + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
theme(axis.text.x = element_text(color="black", size=14),
axis.text.y = element_text(size = 14, color="black")) +
theme(axis.title = element_text(size = 14))
I agree with the other response's use of facet_wrap and geom_line, but my take is slightly different:
the geom_point geometry is necessary since otherwise you will only get dots for the outliers
add the geom_line and geom_point after geom_boxplot, so they don't get covered
jitter the position of the points, as the example does
ggplot(data = dat, aes(x = trat, y = vol_p_ul, fill = trat)) +
geom_boxplot() +
geom_line(aes(group = pair), position=position_dodge(0.2)) +
geom_point(aes(fill=trat,group=pair), position = position_dodge(0.2)) +
facet_wrap(~species)
Are you looking for something like this?:
ggplot(data = dat, aes(x = trat, y = vol_p_ul)) +
geom_line( aes(group = pair)) +
geom_boxplot(aes(fill = trat)) +
facet_wrap(~species) +
theme_bw() + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
theme(axis.text.x = element_text(color="black", size=14),
axis.title.x = element_blank(),
axis.text.y = element_text(size = 14, color="black")) +
theme(axis.title = element_text(size = 14))
I have some Stata code and I want to replicate the results in R. However, even with the same dataset and, I think, the same code, I get different results in R from those in Stata. I think it could be because Stata makes the order of the regression different than keyed in.
Do I need exactly the same order as in Stata to get the same results and how can I do this?
I changed all the variables to factors and tried again but the problem is still there.
I noticed that when I change the order of the explanatory variables I get different results, but I don`t find "the right order" to replicate the Stata results.
Stata code:
. anova testm2 c.testm1 i.hptreat c.cortm1 c.cortm2 i.female if inelig == 0 & anyoutv1 == 0
Number of obs =39 R-squared =0.7048
Root MSE= 16.0144 Adj R-squared =0.6601
Source | Partial SS df MS F Prob>F
---------------------------------------------------------------
Model | 20209.281 5 4041.8563 15.76 0.0000
testm1 | 3516.6527 1 3516.6527 13.71 0.0008
hptreat| 1183.5007 1 1183.5007 4.61 0.0391
cortm1 | 8.5753841 1 8.5753841 0.03 0.8560
cortm2 | 2810.9353 1 2810.9353 10.96 0.0023
female | 2557.3444 1 2557.3444 9.97 0.0034
Residual| 8463.2532 33 256.46222
----------------------------------------------------------------
Total | 28672.535 38 754.54038
R code:
FosseTest<-aov(testm2~testm1+hptreat+cortm1+cortm2+female,data=X2data)
summary(FosseTest)
Df Sum Sq Mean Sq F value Pr(>F)
testm1 1 15121 15121 58.962 7.68e-09 ***
hptreat 1 524 524 2.043 0.16228
cortm1 1 23 23 0.089 0.76715
cortm2 1 1984 1984 7.735 0.00888 **
female 1 2557 2557 9.972 0.00339 **
Residuals 33 8463 256
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
You can see that I get totally different values in the replication.
in the X2data Set I already subset the values for if inelig == 0 & anyoutv1 == 0
for the reconstruction of the data:
dput(X2data)
structure(list(id = c(29L, 30L, 31L, 32L, 34L, 35L, 36L, 37L,
39L, 41L, 42L, 43L, 44L, 46L, 47L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 57L, 58L, 59L, 60L, 61L, 62L, 64L, 65L, 66L, 67L, 68L, 69L,
70L, 71L, 72L, 73L, 74L), inelig = 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), .Label = c("Analytic sample (keep)", "Ineligible (drop)"
), class = "factor"), ccydrop = c(0L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), cortm1v2 = c(0.003, 0.086, 0.047, 0.106, NA, 0.153, 0.086,
0.005, 0.133, 0.036, 0.03, 0.015, 0.014, 0.111, 0.389, 0.298,
0.4, 0.215, 0.062, 0.021, 0.075, 0.073, 0.033, 0.243, 0.126,
0.147, 0.019, 0.048, 0.28, 0.052, 0.039, 0.105, 0.111, 0.133,
0.065, 0.051, 0.143, 0.127, 0.095), cortm2v2 = c(0.025, 0.167,
0.059, 0.112, 0.171, 0.183, 0.102, 0.018, 0.08, 0.015, 0.027,
0.05, 0.025, 0.046, 0.085, 0.144, 0.155, 0.09, 0.057, 0.023,
0.038, 0.205, 0.035, 0.198, 0.112, 0.211, 0.042, 0.142, 0.328,
0.076, 0.067, 0.094, 0.245, 0.153, 0.115, 0.127, 0.257, 0.125,
0.096), cdiffv2 = c(0.022, 0.081, 0.012, 0.006, NA, 0.03, 0.016,
0.013, -0.053, -0.021, -0.003, 0.035, 0.011, -0.065, -0.304,
-0.154, -0.245, -0.125, -0.005, 0.002, -0.037, 0.132, 0.002,
-0.045, -0.014, 0.064, 0.023, 0.094, 0.048, 0.024, 0.028, -0.011,
0.134, 0.02, 0.05, 0.076, 0.114, -0.002, 0.001), testm1v2 = c(38.72,
32.77, 32.32, 17.99, 73.58, 80.69, 48.56, 21.92, 27.24, 40.93,
31.73, 60.05, 38.04, 30.17, 59.07, 26.92, 25.41, 47.81, 63.02,
34.49, 104.38, 38.08, 30.99, 35.23, 104.81, 49.33, 50.03, 11.65,
143.57, 48.31, 90.37, 48.56, 41.67, 75.23, 60.56, 39.03, 18.16,
37.9, 84.5), testm2v2 = c(62.37, 29.23, 27.51, 28.66, 44.67,
105.48, 42.67, 15.01, 21.33, 10.87, 2.14, 44.53, 35.8, 10.43,
47.54, 48.5, 38.98, 91.32, 52.94, 22.43, 58.68, 81.63, 34.79,
38.57, 94.86, 50.83, 55.75, 45.33, 111.62, 65.15, 81.08, 50.08,
44.86, 58.63, 85.85, 58.69, 16.35, 35.97, 99.08), tdiffv2 = c(23.65,
-3.54, -4.81, 10.67, -28.91, 24.79, -5.89, -6.91, -5.91, -30.06,
-29.59, -15.52, -2.24, -19.74, -11.53, 21.58, 13.57, 43.51, -10.08,
-12.06, -45.7, 43.55, 3.8, 3.34, -9.95, 1.5, 5.72, 33.68, -31.95,
16.84, -9.29000000000001, 1.52, 3.19, -16.6, 25.29, 19.66, -1.81,
-1.93, 14.58), testoutv1 = 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), .Label = c("Not selected", "Selected"), class = "factor"),
cortoutv1 = 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
), .Label = c("Not selected", "Selected"), class = "factor"),
anyoutv1 = 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
), .Label = c("Not selected", "Selected"), class = "factor"),
testoutv2 = 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
), .Label = c("Not selected", "Selected"), class = "factor"),
cortoutv2 = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Not selected", "Selected"), class = "factor"),
anyoutv2 = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Not selected", "Selected"), class = "factor"),
pose1rate = c(6L, 7L, 6L, 6L, 7L, 7L, 6L, 7L, 5L, 6L, 7L,
4L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), pose2rate = c(6L,
6L, 5L, 7L, 7L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 6L,
6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 7L, 6L, 7L, 7L, 7L,
6L, 7L, 7L, 7L, 7L, 7L, 6L, 6L), poseratem = c(6, 6.5, 5.5,
6.5, 7, 7, 6.5, 7, 5.5, 6.5, 7, 5.5, 7, 7, 7, 6, 6.5, 7,
7, 7, 6.5, 7, 7, 7, 7, 6.5, 7, 6.5, 7, 7, 7, 6.5, 7, 7, 7,
7, 7, 6.5, 6.5), saldiff = c(24.30555556, 20.83333333, 29.16666667,
18.75, 23.61111111, 34.02777778, 18.05555556, 19.44444444,
21.52777778, 15.97222222, 22.91666667, 13.88888889, 22.22222222,
25, 22.22222222, 22.22222222, 18.05555556, 17.36111111, 22.22222222,
27.08333333, 20.83333333, 24.30555556, 22.22222222, 28.47222222,
24.30555556, 25, 27.77777778, 22.22222222, 15.97222222, 24.30555556,
21.52777778, 19.44444444, 15.97222222, 15.27777778, 15.97222222,
24.30555556, 19.44444444, 24.30555556, 15.27777778), sal2manip = c(19.80555556,
16.33333333, 24.66666667, 14.25, 19.11111111, 29.52777778,
13.55555556, 14.94444444, 17.02777778, 11.47222222, 18.41666667,
9.38888889, 17.72222222, 20.5, 17.72222222, 17.72222222,
13.55555556, 12.86111111, 17.72222222, 22.58333333, 16.33333333,
19.80555556, 17.72222222, 23.97222222, 19.80555556, 20.5,
23.27777778, 17.72222222, 11.47222222, 19.80555556, 17.02777778,
14.94444444, 11.47222222, 10.77777778, 11.47222222, 19.80555556,
14.94444444, 19.80555556, 10.77777778), hptreat = structure(c(2L,
1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("0", "1"), class = "factor"),
female = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L
), .Label = c("0", "1"), class = "factor"), age = c(19L,
20L, 20L, 18L, 21L, 20L, 18L, 21L, 35L, 20L, 18L, 20L, 20L,
18L, 20L, 25L, 18L, 23L, 21L, 19L, 20L, 20L, 30L, 19L, 22L,
18L, 19L, 22L, 19L, 20L, 28L, 28L, 19L, 19L, 20L, 25L, 20L,
25L, 23L), cort1a1 = c(0.004, 0.085, 0.049, 0.107, 0.486,
0.159, 0.088, 0.004, 0.138, 0.035, 0.03, 0.018, 0.017, 0.111,
0.39, 0.292, 0.396, 0.213, 0.065, 0.022, 0.074, 0.077, 0.035,
0.241, 0.126, 0.154, 0.021, 0.05, 0.296, 0.054, 0.04, 0.109,
0.114, 0.133, 0.063, 0.055, 0.149, 0.134, 0.098), cort1a2 = c(0.001,
0.086, 0.045, 0.105, 0.482, 0.147, 0.085, 0.005, 0.127, 0.037,
0.031, 0.013, 0.011, 0.111, 0.389, 0.304, 0.405, 0.218, 0.059,
0.02, 0.076, 0.069, 0.032, 0.246, 0.126, 0.141, 0.017, 0.046,
0.264, 0.051, 0.038, 0.101, 0.109, 0.133, 0.068, 0.048, 0.137,
0.12, 0.092), cort2a1 = c(0.027, 0.174, 0.056, 0.111, 0.175,
0.179, 0.103, 0.021, 0.079, 0.014, 0.028, 0.051, 0.024, 0.051,
0.083, 0.148, 0.156, 0.086, 0.062, 0.024, 0.038, 0.209, 0.036,
0.199, 0.114, 0.207, 0.041, 0.141, 0.333, 0.078, 0.065, 0.088,
0.238, 0.157, 0.119, 0.132, 0.268, 0.132, 0.099), cort2a2 = c(0.023,
0.161, 0.062, 0.113, 0.166, 0.188, 0.101, 0.016, 0.081, 0.015,
0.026, 0.049, 0.026, 0.041, 0.086, 0.139, 0.154, 0.093, 0.052,
0.022, 0.038, 0.202, 0.034, 0.198, 0.111, 0.215, 0.042, 0.142,
0.324, 0.075, 0.068, 0.101, 0.252, 0.149, 0.111, 0.123, 0.247,
0.118, 0.093), cortm1 = c(0.0024999999, 0.085500002, 0.046999998,
0.106, 0.484, 0.153, 0.086499996, 0.0044999998, 0.13249999,
0.035999998, 0.0305, 0.0155, 0.014, 0.111, 0.38949999, 0.29800001,
0.4005, 0.2155, 0.061999999, 0.021, 0.075000003, 0.072999999,
0.033500001, 0.24349999, 0.126, 0.14749999, 0.018999999,
0.048, 0.28, 0.052499998, 0.039000001, 0.105, 0.1115, 0.133,
0.065499999, 0.0515, 0.14300001, 0.127, 0.094999999), cortm2 = c(0.025,
0.1675, 0.059, 0.112, 0.1705, 0.18350001, 0.102, 0.0185,
0.079999998, 0.0145, 0.027000001, 0.050000001, 0.025, 0.046,
0.0845, 0.1435, 0.155, 0.089500003, 0.057, 0.023, 0.037999999,
0.20550001, 0.035, 0.19850001, 0.1125, 0.211, 0.041499998,
0.1415, 0.3285, 0.076499999, 0.066500001, 0.094499998, 0.245,
0.153, 0.115, 0.1275, 0.25749999, 0.125, 0.096000001), cdiff = c(0.022500001,
0.082000002, 0.012000002, 0.0060000047, -0.31349999, 0.03050001,
0.015500002, 0.014, -0.052499995, -0.021499999, -0.0034999996,
0.034500003, 0.011, -0.064999998, -0.30500001, -0.15450001,
-0.2455, -0.12599999, -0.004999999, 0.0020000003, -0.037000004,
0.13250001, 0.0014999993, -0.044999987, -0.013500005, 0.063500002,
0.022499999, 0.093499996, 0.048500001, 0.024, 0.0275, -0.010499999,
0.13350001, 0.019999996, 0.049500003, 0.075999998, 0.11449999,
-0.0020000041, 0.001000002), test1a1 = c(39.87, 33.22, 32.52,
19.74, 78.85, 83.51, 48.37, 22.31, 28.17, 41.44, 32.92, 61.4,
40.31, 30.36, 59.44, 27.52, 26.14, 46.75, 63.73, 34.03, 98.47,
36.62, 30.26, 37.15, 105.64, 47.99, 50.15, 11.33, 149.12,
48.57, 92.04, 51.22, 42.25, 77.07, 62.75, 38.8, 17.91, 40.28,
88.47), test1a2 = c(37.58, 32.32, 32.12, 16.25, 68.31, 77.88,
48.75, 21.53, 26.32, 40.42, 30.55, 58.7, 35.78, 29.97, 58.7,
26.32, 24.69, 48.87, 62.32, 34.95, 110.29, 39.53, 31.72,
33.32, 103.99, 50.67, 49.9, 11.97, 138.02, 48.05, 88.7, 45.89,
41.08, 73.39, 58.38, 39.25, 18.41, 35.53, 80.54), test2a1 = c(64.22,
29.43, 27.98, 28.17, 46.14, 105.92, 43.68, 16.41, 21.42,
11.35, 1.66, 44.17, 38.58, 11.11, 48.57, 48.31, 39.71, 92.04,
52.73, 22.3, 58.23, 82.01, 35.76, 39.59, 94.06, 50.52, 55.82,
45.91, 115.13, 67.59, 82.97, 49.89, 45.09, 57.86, 86.76,
58.83, 16.53, 36.7, 100.4), test2a2 = c(60.53, 29.04, 27.04,
29.14, 43.2, 105.05, 41.66, 13.62, 21.25, 10.39, 2.63, 44.9,
33.02, 9.75, 46.52, 48.7, 38.25, 90.59, 53.15, 22.57, 59.14,
81.24, 33.81, 37.55, 95.66, 51.14, 55.69, 44.74, 108.1, 62.71,
79.18, 50.27, 44.63, 59.39, 84.94, 58.55, 16.16, 35.24, 97.75
), testm1 = c(38.724998, 32.77, 32.32, 17.995001, 73.580002,
80.695, 48.560001, 21.92, 27.245001, 40.93, 31.735001, 60.049999,
38.044998, 30.165001, 59.07, 26.92, 25.415001, 47.810001,
63.025002, 34.490002, 104.38, 38.075001, 30.99, 35.235001,
104.815, 49.330002, 50.025002, 11.65, 143.57001, 48.310001,
90.370003, 48.555, 41.665001, 75.230003, 60.564999, 39.025002,
18.16, 37.904999, 84.504997), testm2 = c(62.375, 29.235001,
27.51, 28.655001, 44.669998, 105.485, 42.669998, 15.015,
21.334999, 10.87, 2.145, 44.535, 35.799999, 10.43, 47.544998,
48.505001, 38.98, 91.315002, 52.939999, 22.434999, 58.685001,
81.625, 34.785, 38.57, 94.860001, 50.830002, 55.755001, 45.325001,
111.615, 65.150002, 81.074997, 50.080002, 44.860001, 58.625,
85.849998, 58.689999, 16.344999, 35.970001, 99.074997), tdiff = c(23.650002,
-3.5349998, -4.8099995, 10.66, -28.910004, 24.790001, -5.8900032,
-6.9049997, -5.9100018, -30.060001, -29.59, -15.514999, -2.2449989,
-19.735001, -11.525002, 21.585001, 13.564999, 43.505001,
-10.085003, -12.055002, -45.694996, 43.549999, 3.7950001,
3.3349991, -9.9550018, 1.5, 5.7299995, 33.675003, -31.955009,
16.84, -9.2950058, 1.5250015, 3.1949997, -16.605003, 25.285,
19.664997, -1.8150005, -1.9349976, 14.57), feelpower = structure(c(2L,
3L, 1L, 2L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 1L, 2L, 1L,
1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 4L, 3L,
4L, 3L, 1L, 3L, 4L, 2L, 2L, 3L), .Label = c("2", "3", "Not at all",
"Very much"), class = "factor"), incharge = structure(c(1L,
1L, 3L, 4L, 1L, 2L, 3L, 3L, 1L, 1L, 3L, 4L, 3L, 2L, 2L, 1L,
3L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L,
1L, 3L, 1L, 1L, 4L, 3L, 1L, 1L), .Label = c("2", "3", "Not at all",
"Very much"), class = "factor"), powm = structure(c(3L, 1L,
1L, 5L, 2L, 4L, 6L, 6L, 1L, 1L, 6L, 7L, 6L, 3L, 4L, 2L, 1L,
4L, 4L, 3L, 2L, 4L, 2L, 2L, 3L, 3L, 3L, 4L, 1L, 5L, 1L, 4L,
6L, 2L, 1L, 7L, 2L, 3L, 1L), .Label = c("1.5", "2", "2.5",
"3", "3.5", "Not at all", "Very much"), class = "factor"),
diceroll = structure(c(2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L
), .Label = c("No", "Yes"), class = "factor")), row.names = c(2L,
3L, 4L, 5L, 7L, 8L, 9L, 10L, 12L, 14L, 15L, 16L, 17L, 19L, 20L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 30L, 31L, 32L, 33L, 34L, 35L,
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L), class = "data.frame")
You can get the same results in R using drop1(FosseTest, test = "F"). This will test the effect of leaving one of the variables off the aov.
drop1(FosseTest, test = "F")
#
# Single term deletions
#
# Model:
# testm2 ~ testm1 + hptreat + cortm1 + cortm2 + female
# Df Sum of Sq RSS AIC F value Pr(>F)
# <none> 8463.3 221.82
# testm1 1 3516.7 11979.9 233.37 13.7122 0.0007751 ***
# hptreat 1 1183.5 9646.8 224.92 4.6147 0.0391333 *
# cortm1 1 8.6 8471.8 219.86 0.0334 0.8560279
# cortm2 1 2810.9 11274.2 231.00 10.9604 0.0022605 **
# female 1 2557.3 11020.6 230.11 9.9716 0.0033895 **
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
summary(FosseTest) displays the sequential effect of addeding the variables one after another.
There was a different way how to access this, but at the moment I can't remember...
I am working with a dataset in which I need to compare ordinal data to continuous data in a different column. i.e, individals were categorized (by age, actually) and I need to compare different age ranges to two different test values. I have been attempting to run a multifactor anova, and have had no luck.
First, I subset each age category and tried this:
aov.first.molar<-aov(carbon.combo~first.m.cat.1+first.m.cat.2+first.m.cat.3+first.m.cat.4+first.m.cat.5)
Error in model.frame.default(formula = carbon.combo ~ first.m.cat.1 + :
invalid type (list) for variable 'first.m.cat.1'
So the subsets didn't work, so I tried just using the column headers, just to see if it would magically organize by category...
> aov.albania.first<-aov(albania$AgeCat_first~albania$juv_deltaC_dentine+albania$Adult_deltaC_collagen)
Warning messages:
1: In model.response(mf, "numeric") :
using type = "numeric" with a factor response will be ignored
2: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors
> summary(aov.albania.first)
Error in levels(x)[x] : only 0's may be mixed with negative subscripts
That obviously didn't work either, and I am not sure what I am doing wrong. I set everything as a factor, and I don't understand why the code is not working.
I am wondering if it has something to do with the fact that the nature of my test data is negative. I am not sure how to fix that without altering the data
Here is my data, as requested. I am sorry it's so messy, I am not sure how to format it better. Turning it into a matrix helped, but I am still having problems with anov and ggplot not being able to find certain things that I already turned into factors...
structure(list(Number = structure(1:10, .Label = c("142-c-1",
"142-c-3", "142-c-5", "156-c-1", "156-c-4", "156-c-6", "157-c-1",
"157-c-3", "157-c-5", "157-c-6", "158-c-3", "158-c-6", "178-c-1/A",
"178-c-2/A", "178-c-2/b", "178-c-3/b", "178-c-4/b", "186-c-2/a",
"186-c-2/b", "186-c-3/b", "186-c-4/b", "186-c-5/b", "186-c-6/b",
"192-c-1", "192-c-2", "192-c-3", "192-c-4", "192-c-5", "205-c-1",
"205-c-2", "205-c-3", "205-c-4", "205-c-5", "205-c-6", "210-c-1",
"210-c-2", "210-c-3", "210-c-4", "210-c-5", "215-c-1", "215-c-2",
"215-c-3", "215-c-4", "215-c-5", "215-c-6", "215-c-7", "270-c-1",
"270-c-2", "270-c-3", "270-c-4", "270-c-5", "295-c-1", "295-c-3",
"295-c-4", "353-c-2", "353-c-3", "353-c-4", "353-c-5", "353-c-6",
"382-c-1", "390-c-1", "390-c-2", "390-c-3"), class = "factor"),
ToothID = structure(c(3L, 3L, 3L, 8L, 8L, 8L, 7L, 7L, 7L,
7L), .Label = c("LI2", "LM1", "LM1-2", "LM3", "LP1-2", "M2",
"RM1-2", "RM2"), class = "factor"), sex = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("F", "M"), class = "factor"),
Al.Qahtani.category = structure(c(2L, 5L, 8L, 2L, 5L, 8L,
2L, 6L, 7L, 8L), .Label = c("AC", "CR 1/2", "CR 3/4", "CRC",
"R 1/2", "R 1/4", "R 3/4", "RC", "Ri ", "unk"), class = "factor"),
AgeCat_first = structure(c(1L, 2L, 3L, 2L, 3L, 4L, 1L, 2L,
2L, 3L), .Label = c("1", "2", "3", "4", "5"), class = "factor"),
AgeCat_second = c(2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 4L),
sample_age_first = structure(c(9L, 18L, 23L, 17L, 27L, 6L,
10L, 13L, 21L, 23L), .Label = c("10.5 to 16.5", "11.5 to 14.5",
"11.5 to 15.5", "11.5 to 18.5", "11.5 to 19.5", "12.5 to 15.5",
"12.5 to 19.5", "15.5 to 20.5", "1.5 to 2.5", "1.5 to 3.5",
"17.5 to 22.5", "2.5 to 4.5", "3.5 to 6.5", "3.5 to 7.5",
" 4.5 to 6.5 ", "4.5 to 6.5", "4.5 to 7.5", "4.5 to 8.5",
"6.5 to 11.5", "6.5 to 8.5", "6.5 to 9.5", "7.5 to 10.5",
"8.5 to 10.5", "8.5 to 11.5", "8.5 to 12.5", "9.5 to 12.5",
"9.5 to 13.5", "9.5 to 15.5", "unk"), class = "factor"),
sample_age_second = structure(c(16L, 25L, 7L, 15L, 26L, 7L,
15L, 22L, 2L, 7L), .Label = c("10.5 to 16.5", "11.5 to 13.5",
"11.5 to 14.5", "11.5 to 15.5", "11.5 to 18.5", "11.5 to 19.5",
"12.5 to 15.5", "12.5 to 19.5", "14.5 to 17.5", "15.5 to 20.5",
"1.5 to 3.5", "17.5 to 22.5", "3.5 to 6.5", "4.5 to 6.5",
"4.5 to 7.5", "4.5 to7.5", " 5.5 to 6.5 ", "6.5 to 11.5",
"6.5 to 8.5", "6.5 to 9.5", "7.5 to 11.5", "7.5 to 12.5",
"8.5 to 12.5", "9.5 to 12.5", "9.5 to12.5", "9.5 to 13.5",
"9.5 to 15.5", "unk"), class = "factor"), AgeCat_adult = c(9L,
9L, 9L, 8L, 8L, 8L, 7L, 7L, 7L, 7L), age_at_death = structure(c(3L,
3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("18-30",
"31-45", ">45", "Adolescent", "Ind"), class = "factor"),
weight_percent_.N = c(11.5, 6.6, 6.8, 7.8, 8.7, 9.4, 5.6,
5.6, 9.1, 3.9), weight_percent_C = c(37.8, 26.2, 29.5, 32.7,
34.7, 34.4, 22, 30.7, 46.8, 22.7), juv_deltaN_dentine = c(4.54,
4.45, NA, 4.03, 5.73, 6.81, 5.03, 4.58, 0.3, NA), juv_deltaC_dentine = c(-22.042,
-22.865, -24.345, -23.557, -23.24, -22.282, -22.85, -22.697,
-25.439, -25.776), juv_proxy = c(7.958, 7.135, 5.655, 6.443,
6.76, 7.718, 7.15, 7.303, 4.561, 4.224), Adult_deltaC_collagen = c(-18.62,
-18.62, -18.62, -18.9, -18.9, -18.9, -18.64, -18.64, -18.64,
-18.64), adult_proxy = c(11.38, 11.38, 11.38, 11.1, 11.1,
11.1, 11.36, 11.36, 11.36, 11.36), Adult_deltaC_apatite = c(12.29,
12.29, 12.29, -10.23, -10.23, -10.23, -10.73, -10.73, -10.73,
-10.73), Adult_deltaN = c(-18.62, -18.62, -18.62, -18.9,
-18.9, -18.9, -18.64, -18.64, -18.64, -18.64), apatite_collagen_spacing = c(8.66,
8.66, 8.66, 7.67, 7.67, 7.67, 7.74, 7.74, 7.74, 7.74), Adult_percent_C = structure(c(2L,
2L, 2L, 6L, 6L, 6L, 7L, 7L, 7L, 7L), .Label = c("14.31%",
"22.35%", "33.96%", "34.58%", "36.60%", "39.07%", "39.51%",
"42.12%", "42.17%", "42.29%", "42.81%", "44.01%", "44.72%",
"45.52%"), class = "factor"), Adult_percent_N = structure(c(14L,
14L, 14L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), .Label = c("12.16%",
"12.30%", "13.04%", "13.78%", "14.20%", "14.89%", "14.97%",
"15.13%", "15.18%", "15.66%", "15.85%", "16.10%", "4.60%",
"7.98%"), class = "factor"), Adult_CN_ratio = c(3.27, 3.27,
3.27, 3.31, 3.31, 3.31, 3.25, 3.25, 3.25, 3.25), delta_18O = c(-5.5,
-5.5, -5.5, -4.79, -4.79, -4.79, -5.39, -5.39, -5.39, -5.39
), CP = c(0.17, 0.17, 0.17, 0.21, 0.21, 0.21, 0.2, 0.2, 0.2,
0.2), IR_SF = c(3.33, 3.33, 3.33, 3.12, 3.12, 3.12, 3.19,
3.19, 3.19, 3.19), adult_bone_sampled = structure(c(2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("femur", "humerus",
"occipital", "temporal", "tibia"), class = "factor")), .Names = c("Number",
"ToothID", "sex", "Al.Qahtani.category", "AgeCat_first", "AgeCat_second",
"sample_age_first", "sample_age_second", "AgeCat_adult", "age_at_death",
"weight_percent_.N", "weight_percent_C", "juv_deltaN_dentine",
"juv_deltaC_dentine", "juv_proxy", "Adult_deltaC_collagen", "adult_proxy",
"Adult_deltaC_apatite", "Adult_deltaN", "apatite_collagen_spacing",
"Adult_percent_C", "Adult_percent_N", "Adult_CN_ratio", "delta_18O",
"CP", "IR_SF", "adult_bone_sampled"), row.names = c(NA, 10L), class = "data.frame")
Your data corresponds to the second question, and so does this answer.
The way the aov function works is by measuring response as dependent on the categories. The formula thus needs to be designed as variable ~ factor.
aov.albania.first <- aov(juv_deltaC_dentine + Adult_deltaC_collagen ~ AgeCat_first,
data = albania)
summary(aov.albania.first)
Df Sum Sq Mean Sq F value Pr(>F)
AgeCat_first 3 6.480 2.160 1.667 0.272
Residuals 6 7.773 1.296
The problem with the first question might be similar to this. Further, check str(first.m.cat.1) and reformat the variable to vector.
Data Sets
> dput(head(spdistuc,50))
structure(list(Lane = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L), Vehicle.class = c(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),
speedmph = c(0, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2,
30.6, 34, 37.4, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2,
30.6, 34, 37.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 30.6,
34, 0, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 30.6,
34, 37.4, 0, 3.4, 6.8, 10.2, 13.6, 17), cprob = c(0, 0.01,
0.04, 0.08, 0.14, 0.22, 0.32, 0.5, 0.73, 0.95, 0.99, 1, 0,
0, 0.03, 0.07, 0.16, 0.3, 0.51, 0.81, 0.99, 1, 1, 0, 0.03,
0.05, 0.1, 0.21, 0.49, 0.84, 1, 1, 0, 0, 0.01, 0.01, 0.06,
0.1, 0.17, 0.4, 0.76, 0.95, 1, 1, 0, 0, 0.01, 0.01, 0.02,
0.04)), .Names = c("Lane", "Vehicle.class", "speedmph", "cprob"
), row.names = c(6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L,
40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 64L, 65L, 66L, 67L,
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 88L, 89L, 90L, 91L, 92L,
93L), class = "data.frame")
> dput(head(cspdistuv,50))
structure(list(lanem = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), cars.speedmph = c(18,
20, 22, 24, 26, 28, 30, 32, 34, 36, 10, 15, 20, 25, 30, 35, 5,
10, 15, 20, 25, 30, 35, 0, 5, 10, 15, 20, 25, 30, 35, 5, 10,
15, 20, 25, 30, 35), cars.prob = c(0, 0.13, 0.17, 0.2, 0.2, 0.27,
0.37, 0.8, 0.97, 1, 0, 0.03, 0.13, 0.4, 0.77, 1, 0, 0.03, 0.17,
0.27, 0.5, 0.8, 1, 0, 0.03, 0.1, 0.27, 0.53, 0.6, 0.83, 1, 0,
0.07, 0.17, 0.33, 0.53, 0.8, 1)), .Names = c("lanem", "cars.speedmph",
"cars.prob"), row.names = c(NA, 38L), class = "data.frame")
Problem
I plotted the spdistuc:
cu1 <- ggplot() + geom_point(data = spdistuc, mapping = aes(x=speedmph, y = cprob, color = 'observed')) + facet_wrap(~Lane) + theme_bw() + my.theme()
This gave me following:
But when I added another plot on the existing one,
cu2 <- cu1 + geom_point(data = cspdistuv, mapping = aes(x = cars.speedmph, y = cars.prob, color = 'simulated-default')) + facet_wrap(~lanem)
I got the following:
Question
Why the existing plot ("observed") changed? You can see more than 1 point for a single value on x-axis. What am I doing wrong?
Expanding my comment into an answer:
The problem is you use "Lane" in the first dataset and "lanem" in the second.
This can be fixed by making the column names the same.
names(cspdistuv)[names(cspdistuv) == "lanem"] <- "Lane"
When this change is made, you should not need to include facet_wrap in your cu2 definition. It will still be remembered from cu1's definition.