Remove unused nodes in ggraph - r

I have the following data.frame.
library(tidyverse)
library(ggraph)
library(tidygraph)
df <- structure(list(from = c(3L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L,
2L, 1L, 3L, 1L, 3L, 3L, 3L, 1L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L,
1L, 3L, 1L, 1L, 2L, 1L, 2L, 3L, 1L, 1L, 3L, 1L, 1L, 3L, 1L, 2L,
3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 1L, 3L, 3L, 3L, 3L, 1L, 2L, 1L,
3L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 1L,
1L, 1L, 3L, 3L, 2L, 3L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L), to = c(31L, 21L, 5L,
97L, 68L, 49L, 3L, 84L, 17L, 335L, 1L, 6L, 207L, 2L, 3L, 457L,
3L, 149L, 17L, 3L, 41L, 126L, 89L, 150L, 42L, 262L, 235L, 79L,
335L, 2L, 104L, 445L, 10L, 79L, 5L, 7L, 3L, 39L, 6L, 402L, 123L,
104L, 246L, 448L, 261L, 44L, 47L, 170L, 158L, 435L, 39L, 47L,
11L, 31L, 18L, 236L, 144L, 237L, 106L, 236L, 19L, 393L, 104L,
6L, 440L, 191L, 171L, 302L, 255L, 134L, 197L, 373L, 68L, 10L,
1L, 6L, 461L, 28L, 216L, 473L, 108L, 238L, 79L, 1L, 11L, 178L,
432L, 5L, 3L, 91L, 449L, 26L, 123L, 456L, 73L, 1L, 105L, 432L,
160L, 10L), time = c(2, 1, 3, 3, 1, 3, 1, 1, 1, 2, 3, 1, 1, 1,
1, 3, 3, 2, 1, 1, 1, 3, 1, 1, 2, 2, 2, 3, 2, 1, 3, 3, 1, 3, 3,
1, 2, 1, 1, 3, 3, 2, 2, 3, 3, 1, 1, 3, 1, 3, 1, 1, 1, 3, 3, 2,
1, 3, 3, 2, 3, 3, 1, 1, 3, 1, 1, 3, 2, 3, 3, 3, 1, 2, 2, 2, 3,
1, 1, 3, 1, 2, 3, 1, 1, 3, 3, 1, 3, 1, 3, 2, 3, 3, 2, 3, 1, 3,
3, 1)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-100L))
g <- df %>% graph_from_data_frame()
And this code to produce a ggraph
g %>%
as_tbl_graph() %>%
activate(nodes) %>%
mutate(degree = centrality_degree()) %>%
ggraph()+
geom_edge_fan(aes(color = time))+
geom_node_point(aes(size = degree),
show.legend = F)+
coord_equal()+
facet_edges(~time, drop = T)+
theme_graph()
Which produces this faceted plot:
My question is: How can I remove the edgesless nodes in the facets of the plot, so that only nodes that have edges appear in each facet?
Thanks!

Related

R: Trying to remove NAs from a boxplot

I am trying to do a basic boxplot, and I can't get NA values away from it. I have tried many tricks for this issue. For example this one.
ggplot(df=subset(df, !is.na(sum_variable)), aes(x = gender, y = sum_variable, fill = gender)) +
stat_boxplot(geom ="errorbar", width = 0.5) +
geom_boxplot(fill = "light blue") +
stat_summary(fun.y=mean, geom="point", shape=10, size=3.5, color="black") +
ggtitle("Title") +
theme_bw() + theme(legend.position="none")
And this ggplot(na.omit(data), aes(x=luse, y=rich)) +
And none of these solve the issue. What would you recommend?
Data↓
structure(list(gender = structure(c(2L, 2L, NA, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 1L, 2L, 1L, 1L), .Label = c("1", "2"), class = "factor"),
sum_variable = c(9, 6, 13, 3, 4, 3, 12, 2, 7, 8, 7, 4, 5,
10, 2, 5, 4, NA, 14, 9, 2, 5, 7, 3, NA, 3, 5, 7, 3, 8, 3,
3, 4, 8, 10, 9, 5, 7, 8, 4, 9, NA, 10, 14, 10, 3, 4, 10,
3, NA, 5, 3, 4, 4, NA, 5, 4, 6, 6, 9, 6, 2, 3, NA, 4, NA,
2, 2, 6, 5, 5, 3, 5, NA, 4, 4)), class = c("rowwise_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -76L), groups = structure(list(
.rows = structure(list(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, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L,
54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L,
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L,
76L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -76L), class = c("tbl_df",
"tbl", "data.frame")))
You can filter your NA values before creating the plot:
df %>%
filter(!is.na(gender)) %>%
ggplot(aes(x = gender, y = sum_variable, fill = gender)) +
stat_boxplot(geom ="errorbar", width = 0.5) +
geom_boxplot(fill = "light blue") +
stat_summary(fun.y=mean, geom="point", shape=10, size=3.5, color="black") +
ggtitle("Title") +
theme_bw() + theme(legend.position="none")
Try removing NAs first before passing in the dataset.
sub_dta = na.omit(dta)
ggplot(data = sub_dta, aes(x = gender, y = sum_variable, fill = gender)) +
stat_boxplot(geom ="errorbar", width = 0.5) +
geom_boxplot(fill = "light blue") +
stat_summary(fun =mean, geom="point", shape=10, size=3.5, color="black") +
ggtitle("Title") +
theme_bw() + theme(legend.position="none")
One solution is to use complete.cases
ggplot(df[complete.cases(df), ], aes(x = gender, y = sum_variable,
fill = gender)) +
stat_boxplot(geom ="errorbar", width = 0.5) +
geom_boxplot(fill = "light blue") +
stat_summary(fun.y=mean, geom="point", shape=10, size=3.5, color="black") +
ggtitle("Title") +
theme_bw() + theme(legend.position="none")
Data
df <- structure(list(gender = structure(c(2L, 2L, NA, 2L, 1L, 1L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 1L, 2L, 1L, 1L), levels = c("1", "2"), class = "factor"),
sum_variable = c(9, 6, 13, 3, 4, 3, 12, 2, 7, 8, 7, 4, 5,
10, 2, 5, 4, NA, 14, 9, 2, 5, 7, 3, NA, 3, 5, 7, 3, 8, 3,
3, 4, 8, 10, 9, 5, 7, 8, 4, 9, NA, 10, 14, 10, 3, 4, 10,
3, NA, 5, 3, 4, 4, NA, 5, 4, 6, 6, 9, 6, 2, 3, NA, 4, NA,
2, 2, 6, 5, 5, 3, 5, NA, 4, 4)), class = c("rowwise_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -76L), groups = structure(list(
.rows = structure(list(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, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L,
54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L,
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L,
76L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -76L), class = c("tbl_df",
"tbl", "data.frame")))

How to bring model predict line in front of geom_rect

I am investigating the effect of time since fire on species diversity. I am attempting to make a graph that has different colours at different time since fire ages. However, putting the colours onto the graph has made the model prediction line fade away. I am wondering if there is some way to bring the line in front of geom_rect?
Loaded packages:
library(voxel)
library(gamm4)
library(ggplot2)
My data:
data <- read.csv('StacksOverflow.csv')
structure(list(Lscape = c(158L, 158L, 158L, 158L, 158L, 158L),
TSF = c(5, 5, 5, 18.5, 5, 18.5), VegtypeNew = structure(c(1L,
1L, 1L, 2L, 1L, 1L), .Label = c("spinsandplain", "woodlndsandplain"
), class = "factor"), FF = c(2L, 2L, 2L, 1L, 2L, 1L), ThreeYearRain = c(913.799997,
913.799997, 913.799997, 913.799997, 913.799997, 913.799997
), Div = c(2.2629743, 1.9630117, 1.7336569, 1.2816843, 2.4155056,
1.4240443), triodia_low = c(19L, 6L, 21L, 32L, 11L, 32L)), row.names = c(NA,
6L), class = "data.frame")
Extended data:
structure(list(Lscape = c(158L, 158L, 158L, 158L, 158L, 158L,
158L, 158L, 201L, 201L, 201L, 201L, 201L, 201L, 201L, 201L, 235L,
235L, 235L, 235L, 235L, 235L, 235L, 235L, 237L, 237L, 237L, 237L,
237L, 237L, 237L, 237L, 254L, 254L, 254L, 254L, 254L, 254L, 254L,
254L, 287L, 287L, 287L, 287L, 287L, 287L, 287L, 287L, 304L, 304L,
304L, 304L, 304L, 304L, 304L, 304L, 311L, 311L, 311L, 311L, 311L,
311L, 311L, 311L, 312L, 312L, 312L, 312L, 312L, 312L, 312L, 312L,
323L, 323L, 323L, 323L, 323L, 323L, 323L, 323L, 326L, 326L, 326L,
326L, 326L, 326L, 326L, 326L, 327L, 327L, 327L, 327L, 327L, 327L,
327L, 327L, 337L, 337L, 337L, 337L, 337L, 337L, 337L, 337L, 355L,
355L, 355L, 355L, 355L, 355L, 355L, 355L, 370L, 370L, 370L, 370L,
370L, 370L, 370L, 370L, 379L, 379L, 379L, 379L, 379L, 379L, 379L,
379L, 411L, 411L, 411L, 411L, 411L, 411L, 411L, 411L, 414L, 414L,
414L, 414L, 414L, 414L, 414L, 414L, 435L, 435L, 435L, 435L, 435L,
435L, 435L, 435L, 437L, 437L, 437L, 437L, 437L, 437L, 437L, 437L,
438L, 438L, 438L, 438L, 438L, 438L, 438L, 438L, 447L, 447L, 447L,
447L, 447L, 447L, 447L, 447L, 452L, 452L, 452L, 452L, 452L, 452L,
452L, 452L), TSF = c(5, 5, 5, 18.5, 5, 18.5, 18.5, 18.5, 11.5,
4.5, 0.5, 20, 11.5, 0.5, 1, 4.5, 1, 1, 4.5, 5, 4.5, 2, 5, 1,
6, 6, 4.5, 6, 14.5, 17, 4.5, 6, 1, 1, 7, 4.5, 2, 2, 7, 7, 20,
4, 3.5, 4, 3.5, 3.5, 11.5, 20, 6, 0.5, 5, 6, 6, 0.5, 7, 6, 3.5,
3.5, 3.5, 11.5, 11.5, 1, 1, 11.5, 1, 1, 4, 1, 1, 4, 1, 10.5,
7, 17.5, 0.5, 0.5, 0.5, 17.5, 7, 0.5, 18, 1.5, 3.5, 18, 18, 5,
3.5, 18.5, 14.5, 1.5, 7, 1.5, 7, 7, 7, 7, 10.5, 1.5, 0, 1.5,
7, 3, 7, 10.5, 0.5, 20, 0.5, 2, 2, 1.5, 2, 3, 20, 1, 1.5, 10.5,
17, 1.5, 1.5, 10.5, 3, 1, 1, 1, 4.5, 1, 6.5, 1, 10, 1.5, 12.5,
1.5, 1.5, 1.5, 1.5, 1.5, 2, 7, 12.5, 2, 7, 2, 2, 2, 1.5, 18.5,
18.5, 1.5, 7, 1.5, 1.5, 5, 12.5, 6.5, 1.5, 1.5, 1.5, 1.5, 1.5,
1.5, 6.5, 6.5, 1.5, 6.5, 18.5, 6.5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, 5L, 3L, 4L, 4L, 4L, 2L, 1L, 2L, 2L, 3L, 4L, 4L, 3L, 2L, 3L,
3L, 3L, 4L, 3L, 3L, 3L, 4L, 2L, 6L, 6L, 6L, 6L, 5L, 2L, 7L, 3L,
2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 3L, 3L, 4L, 4L, 3L, 4L, 3L, 3L,
4L, 5L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 4L, 3L, 6L, 4L, 4L, 3L, 3L,
4L, 0L, 2L, 4L, 3L, 2L, 3L, 3L, 0L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
3L, 2L, 5L, 6L, 3L, 3L, 3L, 3L, 2L, 4L, 3L, 4L, 5L, 4L, 3L, 3L,
6L, 4L, 3L, 5L, 5L, 5L, 5L, 4L, 3L, 2L, 1L, 2L, 2L, 3L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 3L,
2L, 2L, 2L, 3L, 2L, 4L, 2L, 3L, 4L, 5L, 5L, 3L, 4L, 3L, 3L, 4L
), ThreeYearRain = c(913.799997, 913.799997, 913.799997, 913.799997,
913.799997, 913.799997, 913.799997, 913.799997, 938.899988, 938.899988,
938.899988, 938.600004, 938.899988, 938.899988, 938.899988, 938.499989,
930.700005, 932.800001, 930.700005, 930.700005, 932.800001, 930.700005,
930.700005, 932.800001, 932.699991, 934.799987, 934.799987, 934.799987,
932.699991, 934.799987, 932.699991, 934.799987, 896.99999, 896.99999,
908.999991, 908.999991, 908.999991, 908.999991, 910.199991, 898.399994,
928.000009, 939.800006, 935.500004, 928.000009, 928.000009, 923.700007,
931.499996, 931.499996, 866.200004, 866.200004, 867.000003, 867.000003,
867.000003, 867.300002, 867.000003, 869.3, 926.800003, 926.800003,
926.800003, 926.800003, 933.800003, 934.600006, 934.600006, 934.600006,
924.2, 925.100002, 924.2, 924.2, 924.2, 922.2, 924.2, 924.7,
974.799995, 983.500006, 983.500006, 983.500004, 983.500006, 974.799995,
974.799994, 983.500006, 839.1, 839.1, 839.1, 839.100001, 839.300001,
839.1, 838.699999, 839.100001, 839.100001, 838.699999, 842.300004,
842.300004, 842.900006, 842.300004, 842.900006, 842.300004, 936.900014,
936.900014, 936.900014, 932.999984, 933.099983, 932.999984, 936.900014,
936.900014, 870.499995, 870.499995, 877.399998, 877.399998, 876.099997,
876.099997, 876.099997, 859.199997, 957.199982, 966.299982, 955.699998,
955.699998, 957.199982, 955.699998, 955.699998, 956.299985, 852.2,
852.2, 852.600006, 852.500001, 852.500001, 852.500001, 852.600006,
852.500001, 906.700011, 904.700001, 912.600007, 912.600007, 914.600007,
906.700001, 906.399998, 914.600007, 925.599982, 933.299992, 933.299992,
933.299992, 933.299992, 926.500012, 935.899994, 935.199992, 916.800001,
916.100001, 916.800001, 916.400003, 918.700003, 904.100001, 916.800001,
918.700003, 899.1, 904.100001, 906.000003, 903.400002, 904.100001,
903.400002, 906.000003, 906.000003, 905.7, 903.099999, 903.099999,
905.7, 912.199994, 893.200002, 905.399999, 904.999998, 933.700012,
933.700012, 933.700012, 933.700012, 933.700012, 932.30001, 932.300008,
932.300008, 878.500006, 878.500006, 878.500006, 879.300004, 879.300004,
879.300004, 879.300004, 873.200008), Div = c(2.2629743, 1.9630117,
1.7336569, 1.2816843, 2.4155056, 1.4240443, 1.5178948, 0.8993031,
1.2022801, 1.9287665, 2.0237769, 2.004871, 1.5020684, 2.1776591,
2.093787, 2.3139276, 2.7244402, 2.7026829, 1.6644725, 2.0696347,
1.9561853, 2.6018987, 2.5800017, 2.1867866, 2.4144821, 1.7389892,
2.1427451, 1.6544538, 1.8651966, 1.7569776, 1.8257533, 1.4048204,
2.7384914, 2.9344488, 2.2306909, 2.5085619, 1.8874836, 2.3431509,
1.8401602, 1.8620274, 1.8038997, 2.5909049, 2.2265328, 2.0882065,
2.4737837, 2.2995223, 1.4231311, 2.0577752, 1.6463134, 2.1464331,
2.2636437, 2.0992589, 1.7666974, 1.835061, 1.7732171, 2.0813243,
1.865505, 2.0200607, 1.2510612, 1.021761, 0.8111482, 0.2617645,
2.0282081, 1.1145976, 2.2596683, 2.3517629, 1.9424972, 1.9191269,
1.4222035, 2.6007698, 2.0071984, 1.9049132, 1.073374, 0.9576897,
1.6273043, 1.7701581, 0.6890092, 1.5764456, 0.384906, 1.5099996,
1.6713486, 2.5483064, 2.2033185, 2.0798843, 1.9082985, 2.1580972,
1.6952798, 1.6303402, 1.9461221, 1.4116405, 1.5347693, 2.6924921,
1.727278, 1.9384415, 1.6659585, 1.612819, 1.6592884, 2.7129796,
0, 2.7098898, 1.3785924, 2.7635218, 1.1481271, 1.8597007, 2.2191531,
1.088549, 2.431015, 1.3702099, 2.1018035, 2.3442348, 2.3599146,
2.789816, 1.8340235, 1.0606126, 2.5852679, 1.7791063, 1.2273106,
2.2432636, 2.5642458, 1.3306642, 2.6771856, 1.5062567, 2.0903266,
2.0398412, 2.4821503, 0.5979376, 1.479214, 1.9188301, 1.2267089,
2.4491421, 1.5366949, 2.516592, 2.4084849, 2.4385928, 2.549348,
2.7090074, 2.3337573, 1.8982968, 1.7956341, 2.3752386, 1.6587394,
2.6663039, 2.4853204, 1.9325793, 2.4431141, 1.6976331, 0.8791745,
2.6625573, 1.9596877, 1.9287565, 2.4590816, 2.4963942, 1.8767916,
1.3954333, 2.5155936, 2.2327274, 2.6613726, 2.580748, 2.3142567,
2.2280879, 1.7925025, 1.663008, 2.3488945, 2.0746398, 1.7050203,
2.0108246, 1.7317251, 2.4936515, 0.9556999, 1.3716151, 2.0694067,
1.4944032, 1.0984774, 1.2868726, 1.6429103, 1.3720737, 1.8037795,
1.8745583, 1.8921264, 1.8320377, 1.201682, 1.8489571, 1.798546,
0.8486856), triodia_low = c(19L, 6L, 21L, 32L, 11L, 32L, 16L,
29L, 17L, 20L, 0L, 24L, 37L, 0L, 3L, 29L, 4L, 2L, 31L, 28L, 20L,
12L, 6L, 6L, 26L, 28L, 27L, 32L, 37L, 26L, 15L, 27L, 2L, 1L,
19L, 5L, 13L, 10L, 33L, 14L, 25L, 22L, 15L, 34L, 15L, 7L, 36L,
25L, 25L, 0L, 25L, 4L, 21L, 0L, 33L, 16L, 16L, 15L, 22L, 25L,
25L, 0L, 0L, 18L, 2L, 0L, 26L, 0L, 0L, 7L, 0L, 13L, 28L, 35L,
0L, 0L, 0L, 31L, 29L, 0L, 14L, 5L, 14L, 11L, 12L, 16L, 21L, 26L,
22L, 7L, 23L, 10L, 23L, 17L, 19L, 7L, 27L, 3L, 0L, 2L, 29L, 14L,
30L, 12L, 0L, 35L, 0L, 29L, 4L, 5L, 14L, 15L, 33L, 0L, 3L, 21L,
34L, 0L, 2L, 28L, 16L, 0L, 1L, 0L, 11L, 0L, 32L, 0L, 27L, 2L,
28L, 3L, 0L, 4L, 1L, 6L, 14L, 27L, 25L, 12L, 7L, 10L, 16L, 9L,
4L, 15L, 40L, 2L, 18L, 5L, 3L, 6L, 1L, 33L, 2L, 5L, 12L, 4L,
7L, 3L, 17L, 30L, 5L, 7L, 17L, 15L, 16L, 9L, 0L, 26L, 16L, 0L,
24L, 1L, 27L, 32L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA,
-184L))
The model:
m1b <-gamm4(Div~TSF+FF+s(triodia_low, k=6)+VegtypeNew+ThreeYearRain,random=~(1|Lscape),data=data)
Plotting:
p <-plotGAMM (m1b,smooth.cov="triodia_low",groupCovs = NULL,orderedAsFactor=T,rawOrFitted="raw",plotCI=T,grouping = NULL)
p + labs(x= "Years since fire") +
labs(y="Species diversity (H')") +
theme_bw() +
theme(panel.grid.major = element_blank()) +
theme(panel.grid.minor = element_blank()) +
theme(panel.border = element_blank()) +
theme(axis.line = element_line(colour="black")) +
theme(axis.title = element_text(size=22)) + # face="bold"
theme(axis.ticks = element_line()) +
scale_x_continuous(breaks = seq(0,40,by=5)) +
geom_rect(aes(xmin=0,xmax=0.6,ymin=-Inf,ymax=Inf),alpha=0.002, fill="coral4") +
geom_rect(aes(xmin=.6,xmax=1,ymin=-Inf,ymax=Inf),alpha=0.002, fill="gold") +
geom_rect(aes(xmin=1,xmax=5,ymin=-Inf,ymax=Inf),alpha=0.002, fill="darkred") +
geom_rect(aes(xmin=5,xmax=10,ymin=-Inf,ymax=Inf),alpha=0.002,fill="chocolate") +
geom_rect(aes(xmin=10,xmax=40,ymin=-Inf,ymax=Inf),alpha=0.002,fill="orangered") +
theme(axis.text = element_text(size=18, colour="black")) +
theme(text = element_text(family = "Arial")) +
theme(legend.position= "none")
The plot:
Any help would be greatly appreciated :)
Using the development version of gratia you can replicate the plot you showed with a few simple calls to create the data, predict etc.
To install the development version of gratia do
# install.packages("remotes")
remotes::install_github("gavinsimpson/gratia")
Once installed you can produce an object suitable for plotting using:
library('mgcv')
library('gamm4')
library('gratia')
library('ggplot2')
library('dplyr')
## model fit
m1b <- gamm4(Div ~ TSF + FF + s(triodia_low, k=6) + VegtypeNew + ThreeYearRain,
random = ~ (1|Lscape), data = df)
## data to predict at
new_df <- data_slice(m1b, var1 = 'triodia_low', n = 100)
## predict and cast to a tibble
pred_df <- as_tibble(predict(m1b[["gam"]], new_df, se.fit = TRUE))
## add to the data we're predicting at
pred_df <- bind_cols(new_df, pred_df)
## grab the inverse link of the model (not needed here, but is for non-Normal fits)
ilink <- inv_link(m1b)
## create the upper and lower credible interval
pred_df <- mutate(pred_df,
lwr = ilink(fit - (2 * se.fit)),
upr = ilink(fit + (2 * se.fit)),
fit = ilink(fit))
The plot itself can be created using:
ggplot(pred_df, aes(x = triodia_low, y = fit)) +
labs(x = "Years since fire", y = "Species diversity (H')") +
theme_bw() +
theme(panel.grid.major = element_blank()) +
theme(panel.grid.minor = element_blank()) +
theme(panel.border = element_blank()) +
theme(axis.line = element_line(colour="black")) +
theme(axis.title = element_text(size=22)) +
theme(axis.ticks = element_line()) +
scale_x_continuous(breaks = seq(0,40,by=5)) +
geom_rect(aes(xmin=0, xmax=0.6, ymin=-Inf, ymax=Inf), alpha=0.01, fill="coral4") +
geom_rect(aes(xmin=0.6, xmax=1, ymin=-Inf, ymax=Inf), alpha=0.01, fill="gold") +
geom_rect(aes(xmin=1, xmax=5, ymin=-Inf, ymax=Inf), alpha=0.01, fill="darkred") +
geom_rect(aes(xmin=5, xmax=10, ymin=-Inf, ymax=Inf), alpha=0.01, fill="chocolate") +
geom_rect(aes(xmin=10, xmax=40, ymin=-Inf, ymax=Inf), alpha=0.01, fill="orangered") +
geom_point(data = df, mapping = aes(x = triodia_low, y = Div)) +
geom_ribbon(aes(ymin = lwr, ymax = upr), alpha = 0.4) +
geom_line()
Most of the ggplot code is your's, but now that we have full control, we can put the data layers in the foreground by leaving those layers until the end.
Note that I'm not convinced this is a great plot. The fitted function you are showing is conditional upon the other covariates in the data set. Here, and as with voxel::plotGAMM(), we're predicting from the model and hence we have to supply something for the other covariates. Following voxel::plotGAMM and mgcv::vis.gam, we fix the other covariates not shown at
the value of the data observation closest to the median (for continuous variables), or
the modal category (for factor parametric terms)
So, the resulting figure is the fit conditional upon those values. In particular it is for the spinsandplain level of VegTypeNew. As such it is a little misleading.

[r]: Interpreting results of a glmer, retransforming estimates

EDIT:
I am currently writing my master thesis on the effect of a certain insecticide on bumble bee colonies. I was for example checking if damaged/diseased appearing bees were more prevalent in colonies that were exposed to the insecticide compared to the control.
The study design is hierarchical. 16 fields were paired according to landscape characteristics. In each pair one field was randomly assigned to be treated with the insecticide, while the other is the control field. In each field there are 2 boxes and in each box are 2 bumble bee hives. From each hive I have up to ten pupae per sex.
This is how my data looks like:
structure(list(pair = 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,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
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L
), .Label = c("P01", "P02", "P03", "P04", "P05", "P10", "P11",
"P12"), class = "factor"), field = structure(c(6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 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, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 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, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L), .Label = c("VR02", "VR03",
"VR04", "VR05", "VR06", "VR07", "VR09", "VR12", "VR13", "VR14",
"VR16", "VR17", "VR18", "VR20", "VR21", "VR23"), class = "factor"),
treatment = 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, 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, 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, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L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, 4, 4, 4, 19, 19, 19, 19, 19, 19,
19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
26, 26, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14,
31, 31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 15, 15, 15, 15, 15, 16, 16, 16, 18, 18, 18,
18, 18, 18, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18,
5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
22, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
10, 10, 10, 10, 10, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 30, 30, 30, 30, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30), hive.nested = c(21L,
21L, 21L, 21L, 21L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L,
23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 45L, 45L,
45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 46L, 46L, 48L,
48L, 48L, 48L, 48L, 48L, 48L, 48L, 48L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 3L, 3L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 37L, 37L, 37L, 37L, 38L, 38L, 38L,
38L, 38L, 39L, 39L, 39L, 39L, 39L, 39L, 39L, 40L, 40L, 40L,
40L, 40L, 40L, 40L, 49L, 49L, 49L, 49L, 49L, 49L, 49L, 49L,
49L, 49L, 49L, 50L, 50L, 51L, 52L, 25L, 25L, 25L, 26L, 26L,
26L, 27L, 27L, 27L, 27L, 28L, 28L, 28L, 61L, 61L, 61L, 61L,
61L, 62L, 62L, 62L, 62L, 64L, 64L, 64L, 64L, 64L, 64L, 64L,
64L, 64L, 64L, 30L, 30L, 30L, 30L, 30L, 32L, 32L, 32L, 36L,
36L, 36L, 36L, 36L, 36L, 34L, 34L, 34L, 34L, 34L, 34L, 34L,
34L, 35L, 35L, 35L, 36L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 41L, 41L,
41L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 43L, 43L,
43L, 43L, 43L, 43L, 44L, 44L, 44L, 44L, 44L, 13L, 14L, 14L,
14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 16L, 16L, 16L, 16L, 16L, 16L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 19L, 20L, 20L, 20L, 20L, 17L, 17L, 17L, 17L,
17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L,
19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 53L, 53L, 53L, 53L,
54L, 54L, 54L, 54L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 56L,
56L, 56L, 60L, 60L, 60L, 60L, 57L, 57L, 57L, 57L, 57L, 57L,
57L, 58L, 58L, 58L, 58L, 58L, 58L, 59L, 59L, 59L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L, 10L, 14L, 15L, 16L, 21L, 23L, 24L, 26L, 28L, 29L, 30L,
31L, 32L, 33L, 34L, 37L, 38L, 39L, 40L, 42L, 45L, 47L, 48L, 49L,
50L, 51L, 52L, 53L, 54L, 55L, 58L, 60L, 66L, 67L, 68L, 72L, 73L,
74L, 77L, 83L, 85L, 87L, 90L, 92L, 95L, 97L, 100L, 104L, 108L,
115L, 117L, 123L, 125L, 133L, 134L, 137L, 144L, 155L, 156L, 157L,
158L, 159L, 160L, 161L, 162L, 163L, 164L, 166L, 169L, 170L, 172L,
175L, 178L, 179L, 180L, 184L, 185L, 189L, 190L, 191L, 192L, 193L,
194L, 195L, 196L, 197L, 199L, 201L, 202L, 203L, 205L, 206L, 207L,
211L, 212L, 213L, 215L, 217L, 221L, 222L, 224L, 226L, 230L, 244L,
247L, 255L, 258L, 262L, 271L, 272L, 274L, 280L, 281L, 284L, 285L,
288L, 289L, 295L, 296L, 297L, 299L, 300L, 305L, 308L, 309L, 312L,
314L, 326L, 327L, 328L, 329L, 330L, 331L, 332L, 333L, 334L, 335L,
356L, 359L, 362L, 364L, 366L, 375L, 378L, 381L, 388L, 389L, 390L,
391L, 392L, 393L, 404L, 405L, 406L, 407L, 408L, 409L, 410L, 412L,
417L, 418L, 420L, 424L, 425L, 426L, 427L, 428L, 429L, 430L, 431L,
432L, 433L, 435L, 436L, 438L, 439L, 440L, 441L, 442L, 443L, 444L,
446L, 447L, 450L, 453L, 454L, 455L, 456L, 458L, 459L, 461L, 462L,
465L, 466L, 468L, 475L, 476L, 477L, 478L, 479L, 480L, 481L, 482L,
483L, 484L, 485L, 486L, 487L, 490L, 491L, 494L, 495L, 496L, 500L,
501L, 508L, 518L, 519L, 521L, 522L, 524L, 525L, 526L, 527L, 528L,
529L, 530L, 531L, 532L, 533L, 534L, 535L, 538L, 540L, 542L, 543L,
544L, 548L, 549L, 551L, 552L, 553L, 554L, 555L, 556L, 557L, 559L,
560L, 563L, 568L, 569L, 571L, 572L, 576L, 577L, 578L, 579L, 580L,
581L, 582L, 583L, 584L, 585L, 587L, 588L, 590L, 594L, 595L, 596L,
600L, 603L, 604L, 605L, 606L, 607L, 608L, 609L, 616L, 618L, 620L,
622L, 626L, 628L, 631L, 632L, 635L, 636L, 638L, 639L, 641L, 646L,
647L, 651L, 652L, 653L, 654L, 655L, 656L, 658L, 659L, 660L, 661L,
663L, 666L, 667L, 668L, 669L, 670L, 673L, 675L, 676L, 678L, 679L,
680L, 681L, 682L, 684L, 685L, 686L, 687L, 688L, 689L, 690L))
I have run binomial glmer models from the lme4 package to test whether the presence of disease/damage signs in bumble bee colonies is affected by the insecticide.
damage.prev <- glmer(diseased ~ treatment + sex + stage
+ (1|pair/field/box.nested/hive.nested)
,data=df.cocoons.white,
family=binomial)
I have been trying to get estimates and confidence intervals. Thanks to #Benjamin
I got a little closer to the solution, but the estimates sill seem too high.
That's how I tried to get a data.frame of CIs and estimates:
fixed <- fixef(damage.prev)
wald <-confint(damage.prev,method="Wald")
estCloth.damage.ratio <- exp(fixed[1])
estCont.damage.ratio <- exp(fixed[1] + fixed[2])
lwrCloth.damage.ratio <- exp(wald[1,1])
lwrCont.damage.ratio <- exp(wald[1,1] + wald[2,1])
uprCloth.damage.ratio <- exp(wald[1,2])
uprCont.damage.ratio <- exp(wald[1,2] + wald[2,2])
estCloth.damage <- estCloth.damage.ratio/ (1+estCloth.damage.ratio)
estCont.damage <- estCont.damage.ratio / (1+ estCont.damage.ratio)
lwrCloth.damage <- lwrCloth.damage.ratio/ (1+ lwrCloth.damage.ratio)
lwrCont.damage <- lwrCont.damage.ratio /(1+ lwrCont.damage.ratio)
uprCloth.damage <- uprCloth.damage.ratio /(1+uprCloth.damage.ratio)
uprCont.damage <- uprCont.damage.ratio /(1+uprCont.damage.ratio )
treat.damage <- data.frame(Treatment,Estimate,lwr,upr)
What still confuses me are the high estimates of beyond 94%, yet
sum(df.cocoons.white$diseased)/length(df.cocoons.white$diseased)
gives me less than 70%. Doesn't seem realistic. Any idea what might be wrong?
Your model is using a logit transformation.
The way I look at generalized linear models is that they really aren't any different than simple linear regression. In simple linear regression, your response variable is continuous on (theoretically) the entire real number line (-Inf, Inf).
In logistic regression, your response is a proportion, which is continuous on the interval [0, 1]. The odds calculates (p / (1-p)) which is continuous over the interval of [0, inf). The log odds log(p / (1-p)) is continuous over the interval (-Inf, Inf).
This complete transformation (log(p / (1-p))) is referred to as the logit transformation and is pretty standard in logistic regression.
The results of your glmer model, which is a random effects version of logistic regression, uses the same transformation and so the estimated coefficients are on the scale of (-Inf, Inf). If you want odds ratios, you can exponentiate the coefficients, which will give you the odds measured on a scale of (0, Inf), with 1.0 being the null value.

R How to plot a "net interaction effect" (or marginal effect) when there are 'confounding' interactions

I want to draw the interaction effects between x1 and x2 for the following regression (let y be a positive count variable in a panel dataset)
library(lme4)
glmer.repex<-glmer.nb(y ~ x1 + x2 +I(x2^2) + x1:x2 +x1:I(x2^2) + (1|Year), data=repex, nAGQ = 1L)
The easy interaction plot is:
interaction.plot(x1,x2,y) ; interaction.plot(x1,x2,fitted(test.lmer,ndf, type="response"))
Now I want to acknowledge that if x1 changes the values of x1*x2 and x1^2 will also change which will change the fitted values. All of this should be taken into account when plotting the net interaction effect. It might e.g. be so that x1 and x2 both are positively correlated with y, but that their interaction attenuates this positive effect, potentially changing the direction of the prediction. This might additionally be conditional on the values of x1*x2...
So the simple interaction.plot does not really work anymore because the other variables are affecting the outcome as well.
Is there an easy way of doing this either with predict or effect ?
Here is a reproducible example data set (plm.data object) called repex
dput(repex)
structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L,
7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 10L, 10L), .Label = c("1", "2", "4", "6", "7",
"8", "9", "10", "11", "13", "15", "16", "17", "18", "20", "22",
"24", "26", "28", "29", "32", "34", "35", "36", "37", "39", "41",
"42", "44", "47"), class = "factor"), Year = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L,
6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("1991",
"1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999",
"2000", "2001", "2002", "2003", "2004", "2005", "2006"), class = "factor"), y = c(5, 10, 6, 9, 9, 4, 2, 2, 3, 7, 12, 13, 0, 5, 5, 1, 1, 3, 0, 0, 1, 0, 3, 0, 0, 4, 9, 9, 12, 9, 10, 6, 14, 12, 6, 2, 20, 15, 18, 14, 26, 17, 0, 0, 0, 0, 2, 0, 5, 1, 2, 2, 5, 3, 0, 0, 0, 1, 0, 0), x1 = c(0L, 0L, 3L, 3L, 3L, 5L, 0L, 0L, 0L, 0L, 0L, 3L, 0L, 2L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 2L, 1L, 0L, 0L, 2L, 0L, 2L, 8L, 4L, 1L, 0L, 4L, 2L, 1L, 1L, 1L, 1L, 0L, 1L, 3L, 5L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 2L, 6L, 0L, 0L, 1L, 1L, 1L, 0L), x2 = structure(c(4.22657266700715, 7.07828323739468, 5.58155937520987, 6.09945741088926, 4.98990473760187, 13.1975509132969, 0.136363636363636, 0.164922480620155, 0.981640399790555, 1.61119564479727, 4.27951983102512, 4.52440902710094, 2.41282572727806,
2.77748331046807, 7.00223921984389, 3.55837337174436, 5.31590575343992, 2.61930006177923, 0.211351052048726, 0.525647451963241, 0.696630753538187, 0.666082288178836, 1.09492110512526, 3.8035303566375, 0.548336215316966, 0.85237681730237, 0.982747572848003, 6.59758768791534, 8.74094242997363, 9.29913186611362, 3.5542234379174, 3.24728026722101, 3.60927964544638, 3.33459121950297, 2.88526780610146, 4.56612429882729, 26.45256036788,
21.0190838535023, 19.3769702276769, 19.7227148506334, 17.6596029433548, 23.6531530880185, 0.138888888888889, 0.249009205804338, 0.394159544159544, 0.316399286987522, 0.0413533834586466, 3.02315977564407, 3.94214293703149, 1.9143052531528, 3.16707069146414, 5.70642767128218, 4.38285025038957, 4.23425019314604, 0, 0, 0, 0, 0.0416666666666667, 0), .Dim = 60L, .Dimnames = list(c("1990_ABT", "1991_ABT", "1992_ABT", "1993_ABT", "1994_ABT", "1995_ABT", "1990_AKN", "1991_AKN", "1992_AKN", "1993_AKN", "1994_AKN", "1995_AKN",
"1990_ALL", "1991_ALL", "1992_ALL", "1993_ALL", "1994_ALL",
"1995_ALL", "1990_AMG", "1991_AMG", "1992_AMG", "1993_AMG",
"1994_AMG", "1995_AMG", "1990_AZN", "1991_AZN", "1992_AZN",
"1993_AZN", "1994_AZN", "1995_AZN", "1990_BAX", "1991_BAX",
"1992_BAX", "1993_BAX", "1994_BAX", "1995_BAX", "1990_BAY",
"1991_BAY", "1992_BAY", "1993_BAY", "1994_BAY", "1995_BAY",
"1990_BIO", "1991_BIO", "1992_BIO", "1993_BIO", "1994_BIO",
"1995_BIO", "1990_BMS", "1991_BMS", "1992_BMS", "1993_BMS",
"1994_BMS", "1995_BMS", "1990_ABT", "1990_ABT", "1990_ABT",
"1990_ABT", "1994_CHU", "1990_ABT")))), .Names = c("ID",
"Year", "y", "x1", "x2"), row.names = c(1L, 2L, 3L, 4L, 160L,
5L, 172L, 173L, 174L, 175L, 176L, 177L, 188L, 12L, 190L, 191L,
192L, 13L, 212L, 213L, 214L, 215L, 216L, 217L, 22L, 23L, 230L,
231L, 232L, 233L, 28L, 29L, 30L, 31L, 248L, 249L, 36L, 37L, 38L,
39L, 40L, 41L, 276L, 156L, 52L, 158L, 159L, 281L, 56L, 57L, 58L,
295L, 59L, 297L, 588L, 391L, 392L, 393L, 187L, 395L), class = c("plm.dim",
"data.frame"))
Thanks!

