Related
I'm trying to plot data from a study with three within-subjects (test item, frame, sample size) variables in ggplot. I have summarised and plotted test item on the x axis and have separate lines for sample size and have used facet_grid to separate the two frame conditions. The summarised this data to create within-subjects 95% CI error bars. I'd also like to underlay individual participant's lines. All the advice I have found so far doesn't explain how to plot individual and grouped data when you have facetted the data. Everything I have tried looks messy and doesn't clearly show individual's curves/lines.
Is there a way to do this?
I've considered splitting the data by the facetted conditions and plotting separately but if there is an easier way I would like to find it!
Here's a some of the data:
human_exp1 <- structure(list(sample_size = 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, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 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, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L
), .Label = c("1", "2", "3", "4", "5", "6"), class = "factor"),
id = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L), .Label = c("1",
"2", "3", "4", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
"24", "25", "26", "27", "28", "29", "30", "31", "32", "33",
"34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
"44", "45", "46", "47", "48", "49", "50", "85", "86", "87",
"88", "89", "90", "91", "92", "93", "94", "95", "96"), class = "factor"),
response = c(0.75, 0.25, 0.4, 0.5, 0.3, 0.55, 0.65, 0.4,
0.3, 0.5, 0, 0.15, 0.65, 0.65, 0.5, 0.65, 0.8, 0.65, 0.65,
0.75, 0.15, 0.35, 0.6, 0.15, 0.3, 0.5, 0.1, 0.3, 0.5, 0,
0.25, 0.45, 0.75, 0.7, 0.45, 0.65, 0.75, 0.75, 0.3, 0.1,
0.25, 0.15, 0.2, 0.3, 0.35, 0.05, 0.3, 0.5, 0, 0.15, 0.5,
0.1, 0.35, 0.25, 0.5, 0.5, 0, 0.25, 0, 0.3, 0.1, 0.15, 0.35,
0.2, 0, 0.3, 0.5, 0, 0.1, 0.5, 0, 0.3, 0.1, 0.7, 0.45, 0,
0.25, 0, 0.35, 0.1, 0.15, 0.3, 0.1, 0, 0.2, 0.25, 0, 0.1,
0.5, 0, 0.15, 0.3, 0.7, 0.4, 0, 0.05, 0.1, 0.3, 0.1, 0, 0.3,
0.05, 0, 0.25, 0.25, 0, 0.15, 0.5, 0, 0.1, 0, 0.75, 0.6,
0, 0.75, 0.3, 0.9, 0.3, 0.2, 0.95, 0.6, 0.7, 0.6, 0.5, 0,
0, 0.5, 0.9, 0.8, 0.9, 0.75, 0.7, 0.8, 0.5, 0.25, 0.1, 0.05,
0, 0.65, 0.5, 0.3, 0.8, 0.5, 0, 0, 0.5, 0.4, 0.85, 0.5, 0.55,
0.55, 0.35, 0.3, 0.2, 0.15, 0.05, 0, 0.3, 0.15, 0.05, 0.45,
0.5, 0, 0, 0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.25, 0.4, 0, 0.05, 0.2, 0.45,
0, 0, 0, 0, 0, 0.25, 0.5, 0, 0, 0.3, 0.35, 0.5, 0, 0, 0.35,
0, 0.05, 0.25, 0, 0, 0, 0, 0, 0, 0.15, 0.5, 0, 0, 0.15, 0,
0.5, 0, 0, 0.3, 0, 0.05, 0.25, 0, 0, 0, 0, 0, 0, 0.2, 0.5,
0, 0, 0.15, 0, 0.5, 0, 0, 0.35, 0)), row.names = c(NA, -684L
), class = c("tbl_df", "tbl", "data.frame"))
I used summarySEwithin to summarise the data:
within <- Rmisc::summarySEwithin(data = human_exp1, measurevar = "response",
withinvars = c("sample_size", "sampling_frame", "test_item"),
idvar = "id")
I used the summarised data to plot the group means in ggplot. Particularly so I could compute within-ss confidence intervals for the means.
pd <- position_dodge(0.1)
ggplot(within, aes(x=test_item, y=response, colour=factor(sample_size), group=factor(sample_size)))+
geom_point(position=pd, size=5)+
geom_line(position=pd, size = .8)+
facet_grid(cols = vars(sampling_frame))+
geom_errorbar(aes(ymin=response-ci, ymax=response+ci), width=1, position=pd, size=1)+
ylim(0, 1)+
theme_bw()+
scale_x_discrete(
breaks=c("1","2","3", "4", "5", "6"),
labels=c("S1", "S2", "T1", "T2", "T3", "T4")
)+
# theme(legend.position = c(.9, .85))+
labs(x = "Test Item", y = "Generalisation Response")
I then summarised the data and grouped by all the grouping variables including id
gd <- human %>%
group_by(id, test_item, sample_size, sampling_frame) %>%
summarise(response = mean(response))%>%
ungroup()
gd
I then tried many different versions of geom_line() with the gd summarised data to add individual lines.
Any help would be much appreciated. I would like the individual lines to appear as faint grey lines behind the group mean lines.
Here is what I have with the within-subjects grouped data
Here is what I get when I try to add individual lines with geom_line(data = human, aes(x=test_item, y=response, group=id))
Is this what you want? I grouped the individual lines by both id and sample_size to get single lines:
ggplot(within, aes(x=test_item, y=response, colour=factor(sample_size), group=factor(sample_size)))+
geom_point(position=pd, size=5)+
geom_line(position=pd, size = .8)+
facet_grid(cols = vars(sampling_frame))+
geom_errorbar(aes(ymin=response-ci, ymax=response+ci), width=1, position=pd, size=1)+
ylim(0, 1)+
theme_bw()+
scale_x_discrete(
breaks=c("1","2","3", "4", "5", "6"),
labels=c("S1", "S2", "T1", "T2", "T3", "T4")
)+
# theme(legend.position = c(.9, .85))+
labs(x = "Test Item", y = "Generalisation Response") +
geom_line(data=human_exp1, alpha=0.2, color="black", aes(x=test_item, Y=response, group=interaction(id,sample_size)))
Is this what you are lookong for?
library(dplyr)
library(ggplot2)
within %>%
ungroup() %>%
group_by(test_item, sample_size) %>%
summarise(mean = mean(response), ci = sd(response)) -> smry
pd <- "jitter"
ggplot(within, aes(x = test_item, y = response)) +
geom_point(aes(colour = sample_size), position = pd) +
geom_errorbar(
data = smry,
mapping = aes(y = mean, ymin = mean - ci, ymax = mean + ci),
size = 1
)+
facet_grid(cols = vars(sampling_frame)) +
ylim(0, 1) +
scale_x_discrete(
breaks = c("1","2","3", "4", "5", "6"),
labels = c("S1", "S2", "T1", "T2", "T3", "T4")
) +
labs(x = "Test Item", y = "Generalisation Response") +
theme_bw()
# theme(legend.position = c(.9, .85))+
consider the following example data:
ex = structure(list(group = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L,
2L, 3L, 4L, 6L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L, 1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 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, 1L, 2L, 3L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 1L, 1L, 1L,
4L), .Label = c("A", "B", "C", "D", "E", "F"), class = "factor"),
ID = structure(c(35L, 35L, 35L, 35L, 35L, 35L, 1L, 1L, 1L,
1L, 1L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 9L, 9L,
9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 21L, 21L, 22L, 22L,
22L, 22L, 2L, 3L, 4L, 5L, 8L, 15L, 16L, 17L, 18L, 19L, 19L,
20L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 30L, 31L, 32L,
33L, 34L), .Label = c("10", "107", "108", "109", "124", "17",
"18", "187", "19", "21", "24", "26", "27", "28", "335", "336",
"339", "340", "341", "342", "38", "39", "576", "577", "578",
"579", "580", "581", "582", "583", "584", "585", "586", "592",
"6"), class = "factor"), value = c(1L, 7L, 4L, 4L, 3L, 9L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 5L, 1L, 2L, 1L, 2L, 2L,
2L, 1L, 33L, 27L, 28L, 21L, 28L, 1L, 3L, 1L, 1L, 1L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 3L, 3L, 2L, 4L, 1L, 1L, 1L, 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)), class = "data.frame",
row.names = c(NA, -88L), .Names = c("group", "ID", "value")
)
Note that in group A, value = 1 for every ID. I use ggplot2 to create dot plot based on counts of the value variable using geom_dotplot and faceting by group:
ggplot(ex) + aes(x = value) +
geom_dotplot(binwidth = 1, method = "histodot") +
facet_wrap(~ group)
The dot stack in the first facet is cut off, even when exported using ggsave. Changing the y-axis limits has no effect, but changing the aspect ratio so that H >= W seems to fix the issue (usually by adding way more space to the top than necessary). Is this a bug, or am I specifying my dot plot incorrectly?
EDIT
One workaround is to flip my dotplot and bin by the y variable:
ggplot(ex) + aes(x = group, y = value) +
geom_dotplot(binwidth = 1, method = "histodot",
binaxis = "y", stackdir = "centerwhole") +
facet_wrap(~ group, scales = "free_x")
Two other parameters that can help you are stackratio and dotsize. For example:
ggplot(ex) + aes(x = value) +
geom_dotplot(binwidth = 1, method = "histodot", stackratio = 0.9, dotsize = .75) +
facet_wrap(~ group) +
scale_y_continuous(NULL, breaks = NULL)
You would need to tweak the numbers until you got the layout you wanted.
I found an interesting workaround using geom_bar that achieves the same structure as a dot plot but with rectangles:
ggplot(ex) + aes(x = value, group = ID) +
geom_bar(color = "black", fill = "white", width = 1) +
facet_wrap(~ group)
Although it results in rectangles (rather than dots) and you can't control the stack spacing. The rectangles get resized according to the plot window, which would be equivalent to tweaking the dot size in geom_dotplot. Also, it begs the question "why not just use a regular bar plot?"
I am hoping to create a facet grid that has two different set labels.
Below is the graph I have created:
I would like to change the y axis scales to go from 1 to 5 for the top grid and -3 to +3 for the bottom grid.
Here is my current code:
# make graph
ggplot(data = S2, mapping = aes(y = Rating, x = week, group= Question, colour= Question))+
geom_point()+
geom_line()+
scale_colour_manual(values=c(mycols))+
facet_grid(measureType ~., scales= "free")+
geom_smooth(method = 'loess', colour= "black", aes(group=1), se= FALSE)+
theme(legend.position="bottom",
axis.text = element_text(family= "sans", face= "bold", colour = "black"),
axis.title.x = element_text(family= "sans", face= "bold", colour = "black"),
axis.title.y = element_text(family= "sans", face= "bold", colour = "black"),
strip.text.x = element_text(family= "sans", face= "bold", colour = "black"),
strip.text.y = element_text(family= "sans", face= "bold", colour = "black"),
strip.background = element_rect(colour="#9C964A", fill="#FAD77B"),
panel.border = element_rect(color = "#9C964A", fill = NA, size = 1),
axis.line = element_line(colour = "#9C964A", size=1)) +
labs (x= "Date", y="Ratings by Question: 838607")
Thank you in advance!!
***** EDIT******
This is not a duplicate of the other question, because I don't want my scales to be "free". I want them to be explicitly set as 1 to 5 and +3 to -3. What I am having trouble with is how to make them different but set.
Please find my attached data.
> dput (S2)
structure(list(StudentFactor = 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), .Label = "838607", class = "factor"),
Question = structure(c(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, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 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, 9L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L), .Label = c("Q", "Q10", "Q11_1", "Q11_2", "Q11_3", "Q11_4",
"Q11_5", "Q12", "Q2", "Q8"), class = "factor"), Type = 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, 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, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 11L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("Jan11", "Jan25",
"Feb1", "Feb8", "Feb15", "Mar1", "Mar8", "Mar15", "Mar22",
"Mar29", "Apr5"), class = "factor"), Rating = c(5, 5, 5,
5, 5, 5, 5, 5, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, -1, 5, 5, 5,
5, 5, 4, 4, 5, 4, 3, 0, 0, 0, 0, 0, -1, 0, 1, -1, 5, 5, 5,
5, 5, 5, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, -1, 0, 0, 5, 5, 5,
5, 5, 4, 4, 4, 3, 4, 0, 0, 0, 0, 0, -1, 0, 0, -1, 5, 5, 5,
5, 5, 4, 4, 4, 4, 3, 0, 0, 0, 0, 0, -1, 0, 0, 0)), class = "data.frame", row.names = c(NA,
-95L), .Names = c("StudentFactor", "Question", "Type", "measureType",
"week", "Rating"))
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,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L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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, 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, 1L, 1L, 1L, 1L, 1L, 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,
1L, 1L, 1L, 1L, 1L, 1L, 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, 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), .Label = c("Clothianidin", "Control"), class = "factor"),
box.nested = c(11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24,
24, 24, 24, 24, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3,
3, 3, 4, 4, 4, 4, 4, 4, 4, 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, 59L, 59L,
59L, 59L, 59L, 60L, 60L, 60L, 60L, 60L, 60L), stage = structure(c(2L,
1L, 1L, 3L, 1L, 1L, 1L, 1L, 2L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, 0, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0,
1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0,
0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0)), .Names = c("pair",
"field", "treatment", "box.nested", "hive.nested", "stage", "condition",
"sex", "diseased"), class = "data.frame", row.names = c(5L, 7L,
8L, 9L, 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.
I am currently trying to make a 'heat map' using ggplot2 to display a series of p-values, but can't figure out how to tailor the actual color assignments and legend.
sampledata.m <- melt(sampledata)
sampledata.m$var2 <- as.character(sampledata.m$var2)
sampledata.m$var2 <- factor(sampledata.m$var2, levels=unique(sampledata.m$var2),ordered=TRUE)
sampledata.m$var1 <- as.character(sampledata.m$var1)
sampledata.m$var1 <- factor(sampledata.m$var1, levels=unique(sampledata.m$var1),ordered=TRUE)
This was done so that I could maintain the order of my variables.
p <- ggplot(sampledata.m, aes(var2, var1)) +
geom_tile(aes(fill = value), colour = "transparent") +
scale_fill_gradientn(colours=c("light green","dark green", "black"),
values=rescale(c(0,0.0003,0.05,0.5,1)),limits=c(0,1)))
p + theme_bw(base_size = base_size) + labs(x = "", y = "") +
scale_x_discrete(expand = c(0,0)) +
theme(legend.position = "bottom", axis.ticks = element_blank(),
axis.text.x = element_text(size = base_size * 0.8, angle = 310,
hjust = 0, colour = "black"))
This creates a nice looking plot, however my legend and my color gradient don't represent the rescale that I assigned. Forgive my ignorance if this is a simple fix, but I've only been coding R for about 2 weeks now. Ideally, I would love my plot and legend to mimic the color scheme and legend labeling similar to this paper: http://www.ncbi.nlm.nih.gov/pubmed/22496159
structure(list(var1 = 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, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L
), .Label = c("A", "B", "C",
"D", "E"), class = "factor"), var2 = structure(c(1L,
5L, 23L, 18L, 9L, 8L, 14L, 12L, 20L, 6L, 21L, 11L, 2L, 22L, 10L,
3L, 19L, 16L, 4L, 7L, 15L, 17L, 13L, 24L, 1L, 5L, 23L, 18L, 9L,
8L, 14L, 12L, 20L, 6L, 21L, 11L, 2L, 22L, 10L, 3L, 19L, 16L,
4L, 7L, 15L, 17L, 13L, 24L, 1L, 5L, 23L, 18L, 9L, 8L, 14L, 12L,
20L, 6L, 21L, 11L, 2L, 22L, 10L, 3L, 19L, 16L, 4L, 7L, 15L, 17L,
13L, 24L, 1L, 5L, 23L, 18L, 9L, 8L, 14L, 12L, 20L, 6L, 21L, 11L,
2L, 22L, 10L, 3L, 19L, 16L, 4L, 7L, 15L, 17L, 13L, 24L, 1L, 5L,
23L, 18L, 9L, 8L, 14L, 12L, 20L, 6L, 21L, 11L, 2L, 22L, 10L,
3L, 19L, 16L, 4L, 7L, 15L, 17L, 13L), .Label = c("1", "2",
"3", "4", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21",
"22", "23", "24"), class = "factor"), variable = 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), .Label = "pvalue", class = "factor"),
value = c(0.810172671, 0.596026338, 0.076550169, 0.908670635,
0.300418653, 0.051553286, 0.124196482, 0.601568833, 0.058431468,
0.341726981, 0.876674726, 0.002698295, 0.812059425, 0.068199656,
0.758383287, 0.60362134, 0.89265723, 0.246111936, 0.156348035,
0.909574522, 0.020202377, 0.388843992, 0.769441835, 0.102272916,
0.38895717, 0.882296525, 0.792438683, 0.000491393, 0.004233434,
0.202424095, 0.426941568, 0.08520186, 0.763036306, 0.602828564,
0.037278697, 0.121642743, 0.669123606, 0.974328438, 0.834329923,
0.050413697, 0.078476666, 0.387647156, 0.000540422, 0.379576632,
0.361428444, 0.502439758, 0.001326035, 0.027652693, 0.188885638,
0.579244445, 0.471985778, 0.677458228, 0.119307242, 0.364857868,
0.238260538, 0.53472206, 0.204344281, 0.291888993, 0.295809688,
0.00029, 0.005476157, 0.960975822, 0.00029, 0.055915429,
0.618284682, 0.040605253, 0.521649682, 0.421086546, 0.164333061,
0.755528982, 0.306854182, 0.012832628, 0.270393143, 0.946675764,
0.59227376, 0.112658388, 0.429091426, 0.01662083, 0.017342483,
0.065817234, 0.012140224, 0.359828816, 0.031969725, 0.00029,
0.14555102, 0.18865081, 0.00029, 0.064107531, 0.505257768,
0.070224536, 0.017082975, 0.375864198, 0.00029, 0.104103689,
0.898979883, 0.004879605, 0.003597954, 0.036722932, 0.849058218,
0.00029, 0.003739938, 0.00029, 0.00029, 0.00029, 0.008179017,
0.193870353, 0.460181712, 0.389475522, 0.00029, 0.8785017,
0.070414642, 0.584977921, 0.990764677, 0.767253318, 0.002234906,
0.051331823, 0.00446149, 0.234477639, 0.275139791)), .Names = c("var1", "var2", "variable", "value"), row.names = c(NA, -119L), class = "data.frame")
I'm not going to get into all of the theme settings you've got - as I understand it the key of your problem is the scale of the fill gradient. You can set this in scale_fill_gradient() with a log transformation:
p <- ggplot(sampledata.m, aes(var2, var1)) +
geom_tile(aes(fill = value), colour = "transparent") +
scale_fill_gradient(trans = "log", low = "light green", high = "black",
breaks = c(0, 0.001, 0.05, 0.5))
dt <- data.frame(
N=letters[5:11],
a=c(0.01,0.05,0.1,0.5,1,5,10),
b=c(10,20,50,100,200,1000,2000))
dt.mlt <- melt(dt,variable.name="Cls",value.name="Val")
ggplot(dt.mlt,aes(x=N,y=Cls,fill=Val))+
geom_tile()+
scale_fill_gradient2(
low="green",high="red",mid="black",trans="log",breaks=c(0,0.01,0.1,1,10,100,1000))+
geom_text(data=dt.mlt,aes(x=N,y=Cls,label=Val))
But if I add the midpoint=10 to the scale_fill_gradient2, the picture will become: