How can i plot a Heatmap in R - r

Im trying to plot a heatmap in R, but when I run my code it gives me this error:
Error: Can't combine `No.` <integer> and `Mes` <character>.
What I'm doing wrong? Here is my code:
df %>%
pivot_longer(-Localidad) %>%
ggplot(aes(x = name, y = Localidad , fill = value)) +
geom_tile(colour="gray80", size=0.2) +
geom_text(aes(label=value)) +
theme_minimal() +
scale_fill_distiller(palette = "YlGnBu", direction = -1, na.value = "white")
My df its something like this, Im working with a lot of data, so thats why I didnt want to print all the head of, but here it is.
> dput(head(df))
structure(list(No. = 1:6, Mes = c("oct-10", "oct-10", "oct-10",
"oct-10", "oct-10", "oct-10"), Delegacion = c("09CIUDAD DE MÉXICO",
"09CIUDAD DE MÉXICO", "09CIUDAD DE MÉXICO", "09CIUDAD DE MÉXICO",
"09CIUDAD DE MÉXICO", "09CIUDAD DE MÉXICO"), Localidad = c("09016MIGUEL HIDALGO",
"09005GUSTAVO A. MADERO", "09005GUSTAVO A. MADERO", "09003COYOACÁN",
"09010ÁLVARO OBREGÓN", "09011TLÁHUAC"), Esquema = c("U", "U",
"U", "U", "U", "U"), Número = c(629L, 1402L, 699L, 48L, 539L,
55L), Nombre = c("MUNDO DE LOS PEQUES", "GUARDERIA EL ARBOL DE LA NIÑEZ",
"LOS PEQUEÑOS GENIOS II", "MI MUNDO FELIZ", "CENTRO ECOLÓGICO DE DESARROLLO INFANTIL II",
"ESTANCIA INFANTIL TERCER MILENIO"), X2.1 = c(1L, 1L, 1L, 1L,
1L, 1L), X2.2 = c(1L, 1L, 1L, 1L, 1L, 1L), X2.3 = c(1L, 1L, 1L,
1L, 1L, 1L), X2.4 = c(1L, 1L, 1L, 1L, 1L, 1L), X2.5 = c(1L, 1L,
1L, 1L, 1L, 1L), X2.6 = c(1L, 1L, 0L, 1L, 1L, 0L), X2.7 = c(1L,
1L, 1L, 1L, 1L, 1L), X2.8 = c(1L, 1L, 1L, 1L, 1L, 1L), X2.9 = c(1L,
1L, 1L, 1L, 1L, 1L), X2.1.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X2.11 = c(1L,
1L, 1L, 1L, 1L, 1L), X2.12 = c(1L, 1L, 1L, 1L, 1L, 1L), X3.1 = c(1L,
1L, 1L, 1L, 1L, 1L), X3.2 = c(1L, 1L, 1L, 1L, 1L, 1L), X5.1 = c(1L,
1L, 1L, 1L, 1L, 1L), X5.2 = c(1L, 1L, 1L, 1L, 1L, 1L), X5.3 = c(1L,
1L, 1L, 1L, 1L, 1L), X5.4 = c(1L, 1L, 1L, 1L, 1L, 1L), X5.5 = c(1L,
1L, 1L, 1L, 1L, 1L), X5.6 = c(1L, 1L, 1L, 1L, 1L, 1L), X5.7 = c(1L,
1L, 1L, 1L, 1L, 1L), X5.8 = c(1L, 1L, 1L, 1L, 1L, 1L), X6.1 = c(1L,
1L, 1L, 1L, 1L, 1L), X6.2 = c(1L, 1L, 1L, 1L, 1L, 1L), X6.3 = c(1L,
1L, 1L, 1L, 1L, 1L), X6.4 = c(1L, 1L, 1L, 1L, 1L, 1L), X6.5 = c(1L,
1L, 1L, 1L, 1L, 1L), X7.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X7.2 = c(1L,
1L, 1L, 1L, 1L, 1L), X7.3 = c(1L, 1L, 1L, 1L, 1L, 1L), X7.4 = c(1L,
1L, 1L, 1L, 1L, 1L), X8.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X8.2 = c(1L,
1L, 1L, 1L, 1L, 1L), X9.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X9.2 = c(1L,
1L, 1L, 1L, 1L, 1L), X9.3 = c(1L, 1L, 1L, 1L, 1L, 1L), X9.4 = c(1L,
1L, 1L, 1L, 1L, 1L), X10.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X10.2 = c(1L,
1L, 1L, 1L, 1L, 1L), X10.3 = c(1L, 1L, 1L, 1L, 1L, 1L), X10.4 = c(1L,
1L, 1L, 1L, 1L, 1L), X10.5 = c(1L, 1L, 1L, 1L, 1L, 1L), X10.6 = c(1L,
1L, 1L, 1L, 1L, 1L), X10.7 = c(1L, 1L, 1L, 1L, 1L, 1L), X10.8 = c(1L,
1L, 1L, 1L, 1L, 1L), X10.9 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.1 = c(1L,
1L, 1L, 1L, 1L, 1L), X11.2 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.3 = c(1L,
1L, 1L, 1L, 1L, 1L), X11.4 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.5 = c(1L,
1L, 1L, 1L, 1L, 1L), X11.6 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.7 = c(1L,
1L, 1L, 1L, 1L, 1L), X11.8 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.9 = c(1L,
1L, 1L, 1L, 1L, 1L), X11.1.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.11 = c(1L,
1L, 1L, 1L, 1L, 1L), X11.12 = c(1L, 1L, 1L, 1L, 1L, 1L), X11.13 = c(1L,
1L, 1L, 1L, 1L, 1L), X11.14 = c(1L, 1L, 1L, 1L, 0L, 0L), X11.15 = c(1L,
1L, 1L, 1L, 1L, 0L), X11.16 = c(1L, 1L, 1L, 1L, 1L, 1L), X12.1 = c(1L,
1L, 1L, 1L, 1L, 1L), X12.2 = c(1L, 1L, 1L, 1L, 1L, 1L), X12.3 = c(1L,
1L, 1L, 0L, 1L, 1L), X12.4 = c(1L, 1L, 1L, 1L, 1L, 1L), X12.5 = c(1L,
1L, 1L, 1L, 1L, 1L), X12.6 = c(1L, 1L, 1L, 1L, 1L, 1L), X12.7 = c("SI",
"SI", "SI", "SI", "NO", "NO"), X12.8 = c(0L, 0L, 0L, 0L, NA,
NA), X14.1 = c(1L, 1L, 1L, 1L, 1L, 1L), X14.2 = c(1L, 1L, 1L,
1L, 0L, 1L), Puntos.máximos = c(71L, 71L, 71L, 71L, 70L, 70L),
Puntos.alcanzados = c(70L, 70L, 69L, 69L, 68L, 67L), X. = c(98.59,
98.59, 97.18, 97.18, 97.14, 95.71), No..de.Padres = c(7L,
7L, 6L, 7L, 7L, 7L), Horas = c(14L, 14L, 12L, 14L, 14L, 14L
)), row.names = c(NA, 6L), class = "data.frame")