Error in migrating a ggplot graph to ver. 0.9.3

Trying to migrate a ggplot graph to ver 0.9.3, the commented out line in the code below produces a Discrete value supplied to continuous scale error (which was not a problem before).
Can you help me correct it?
If you need the data to experiment with, I provide it below.
ggplot(mdfr, aes(as.Date(value, "%d/%m/%Y"), name, colour = factor(stadio))) +
geom_line(aes(size=rating)) +
labs(colour="Baseline/Actual :", x = "", y = "") +
scale_colour_brewer(palette="BrBG",breaks = c("1", "3", "6","8"),
labels = c("Label 1", "Label 2", "Label 3","Label 4")) +
scale_size_manual(breaks = levels(mdfr$rating), values = as.integer(levels(mdfr$rating)), guide = "none") +
theme_bw() +
#geom_vline(data=dfrDataDate, aes(xintercept= as.Date(data.date, "%Y-%m-%d")),colour=rgb(215, 25, 28, max = 255),size=1) +
geom_text(data=dfrDataDate, aes(x= as.Date(data.date, "%Y-%m-%d"), label = format(as.Date(data.date),"%d/%m/%Y")), hjust = -0.05, vjust = 1.5, colour = "darkred", size = 3 ) +
geom_text(data=dfrLabels, aes(x= as.Date(diag_date, "%d/%m/%Y"), label = format(as.Date(diag_date, "%d/%m/%Y"),"%d/%m/%Y")), hjust = 0.5, vjust = -1, colour = "black", size = 3 ) +
geom_text(data=dfrYpogr, aes(x= as.Date(ypogr_date, "%d/%m/%Y"), label = format(as.Date(ypogr_date, "%d/%m/%Y"),"%d/%m/%Y")), hjust = 0.5, vjust = -1, colour = "black", size = 3 ) +
scale_y_discrete(breaks=names, labels=new.names) +
ggtitle('New plot title') +
theme(plot.title = element_text(size=16),
legend.position = "top",
legend.title = element_text(size=12),
legend.text = element_text(size=10),
legend.key = element_rect(colour='white'),
axis.text.x = element_text(angle=0,size = 10),
axis.title.x = element_text(size=14),
axis.text.y = element_text(size=10),
axis.title.y = element_text(angle=90,size=14),
axis.ticks.length = unit(.05, "cm")
)
DATA needed to produce the graph:
mdfr <- structure(list(name = structure(c(22L, 22L, 22L, 22L, 20L, 20L,
20L, 20L, 18L, 18L, 18L, 18L, 16L, 16L, 16L, 16L, 14L, 14L, 14L,
14L, 12L, 12L, 12L, 12L, 10L, 10L, 10L, 10L, 8L, 8L, 8L, 8L,
6L, 6L, 6L, 6L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 21L, 21L, 21L,
21L, 19L, 19L, 19L, 19L, 17L, 17L, 17L, 17L, 15L, 15L, 15L, 15L,
13L, 13L, 13L, 13L, 11L, 11L, 11L, 11L, 9L, 9L, 9L, 9L, 7L, 7L,
7L, 7L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 22L,
22L, 22L, 22L, 20L, 20L, 20L, 20L, 18L, 18L, 18L, 18L, 16L, 16L,
16L, 16L, 14L, 14L, 14L, 14L, 12L, 12L, 12L, 12L, 10L, 10L, 10L,
10L, 8L, 8L, 8L, 8L, 6L, 6L, 6L, 6L, 4L, 4L, 4L, 4L, 2L, 2L,
2L, 2L, 21L, 21L, 21L, 21L, 19L, 19L, 19L, 19L, 17L, 17L, 17L,
17L, 15L, 15L, 15L, 15L, 13L, 13L, 13L, 13L, 11L, 11L, 11L, 11L,
9L, 9L, 9L, 9L, 7L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 1L), .Label = c("733 A", "733 B", "725 A", "725 B",
"727 A", "727 B", "558 A", "558 B", "705 A", "705 B", "635 A",
"635 B", "737 A", "737 B", "719 A", "719 B", "700 A", "700 B",
"579 A", "579 B", "541 A", "541 B"), class = "factor"), stadio = c(2,
4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4,
5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 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L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("start_date",
"end_date"), class = "factor"), value = structure(c(3L, 18L,
20L, 36L, 2L, 14L, 24L, 38L, 7L, 7L, 7L, 31L, 9L, 15L, 27L, 34L,
4L, 19L, 21L, 37L, 1L, 9L, 23L, 33L, 8L, 13L, 25L, 32L, 10L,
16L, 28L, 29L, 5L, 12L, 26L, 35L, 6L, 17L, 22L, 39L, 11L, 17L,
22L, 30L, 3L, 18L, 20L, 36L, 2L, 14L, 24L, 31L, 50L, 50L, 50L,
56L, 15L, 52L, 55L, 32L, 48L, 49L, 55L, 34L, 1L, 53L, 53L, 57L,
49L, 51L, 54L, 58L, 10L, 16L, 28L, 29L, 5L, 12L, 26L, 35L, 6L,
17L, 22L, 39L, 11L, 17L, 22L, 30L, 18L, 20L, 36L, 45L, 14L, 24L,
38L, 46L, 7L, 7L, 31L, 42L, 15L, 27L, 34L, 44L, 19L, 21L, 37L,
44L, 9L, 23L, 33L, 40L, 13L, 25L, 32L, 44L, 16L, 28L, 29L, 41L,
12L, 26L, 35L, 43L, 17L, 22L, 39L, 47L, 17L, 22L, 30L, 47L, 18L,
20L, 36L, 59L, 14L, 24L, 31L, 64L, 50L, 50L, 56L, 66L, 52L, 55L,
32L, 61L, 49L, 55L, 34L, 63L, 53L, 53L, 57L, 65L, 51L, 54L, 58L,
60L, 16L, 28L, 29L, 63L, 12L, 26L, 35L, 62L, 17L, 22L, 39L, 67L,
17L, 22L, 30L, 61L), .Label = c("03/05/2012", "07/06/2011", "22/02/2011",
"22/06/2012", "23/12/2011", "28/12/2011", "29/02/2012", "29/03/2012",
"29/06/2012", "30/05/2011", "30/12/2011", "03/02/2012", "04/07/2012",
"08/02/2012", "10/07/2012", "17/10/2011", "23/02/2012", "24/05/2011",
"25/07/2012", "01/06/2011", "01/08/2012", "02/03/2012", "05/07/2012",
"09/02/2012", "12/07/2012", "16/02/2012", "19/07/2012", "27/01/2012",
"06/04/2012", "07/06/2012", "08/05/2012", "09/10/2012", "11/09/2012",
"16/10/2012", "18/05/2012", "20/09/2011", "23/10/2012", "24/04/2012",
"31/05/2012", "04/02/2013", "09/09/2012", "12/11/2012", "19/11/2012",
"28/02/2013", "28/09/2012", "29/10/2012", "30/11/2012", "01/07/2012",
"06/07/2012", "22/03/2012", "02/08/2012", "17/07/2012", "31/07/2012",
"06/09/2012", "26/07/2012", "12/06/2012", "13/11/2012", "20/11/2012",
"17/01/2013", "21/05/2013", "21/12/2012", "22/07/2012", "28/12/2012",
"30/03/2013", "30/04/2013", "31/01/2013", "31/12/2012"), class = "factor"),
rating = 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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("3", "5"), class = "factor")), row.names = c(NA,
-176L), .Names = c("name", "stadio", "variable", "value", "rating"
), class = "data.frame")
dfrDataDate <- structure(list(name = structure(1L, .Label = "733 A", class = "factor"),
data.date = structure(1L, .Label = "2013-01-02", class = "factor")), .Names = c("name",
"data.date"), row.names = c(NA, -1L), class = "data.frame")
dfrLabels <- structure(list(name = c("541 A", "579 A", "700 A", "719 A", "737 A",
"635 A", "705 A", "558 A", "727 A", "725 A", "733 A"), diag_date = c("20/09/2011",
"08/05/2012", "12/06/2012", "09/10/2012", "16/10/2012", "13/11/2012",
"20/11/2012", "06/04/2012", "18/05/2012", "31/05/2012", "07/06/2012"
)), .Names = c("name", "diag_date"), row.names = c(135L, 139L,
143L, 147L, 151L, 155L, 159L, 163L, 167L, 171L, 175L), class = "data.frame")
dfrYpogr <- structure(list(name = c("541 A", "579 A", "700 A", "719 A", "737 A",
"635 A", "705 A", "558 A", "727 A", "725 A", "733 A"), ypogr_date = c("17/01/2013",
"30/03/2013", "31/01/2013", "21/12/2012", "28/12/2012", "30/04/2013",
"21/05/2013", "28/12/2012", "22/07/2012", "31/12/2012", "21/12/2012"
)), .Names = c("name", "ypogr_date"), row.names = c(136L, 140L,
144L, 148L, 152L, 156L, 160L, 164L, 168L, 172L, 176L), class = "data.frame")
names <- as.character(unique(mdfr$name))
new.names <- c("No.541", "No.579", "No.700", "No.719", "No.737", "No.635",
"No.705", "No.558", "No.727", "No.725", "No.733", "", "",
"", "", "", "", "", "", "", "", "")
Adding of as.numeric() around as.Date() in call to geom_vline() makes red line to apear.
geom_vline(data=dfrDataDate, aes(xintercept= as.numeric(as.Date(data.date, "%Y-%m-%d"))),colour=rgb(215, 25, 28, max = 255),size=1)

Resources