You can try to exclude the character columns from pivoting. Not entirely sure if the result will be what you expected though.
library(ggplot2)
library(tidyr)
dff <- pivot_longer(df, colnames(df)[!sapply( df, is.character )] )
ggplot(dff, aes(x = name, y = Localidad , fill = value)) +
geom_tile(colour="gray80", size=0.2) +
geom_text(aes(label=value)) +
theme_minimal() +
scale_fill_distiller(palette = "YlGnBu", direction = -1, na.value = "white")
# plot

Related

how to add a new column based on certain conditions with tidyverse?

I am trying to create a new column based on whether a respondent is healthy or not.
Here it the type fo data I have:
test <- structure(list(`cutree(hc_diana, k = 4)` = c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), id = c("117dbbbf15", "117dbbbf15", "117dbbbf15", "117dbbbf15",
"117dbbbf15", "117dbbbf15", "117dbbbf15", "117dbbbf15", "117dbbbf15",
"3c8bfb6fc3", "3c8bfb6fc3", "3c8bfb6fc3", "3c8bfb6fc3", "3c8bfb6fc3",
"3c8bfb6fc3", "3c8bfb6fc3", "3c8bfb6fc3", "3c8bfb6fc3", "8a594e9340",
"8a594e9340"), covid_tested = c("positive", "positive", "positive",
"positive", "positive", "positive", "positive", "positive", "positive",
"positive", "positive", "positive", "positive", "positive", "positive",
"positive", "positive", "positive", "positive", "positive"),
age = c(51, 51, 51, 51, 51, 51, 51, 51, 51, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28), gender = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("Female", "Male", "Other"), class = "factor"),
number_morbidities = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1), chills = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("No", "Yes"), class = "factor"), cough = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"),
diarrhoea = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("No",
"Yes"), class = "factor"), fatigue = structure(c(2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"),
headache = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("No",
"Yes"), class = "factor"), loss_smell_taste = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L), .Label = c("No", "Yes"), class = "factor"),
muscle_ache = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("No",
"Yes"), class = "factor"), nasal_congestion = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"),
nausea_vomiting = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("No",
"Yes"), class = "factor"), shortness_breath = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"),
sore_throat = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("No",
"Yes"), class = "factor"), sputum = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("No", "Yes"), class = "factor"), temperature = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"),
loss_appetite = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("No",
"Yes"), class = "factor"), chest_pain = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"),
itchy_eyes = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("No",
"Yes"), class = "factor"), joint_pain = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"),
comorbidities = c("asthma", "diabetes_type_one", "diabetes_type_two",
"obesity", "hypertension", "heart_disease", "lung_condition",
"liver_disease", "kidney_disease", "asthma", "diabetes_type_one",
"diabetes_type_two", "obesity", "hypertension", "heart_disease",
"lung_condition", "liver_disease", "kidney_disease", "asthma",
"diabetes_type_one"), bolean_yes_no = c("No", "No", "No",
"Yes", "No", "No", "No", "No", "No", "No", "No", "No", "No",
"No", "No", "No", "No", "No", "No", "No")), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
I have 15 rows with 3 unique id's in
Yet, I want to get new column based on several conditions:
if they have a comorbidity -> then select only the row with that comorbidity in question and add it into the new column with its name, yet all the other should have NA
as you can see the second id , does not have whatsoever any comorbidity, therefore I want a new category for it and treat it as a "healthy" category and the rest of the rows pertaining to this patient to appear as NA. This is the same for the third responder.
How do I do this with tidyverse?
A sample of how I want the new column to look like is here, check the last column that summarises the above points.
structure(list(id = c("117dbbbf15", "117dbbbf15", "117dbbbf15",
"117dbbbf15", "117dbbbf15", "117dbbbf15", "117dbbbf15", "117dbbbf15",
"117dbbbf15", "3c8bfb6fc3", "3c8bfb6fc3", "3c8bfb6fc3", "3c8bfb6fc3",
"3c8bfb6fc3", "3c8bfb6fc3", "3c8bfb6fc3", "3c8bfb6fc3", "3c8bfb6fc3",
"8a594e9340", "8a594e9340"), number_morbidities = c(1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1), chills = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), cough = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), diarrhoea = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), fatigue = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), headache = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), loss_smell_taste = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L), .Label = c("No", "Yes"), class = "factor"), muscle_ache = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), nasal_congestion = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), nausea_vomiting = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), shortness_breath = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), sore_throat = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), sputum = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), temperature = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), loss_appetite = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), chest_pain = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), itchy_eyes = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), joint_pain = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("No", "Yes"), class = "factor"), comorbidities = c("asthma",
"diabetes_type_one", "diabetes_type_two", "obesity", "hypertension",
"heart_disease", "lung_condition", "liver_disease", "kidney_disease",
"asthma", "diabetes_type_one", "diabetes_type_two", "obesity",
"hypertension", "heart_disease", "lung_condition", "liver_disease",
"kidney_disease", "asthma", "diabetes_type_one"), bolean_yes_no = c("No",
"No", "No", "Yes", "No", "No", "No", "No", "No", "No", "No",
"No", "No", "No", "No", "No", "No", "No", "No", "No"), morbiditiy_healthy = c(NA,
NA, NA, "obesity", NA, NA, NA, NA, NA, "healthy", NA, NA, NA,
NA, NA, NA, NA, NA, "healthy", NA)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
We group by 'id', create the 'morbidity_healthy' with case_when where we check for 'Yes' in 'bolean_yes_no' column, if it is TRUE, then get the corresponding 'comorbidities', and if there are not (!) any 'Yes' and the row_number is 1, then return the 'healthy' for that row
library(dplyr)
test %>%
group_by(id) %>%
mutate(morbidity_healthy = case_when(bolean_yes_no == 'Yes' ~ comorbidities,
(!any(bolean_yes_no == 'Yes')) & row_number()==1 ~ 'healthy'))

Add text outside plot area in ggplot2

I am trying to create the following plot and I want to add some additional illustration to the plot to point the reader to some important characteristics of it.
I am trying to add the 4 text descriptions (hopefully with the line blocks) to the graph.
Is this possible with ggplot or should I look to some other package to do this?
The length of the lines are related to the dispersion of the points, I do not mind manually adding these lines with a fixed width for each but it would be also cool to see if there is a way to make the line lengths dependent on the dispersion (but again really not important!).
I am plotting predicted probabilities from one model vs another model.
I have a data frame called x
ggplot code:
ggplot(x, aes(x=mod1, y=mod2, colour = actual)) +
geom_point(alpha = 1) +
geom_density_2d() +
stat_density_2d(aes(fill = ..level..), geom="polygon", alpha = 1) +
labs(x = "plot1 results", subtitle = "--------------- subtitle which can be quite long like this -------------------", y = "plot2 results", title = "title") +
theme_bw(base_size = 11, base_family = "") +
theme(aspect.ratio = 1) +
scale_color_manual(values=c("yellow", "grey"))
Data:
structure(list(mod1 = c(0.0428680343284435, 0.846016555762155,
0.326787531886571, 0.553755029639909, 0.687879627696911, 0.0960930400203601,
0.744828994271728, 0.540002328947346, 0.220881375043177, 0.0702872626691926,
0.326427351123072, 0.242150667845905, 0.0585994813808256, 0.0476546237354429,
0.677024473452915, 0.141965306592508, 0.580238736830929, 0.209089243871524,
0.202588588164632, 0.462602376730863, 0.170047796107216, 0.0599183763024999,
0.144128948353236, 0.704983877871062, 0.148981617804389, 0.0259100317297817,
0.070919748619838, 0.847120835339521, 0.280625159241402, 0.241346727659237,
0.285528700584795, 0.522832128634093, 0.0495932506050149, 0.191222810970403,
0.848539280298263, 0.460823513872965, 0.297519579850422, 0.299706327222228,
0.29118588708967, 0.569263883004122, 0.814633900535549, 0.268597010973285,
0.583651002250045, 0.0771418703083737, 0.272577428138581, 0.0652980769011686,
0.0645563141023351, 0.185751367095499, 0.163771389063719, 0.922377554059713,
0.118440921292355, 0.601015657502687, 0.458036991708823, 0.706976353965206,
0.557104373519309, 0.336600458082119, 0.365573066188997, 0.349695386601579,
0.885005310870269, 0.340463030723538, 0.646538075407289, 0.347697108751173,
0.23887463827597, 0.222397529338268, 0.261741960415693, 0.255160177543014,
0.394003003413919, 0.350059479442216, 0.443055385407801, 0.999532291288415,
0.025735898423369, 0.369031728988488, 0.105252031223466, 0.233622390662318,
0.258892195873903, 0.101629573908821, 0.260570520936073, 0.209063089308277,
0.265239879267213, 0.137555975136797, 0.0855678812173928, 0.73880289082864,
0.802094313494666, 0.973899882546715, 1, 0.943515501797875, 0.020488912431986,
0.138004937708603, 0.111975864093794, 0.818441921329778, 0.0392858886896277,
0.593233184537478, 0.186525732878499, 0.467999992773845, 0.653350287632996,
0.550997098851144, 0.525885581162108, 0.98809473982989, 0.304496141867713,
0.233695105089987, 0.168798462655651, 0.276530329800917, 0.0258753799208103,
0.398677230034304, 0.601193870473126, 0.33018455671484, 0.546663783633736,
0.0732052848477258, 0.299579613100531, 0.195704039249802, 0.41032712235619,
0.358664485435842, 0.102182019177211, 0.111387254699232, 0.558663221258304,
0.701343535849789, 0.565610725546385, 0.165796298650061, 0.844108924341358,
0.65239500174214, 0.795485589918214, 0.575301458796068, 0.179957090666757,
0.883763860288409, 0.491162742628921, 0.615729900118775, 0.561630408279962,
0.284792242641951, 0.969432341514501, 0.477295676601633, 0.375561806375457,
0.206089561651849, 0.465548611290696, 0.336325053961017, 0.373667379797929,
0.570137795506596, 0.491918452884049, 0.190682849018188, 0.225091942414155,
0.410820863660711, 0.227786666632663, 0.138933996506405, 0.0334324000181915,
0.162488265240681, 0.805662813620657, 0.0933958810234599, 0.153939073268865,
0.857625661330725, 0.0963304147676984, 0.380415923377483, 0.0519017480874503,
0.0779283157397147, 0.107444894718232, 0.10441308985682, 0.712516878369368,
0.284157191775836, 0.265396916220893, 0.0400873874170811, 0.0393202404365727,
0.477044935156301, 0.124630185722413, 0.0650517287131615, 0.124980600269956,
0.320411167606173, 0.248807440955571, 0.082630461317074, 0.756327325304615,
0.225984532731912, 0.403768216845618, 0.630147108079651, 0.15594857762537,
0.0839887294293966, 0.150716106034452, 0.121445695528259, 0.35994354107865,
0.368300361452314, 0.24483459459127, 0.152527988563374, 0.551278085315308,
0.150034610924398, 0.250987845804079, 0.0565821061235104, 0.712734202323831,
0.335055664246889, 0.756065329175747, 0.29393094296416, 0.106641584154548,
0.420158769864025, 0.356454970300771, 0.47515374360064, 0.580812111995106,
0.000717130307059303, 0.154790410888561, 0.219513327079438, 0.364778107570437,
0.412859716955486, 0.692560953694384, 0.32077704674508, 0.0877703437440174,
0.275616887306484, 0.163821283189476, 0.808953944759211, 0.160209449757217,
0.394250485937628, 0.790498740283875, 0.447255960695868, 0.755359668103058,
0.537682737841053, 0.593538678937104, 0.196985122041494, 0.276494311008183,
0.0872459557748893, 0.235580847722567, 0.694748763162609, 0.354039737000946,
0.44200303784393, 0.165473335918548, 0.467358717157778, 0.0892700946170289,
0.99962830878852, 0.14014494671621, 0.0690684955560723, 0.145798038742771,
0.250339990117619, 0.341301599717557, 0.999244229176301, 0.478057677073386,
0.499445867397323, 0.676004506849537, 0.719051923777321, 0.105192369641408,
0.299010580450917, 0.684795758632428, 0.291732308319941, 0.0270760029206405,
0.231610195736126, 0.611894118472659, 0.252430564645428, 0.131649581553132,
0.510978021474668, 0.462763808757614, 0.237101727522011, 0.119024947839695,
0.135741592765658, 0.494417857244557, 0.173666298996107, 0.318988535201983,
0.173381070170447, 0.142219387103895, 0.656232876487217, 0.205846250120757,
0.122824951848628, 0.162495489321409, 0.872492951796276, 0.909080602176691,
0.66011964524832, 0.124102526142048, 0.138596610491767, 0.402363981778695,
0.443105000056591, 0.351367846003605, 0.32105628268368, 0.218800962448852,
0.118826349897186, 0.276304590212166, 0.0610016455322748, 0.558898605504491,
0.896861618042103, 0.219229588571431, 0.452525152002443, 0.200601477807967,
0.497083783289865, 0.110887650356807, 0.423945207587228, 0.0788144014885928,
0.389111968949349, 0.234569095311629, 0.574908316733503, 0.0650452798024705,
0.420115866748883, 0.457514278320592, 0.880126441902655, 0.0521590907403362,
0.961235825781951, 0.270440842694789, 0.114619887870278, 0.34148441206563,
0.334334164627438, 0.272603252040328, 0.0726735743345817, 0.0492160623167159,
0.718306549552725, 0.186814116478276, 0.358501035429906, 0.657342239918421,
0.336900266574, 0.704012501445306, 0.171667389605084, 0.0887593837030433,
0.194855062569366, 0.992724683416655, 0.217781466318124, 0.244417900635284,
0.772342328020165, 0.0787202755309474, 0.360467546646677, 0.208455253587378,
0.412048159709031, 0.237356742436605, 0.0975803958838624, 0.211064024176639,
0.311204223705994, 0.0583237843898937, 0.389651029808109, 0.345025935039879,
0.624846648212751, 0.58851623834231, 0.887105187034623, 0.321097430780683,
0.235333454689797, 0.317948692315331, 0.221829921658406, 0.741550229770297,
0.20323638533706, 0.300011118692256, 0.10709664298948, 0.128658851710521,
0.215608428571485, 0.850613750148793, 0.520018226875275, 0.229616805359878,
0.795191910398009, 0.0903188238048897, 0.397921159282847, 0.203044598795871,
0.56273414261286, 0.574577913744773, 0.442200678899054, 0.245465935323322,
0.278019517090414, 0.352947382006002, 0.174645818312427, 0.122145774176944,
0.170757436677423, 0.397071879644391, 0.191901705813107, 0.0904542790515756,
0.185273222274775, 0.132590145000319, 0.371241327758872, 0.58624534957165,
0.0326746116517388, 0.314917326956508, 0.949721350006339, 0.452309070827074,
0.103763927657044, 0.0701849859768701, 0.586205681965722, 0.1872300676421,
0.362091824661685, 0.528553649819102, 0.190539638058716, 0.0327545040641537,
0.762165679327963, 0.274571490276717, 0.512464730498834, 0.27234499730487,
0.650035734997207, 0.713152866696705, 0.199327736885447, 0.888922212975112,
0.256517093465039, 0.0491122775280585, 0.145917596045208, 0.519396637888038,
0.0794241734859925, 0.109718966941177, 0.149020433838973, 0.508447476265555,
0.175000348118242, 0.226815958091545, 0.282973740661336, 0.320684074902339,
0.299983143281491, 0.081224779932806, 0.959972054857087, 0.40619879677503,
0.844555422937453, 0.534525371689877, 0.220441245612505, 0.188279840325184,
0.184744989425758, 0.577591333604708, 0.182105298203728, 0.578046328506155,
0.447224424542321, 0.545195283609091, 0.620149367286776, 0.143021628347417,
0.365110938943906), actual = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 2L, 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, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 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, 2L, 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, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 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, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L), .Label = c("0",
"1"), class = "factor"), mod2 = c(0.00921301729977131, 0.420679152011871,
0.280125766992569, 0.829287350177765, 0.0740553513169289, 0.0137514220550656,
0.345879256725311, 0.675718426704407, 0.224963515996933, 0.0168505609035492,
0.206158310174942, 0.0561505369842052, 0.013946115039289, 0.0312005542218685,
0.812829315662384, 0.0040231547318399, 0.164083942770958, 0.144838228821754,
0.0544591136276722, 0.0705596581101418, 0.0993659123778343, 0.0502875261008739,
0.0711468532681465, 0.97149133682251, 0.0123780304566026, 0.0158327762037516,
0.0151439299806952, 0.161300778388977, 0.0351154617965221, 0.371416091918945,
0.102943770587444, 0.757962644100189, 0.0110324919223785, 0.047280989587307,
0.353544414043427, 0.103088781237602, 0.050556942820549, 0.0350653082132339,
0.401749402284622, 0.242998450994492, 0.95191890001297, 0.247199147939682,
0.877671599388123, 0.0228813849389553, 0.316035985946655, 0.0982891768217087,
0.105327241122723, 0.0394041128456593, 0.158778890967369, 0.965853333473206,
0.00885774753987789, 0.0148940868675709, 0.0758267268538475,
0.157344698905945, 0.551176071166992, 0.12992300093174, 0.0138051174581051,
0.475996285676956, 0.948372185230255, 0.134769633412361, 0.556590795516968,
0.241968929767609, 0.0591361075639725, 0.136440336704254, 0.0668027997016907,
0.0214910972863436, 0.168018579483032, 0.0699143782258034, 0.0613136924803257,
0.983657658100128, 0.01039685215801, 0.0587030127644539, 0.00913762766867876,
0.0317753851413727, 0.0524604544043541, 0.0171482041478157, 0.0351034365594387,
0.0221143364906311, 0.390063345432281, 0.0124582275748253, 0.0435651019215584,
0.960456311702728, 0.981251120567322, 0.962678015232086, 0.68353945016861,
0.971885144710541, 0.00406940141692758, 0.0203599724918604, 0.0633115172386169,
0.130489587783813, 0.00637582363560796, 0.755789935588837, 0.00400055898353457,
0.0196541585028172, 0.452250599861145, 0.902487516403198, 0.0917441099882126,
0.70908921957016, 0.854791462421417, 0.0794311910867691, 0.0273289680480957,
0.0311463791877031, 0.0127724697813392, 0.0723041146993637, 0.95717054605484,
0.264508247375488, 0.127880096435547, 0.0171502381563187, 0.0879878774285316,
0.057909045368433, 0.195128262042999, 0.0270854383707047, 0.0782773569226265,
0.0383500903844833, 0.372881323099136, 0.285864651203156, 0.116122633218765,
0.0114724459126592, 0.158793538808823, 0.761920690536499, 0.861652076244354,
0.227078512310982, 0.0417833961546421, 0.968308210372925, 0.249760672450066,
0.817656457424164, 0.657787919044495, 0.0230655912309885, 0.991730153560638,
0.243526577949524, 0.156855791807175, 0.269827306270599, 0.2051682472229,
0.254308879375458, 0.0800860077142715, 0.34795343875885, 0.972077071666718,
0.0477356761693954, 0.0464455783367157, 0.736665904521942, 0.0210415944457054,
0.0383482202887535, 0.00386064685881138, 0.012203705497086, 0.857867896556854,
0.0814997106790543, 0.0443238392472267, 0.988256871700287, 0.0165475029498339,
0.0615949258208275, 0.0449854917824268, 0.0454509183764458, 0.01393414568156,
0.0262223742902279, 0.186277061700821, 0.0324785746634007, 0.033482164144516,
0.00819115899503231, 0.0117478491738439, 0.0783571749925613,
0.0223740469664335, 0.0198381245136261, 0.0501869209110737, 0.0376051589846611,
0.0624551437795162, 0.0314894691109657, 0.860675990581512, 0.0612487271428108,
0.276889532804489, 0.0167709998786449, 0.0528305657207966, 0.00499858101829886,
0.0550522655248642, 0.0401820614933968, 0.15572227537632, 0.025402145460248,
0.0761110484600067, 0.0188306048512459, 0.0380858443677425, 0.0132785346359015,
0.0640550553798676, 0.00517340190708637, 0.0772885754704475,
0.19677597284317, 0.70759129524231, 0.147291049361229, 0.0479679442942142,
0.448112070560455, 0.438730537891388, 0.1216806396842, 0.00735889840871096,
0.400070250034332, 0.11030688136816, 0.0442567691206932, 0.843577682971954,
0.558653950691223, 0.0176850743591785, 0.0946979001164436, 0.0153300678357482,
0.0788581073284149, 0.287300169467926, 0.18515208363533, 0.0233458057045937,
0.222889557480812, 0.0536097027361393, 0.046075414866209, 0.454137802124023,
0.0345671623945236, 0.0211565420031548, 0.00878079421818256,
0.0967049226164818, 0.0367577895522118, 0.0918731242418289, 0.801138401031494,
0.863967061042786, 0.481592088937759, 0.0336476154625416, 0.042931966483593,
0.146649375557899, 0.879879474639893, 0.0114125544205308, 0.00469835428521037,
0.0382965281605721, 0.00703405775129795, 0.0215587317943573,
0.653248488903046, 0.0782845988869667, 0.121728673577309, 0.0159873776137829,
0.146458551287651, 0.0206322781741619, 0.0499792955815792, 0.406131267547607,
0.0681000128388405, 0.0272951126098633, 0.0921002179384232, 0.705728650093079,
0.0432888120412827, 0.0904701724648476, 0.701448082923889, 0.150377616286278,
0.0676496997475624, 0.0228539109230042, 0.0765143036842346, 0.113855704665184,
0.0267140381038189, 0.0929989665746689, 0.152857646346092, 0.0418933369219303,
0.565863370895386, 0.0166599620133638, 0.0156008023768663, 0.0115091372281313,
0.903479337692261, 0.986901104450226, 0.928888380527496, 0.0101279402151704,
0.0802175477147102, 0.0816037356853485, 0.184011369943619, 0.306637078523636,
0.153407230973244, 0.0167400408536196, 0.22508542239666, 0.0621875934302807,
0.045804962515831, 0.171572059392929, 0.282828807830811, 0.0179158430546522,
0.840476810932159, 0.0105379819869995, 0.0114276595413685, 0.0119720129296184,
0.181616917252541, 0.0978458821773529, 0.12760978937149, 0.302467346191406,
0.833696305751801, 0.0464334487915039, 0.945941805839539, 0.511578798294067,
0.974729835987091, 0.0164413973689079, 0.0989365875720978, 0.0765514746308327,
0.0109183257445693, 0.27131775021553, 0.107158131897449, 0.0736118629574776,
0.00482818158343434, 0.0201019216328859, 0.941181838512421, 0.0378244519233704,
0.797275304794312, 0.351534575223923, 0.117122322320938, 0.493010193109512,
0.0246974248439074, 0.0206503849476576, 0.0464012213051319, 0.959466278553009,
0.103862524032593, 0.0122975073754787, 0.380784571170807, 0.0136013478040695,
0.184185728430748, 0.0669872164726257, 0.214213669300079, 0.0391909405589104,
0.0619069524109364, 0.0159242562949657, 0.0373520478606224, 0.00958761665970087,
0.377378851175308, 0.166095584630966, 0.925777494907379, 0.0208236053586006,
0.0404847823083401, 0.559196531772614, 0.0746391415596008, 0.325236350297928,
0.0599571503698826, 0.26615184545517, 0.0652189999818802, 0.0173883624374866,
0.0242639016360044, 0.024857934564352, 0.0169697199016809, 0.911997854709625,
0.100104205310345, 0.167153507471085, 0.787109971046448, 0.0342921391129494,
0.15250888466835, 0.0209930054843426, 0.0491697303950787, 0.13340713083744,
0.471260368824005, 0.0106122875586152, 0.144313588738441, 0.0787744149565697,
0.235925808548927, 0.0196361448615789, 0.0727006196975708, 0.0628127604722977,
0.0305791813880205, 0.0411793142557144, 0.195792764425278, 0.0215679779648781,
0.239042669534683, 0.150531396269798, 0.0177980363368988, 0.0576319955289364,
0.959489762783051, 0.480692893266678, 0.0187541544437408, 0.00829488877207041,
0.0474584363400936, 0.0482899323105812, 0.785768091678619, 0.0142318215221167,
0.0503279566764832, 0.00939451158046722, 0.37018147110939, 0.0408202894032001,
0.196333780884743, 0.128449380397797, 0.934917628765106, 0.792946517467499,
0.138556912541389, 0.706277251243591, 0.00852821208536625, 0.0416146814823151,
0.0253815017640591, 0.825974524021149, 0.0193344969302416, 0.0097988685593009,
0.0383418351411819, 0.791619479656219, 0.138332143425941, 0.017676180228591,
0.0617045052349567, 0.00605513388291001, 0.0927852019667625,
0.0261132270097733, 0.953198134899139, 0.182122096419334, 0.958361387252808,
0.270839661359787, 0.0256280936300755, 0.0315133333206177, 0.0611352697014809,
0.410940438508987, 0.0302151944488287, 0.868182957172394, 0.0327513180673122,
0.0963760241866112, 0.955038785934448, 0.0473414175212383, 0.0430381260812283
)), row.names = c(NA, 400L), class = "data.frame")

setting color axis limits in ggplot2

I am trying to make a heat-map of chlorophyll fluorescence vs depth and time. I have things working pretty ok, but I'm trying to improve my colour contrast. I generate my heatmap with the following code.
ggplot(subset(ctdamotInt2, variable == 'fluorescence'), aes(time, depth)) +
geom_tile(aes(fill = log10(value))) + scale_y_reverse(limits = c(110, 0)) +
scale_x_time(limits = c(min(subset(ctdamot, variable == 'nh4')$time) - 2 * 60^2, max(subset(ctdamot, variable == 'nh4')$time) + 2* 60^2)) +
geom_point(data = samplesCTD, aes( x = time, y = depth)) +
scale_fill_gradient2(low = "blue", mid = "white", high = "green")
Generally I am finding that the dark green colours essentially never get utilized and so my heatmap ends up looking washed out and doesn't do a great job of communicating where chlorophyll fluorescence is greatist If I were working in matlab, I would get around this by setting
caxis([-1 0.4])
which would set all values above 0.4 to the maximum green value. You wouldn't be able to tell the relative difference of the really high values, but you'd at least be able to get a better idea about the relative differences of the intermediate values that make up most of the plot. Any suggestions on how I can have a larger proportion of this plot be green? I suppose I could manually rescale the input values, but would rather not if there is a better way.
Edit: At the request of Mike H
dput(head(ctdamotInt2,100))
structure(list(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), .Label = c("temperature", "salinity",
"fluorescence", "oxygen", "nh4"), class = "factor"),
depth = 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), time = structure(c(1482764087,
1482767687, 1482771287, 1482774887, 1482778487, 1482782087,
1482785687, 1482789287, 1482792887, 1482796487, 1482800087,
1482803687, 1482807287, 1482810887, 1482814487, 1482818087,
1482821687, 1482825287, 1482828887, 1482832487, 1482836087,
1482839687, 1482843287, 1482846887, 1482850487, 1482854087,
1482857687, 1482861287, 1482864887, 1482868487, 1482872087,
1482875687, 1482879287, 1482882887, 1482886487, 1482890087,
1482893687, 1482897287, 1482900887, 1482904487, 1482908087,
1482911687, 1482915287, 1482918887, 1482922487, 1482926087,
1482929687, 1482933287, 1482936887, 1482940487, 1482944087,
1482947687, 1482951287, 1482954887, 1482958487, 1482962087,
1482965687, 1482969287, 1482972887, 1482976487, 1482980087,
1482983687, 1482987287, 1482990887, 1482994487, 1482998087,
1483001687, 1483005287, 1483008887, 1483012487, 1483016087,
1483019687, 1483023287, 1483026887, 1483030487, 1483034087,
1483037687, 1483041287, 1483044887, 1483048487, 1483052087,
1483055687, 1483059287, 1483062887, 1483066487, 1483070087,
1483073687, 1483077287, 1483080887, 1483084487, 1483088087,
1483091687, 1483095287, 1483098887, 1483102487, 1483106087,
1483109687, 1483113287, 1483116887, 1483120487), class = c("POSIXct",
"POSIXt")), value = c(27.3483, 27.3483, 27.3483, 27.3483,
27.4404348314607, 27.5325696629213, 27.624704494382, 27.7168393258427,
27.8089741573034, 27.901108988764, 27.9932438202247, 28.0853786516854,
28.1006709677419, 28.1151870967742, 28.1297032258065, 28.1602961677656,
28.3392342471866, 28.5181723266075, 28.6971104060285, 28.8760484854494,
29.0549865648704, 29.1744078768732, 29.2330425521923, 29.2916772275114,
29.3503119028306, 29.4089465781497, 29.4675812534688, 29.5262159287879,
29.5233725024786, 29.5198033650201, 29.5162342275617, 29.5126650901032,
29.5090959526448, 29.5055268151863, 29.5019576777279, 29.4983885402694,
29.494819402811, 29.4392079391567, 29.3230472306014, 29.2068865220461,
29.0907258134908, 28.9745651049355, 28.8584043963802, 28.7422436878249,
28.6260829792696, 28.5099222707143, 28.5396702257581, 28.6045126836247,
28.6693551414913, 28.734197599358, 28.7990400572246, 28.8638825150912,
28.9287249729579, 28.9935674308245, 29.0584098886912, 29.1232523465578,
29.1880948044244, 29.2529372622911, 29.3177797201577, 29.3826221780244,
29.447464635891, 29.5123070937576, 29.4047436790674, 29.2746548739928,
29.1445660689182, 29.0144772638436, 28.8843884587691, 28.7542996536945,
28.6242108486199, 28.4941220435453, 28.4440444629526, 28.4161338799902,
28.3882232970279, 28.3603127140655, 28.3324021311032, 28.3044915481409,
28.2765809651785, 28.2486703822162, 28.2207597992539, 28.1928492162915,
28.1649386333292, 28.1370280503668, 28.1091174674045, 28.0812068844422,
28.0532963014798, 28.0253857185175, 27.9974751355552, 27.9695645525928,
27.9416539696305, 27.9137433866682, 27.8858328037058, 27.8579222207435,
27.8300116377811, 27.8021010548188, 27.7741904718565, 27.7462798888941,
27.7183693059318, 27.6904587229695, 27.6625481400071, 27.6346375570448
)), .Names = c("variable", "depth", "time", "value"), row.names = c(NA, 100L), class = "data.frame")

some data are not showing up on the ggplot2 window

head(x)
Region Type Date count
1 Americas Point 2011-10-26 1
2 Americas Point 2011-10-27 2
3 Americas Point 2011-10-31 1
4 Americas Point 2011-11-01 1
5 Americas Point 2011-12-05 1
6 Americas Point 2011-12-07 1
dput(x)
structure(list(Region = 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), .Label = "Americas", class = "factor"), Type = 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), .Label = "Point", class = "factor"),
Date = structure(c(15273, 15274, 15278, 15279, 15313, 15315,
15316, 15320, 15341, 15342, 15351, 15358, 15370, 15390, 15392,
15405, 15407, 15411, 15418, 15421, 15433, 15467, 15470, 15482,
15495, 15497, 15503, 15517, 15530, 15551, 15554, 15582, 15586,
15589, 15593, 15601, 15602, 15610, 15615, 15616, 15624, 15643,
15645, 15656, 15663, 15664, 15665, 15672, 15673, 15677, 15678,
15679, 15680, 15684, 15686, 15693, 15694, 15698, 15699, 15705,
15706, 15707, 15712, 15713, 15714, 15719, 15720, 15721, 15727,
15736, 15740, 15741, 15742, 15743), class = "Date"), count = c(1L,
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, 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, 1L)), .Names = c("Region",
"Type", "Date", "count"), row.names = c(NA, -74L), class = "data.frame")
I am trying to build a stack bar graph as follows:
ggplot(x, aes(Date, count, group=Region)) +
geom_bar(aes(fill=Type, width=0.3),stat="identity", position="stack") +
scale_x_date(breaks = "1 month",
minor_breaks = "2 weeks",
labels=date_format("%b-%y")) +
geom_smooth(method="lm", se=T, size=0.5, colour="yellow") +
facet_wrap(~Region)
by default, I see some missing points but when I stretched the plot window, points appear. I really need all the points in the chart, other wise it looks like I am miss reporting the data. Any suggestions how can I address this so that I see all the data points on the chart. My window size is 500 by 500.
Indeed, by increasing the screen size more bars appear. You can't see them in the small window of the R console because the width of the bars is too small. But when you save it, the bars can be seen in the output:
plot <- ggplot(x, aes(Date, count, group=Region)) +
geom_bar(aes(fill=Type, width=0.3),stat="identity", position="stack") +
scale_x_date(breaks = "1 month",
minor_breaks = "2 weeks") +
geom_smooth(method="lm", se=T, size=0.5, colour="yellow") +
facet_wrap(~Region)
ggsave("test.pdf",plot )
To see all the points in the R console increase the width, for instance:
(plot <- ggplot(x, aes(Date, count, group=Region)) +
geom_bar(aes(fill=Type, width=1),stat="identity", position="stack") +
scale_x_date(breaks = "1 month",
minor_breaks = "2 weeks") +
geom_smooth(method="lm", se=T, size=0.5, colour="yellow") +
facet_wrap(~Region))

plotting only time using ggplot2

I have a data frame like this:
head(yy)
Team Date STime ETime
1 A 2012-03-06 07:03 10:13
2 A 2012-03-06 07:03 10:13
3 A 2012-03-06 07:03 10:13
4 A 2012-03-06 07:03 10:13
5 A 2012-03-06 07:03 10:13
6 A 2012-03-06 07:03 10:13
dput(yy)
dput(yy)
structure(list(Team = 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), .Label = "A", class = "factor"),
Date = 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), .Label = "2012-03-06", class = "factor"),
STime = 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), .Label = "07:03", class = "factor"),
ETime = 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), .Label = "10:13", class = "factor")), .Names = c("Team",
"Date", "STime", "ETime"), class = "data.frame", row.names = c(NA,
-50L))
I like to see the y-axis from 00:00 23:59 in 2 hours increment and be able to draw a red line on STime value.
I have somthing like this but it does not look right:
ggplot(yy, aes(Date, ETime, group="Team")) + geom_jitter(size=0.05) + facet_wrap( ~ Team) + geom_hline(yintercept=yy$Stime, colour="red", size=2)
how would you do this in ggplot2? Can somebody give me pointers/start me in the right direction?
Regards,
You have to format your times into actual times. Right now they are factors (Check your data frame with str(yy)). When ETime is plotted, the single time is plotted as 1 and labeled "10:13." So, the solution below first converts the string "10:13" into a time (strptime) then converts it to POSIXct, or seconds since an origin (1/1/1970).
library(ggplot2); library(scales)
#Convert date string into POSIXct format
yy$STime <- as.POSIXct(strptime(yy$STime, format = "%H:%M", tz = "UTC"))
yy$ETime <- as.POSIXct(strptime(yy$ETime, format = "%H:%M", tz = "UTC"))
#Define y-axis limits
lims <- as.POSIXct(strptime(c("0:00","23:59"), format = "%H:%M", tz= "UTC"))
ggplot(yy, aes(Date, ETime, group="Team")) + geom_jitter(size=1) + facet_wrap( ~ Team) +
geom_hline(data = yy, aes(yintercept= as.numeric(STime)), colour="red", size=2) +
scale_y_datetime(limits =lims, breaks=date_breaks("2 hour"),
labels=date_format("%H:%M", tz = "UTC") )
Note on geom_line to date axis.
Pay attention to your timezones too. Otherwise R/ggplot will format things according to your local time zone.

Resources