change data type by indexing - r

I want to change some columns to integers (from the 4 column of my table to the last one). How can I do that?
My data
I already tried
databianca %>%
mutate_if(is.factor, ~as.integer(as.character(.)))
and
databianca %>%
mutate_at(vars(columbia:escrita.POS), ~as.integer(as.character(.)))
and with both of them I get these warnings:
Warning messages:
1: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
2: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
3: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
4: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
5: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
6: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
7: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
8: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
Isn't there an option to treat all columns from columbia to the last column (in matlab I would use 4:end to give the instructions from the 4th column until the last element) as integrals?
my data via dpasta:
databianca <- data.frame(
SEXO = as.factor(c("F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "M", "M", "M", "M",
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M",
"M", "M", "M", "M", "M", "M", "M", "M", "M",
"M", "M", "M", "M")),
GRUPOS = as.factor(c("C", "E", "E", "C", "C", "C", "E", "E", "E",
"C", "E", "E", "E", "C", "C", "C", "E", "C",
"E", "E", "C", "E", "C", "C", "C", "E", "E",
"E", "C", "E", "C", "C", "C", "E", "C", "E", "C",
"E", "E", "C", "C", "C", "E", "E", "E", "E",
"C", "C", "C", "C")),
SALAS = as.factor(c("B", "A", "C", "A", "A", "C", "A", "B", "C",
"C", "C", "B", "A", "C", "A", "C", "A", "C",
"C", "C", "B", "B", "B", "A", "B", "C", "A",
"B", "A", "A", "C", "B", "B", "A", "A", "B", "B",
"B", "C", "B", "B", "B", "B", "A", "A", "B",
"C", "C", "A", "B")),
token = c(100L, 95L, 106L, 112L, 84L, 125L, 108L, 114L, 112L,
117L, 84L, 96L, 124L, 118L, 99L, 84L, 95L, 113L, 123L,
135L, 100L, 92L, 101L, 129L, 93L, 106L, 101L, 84L, 84L,
128L, 113L, 95L, 109L, 127L, 112L, 108L, 108L, 118L, 97L,
112L, 116L, 88L, 92L, 111L, 123L, 112L, 116L, 104L, 90L,
89L),
hab.visuo = c(3L, 3L, 2L, 0L, 0L, 1L, 3L, 3L, 2L, 1L, 1L, 1L, 0L, 2L,
1L, 1L, 2L, 2L, 0L, 7L, 3L, 2L, 0L, 2L, 0L, 3L, 1L, 0L,
1L, 1L, 1L, 1L, 0L, 2L, 2L, 1L, 1L, 0L, 0L, 0L, 1L, 2L,
1L, 1L, 0L, 2L, 0L, 0L, 3L, 3L),
tamanho = c(7L, 4L, 8L, 7L, 7L, 8L, 8L, 8L, 8L, 7L, 7L, 7L, 8L, 8L,
8L, 6L, 7L, 7L, 8L, 8L, 8L, 7L, 7L, 8L, 8L, 8L, 7L, 7L,
7L, 7L, 8L, 8L, 7L, 8L, 7L, 8L, 8L, 7L, 7L, 7L, 8L, 8L,
8L, 8L, 8L, 7L, 8L, 8L, 7L, 7L),
forma = c(6L, 6L, 7L, 4L, 6L, 6L, 6L, 7L, 6L, 6L, 6L, 6L, 6L, 5L,
6L, 7L, 5L, 6L, 7L, 7L, 6L, 7L, 0L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 2L, 2L, 6L, 7L, 7L, 6L, 7L, 7L,
6L, 7L, 6L, 6L, 6L, 6L, 6L, 7L),
nomeou.cor = c(67L, 82L, 85L, 83L, 68L, 72L, 74L, 74L, 80L, 80L, 63L,
70L, 80L, 81L, 68L, 72L, 81L, 76L, 78L, 81L, 81L, 80L,
65L, 78L, 74L, 75L, 61L, 63L, 69L, 79L, 70L, 75L, 77L,
79L, 74L, 78L, 70L, 79L, 71L, 70L, 78L, 53L, 67L, 75L, 83L,
82L, 84L, 71L, 53L, 69L),
nomeou.incor = c(23L, 8L, 5L, 7L, 22L, 18L, 16L, 16L, 10L, 10L, 27L,
20L, 10L, 9L, 22L, 18L, 9L, 14L, 12L, 9L, 8L, 10L, 25L,
12L, 16L, 15L, 29L, 27L, 21L, 11L, 20L, 15L, 13L, 11L,
16L, 12L, 20L, 11L, 19L, 20L, 12L, 37L, 23L, 15L, 15L, 8L,
6L, 19L, 38L, 21L),
token.POS = c(120L, 120L, 136L, 117L, 122L, 118L, 135L, 122L, 124L,
112L, 98L, 127L, 130L, 119L, 116L, 94L, 122L, 125L,
130L, 123L, 113L, 116L, 105L, 109L, 113L, 120L, 99L, 87L,
0L, 123L, 0L, 94L, 136L, 125L, 118L, 124L, 123L, 128L,
101L, 124L, 131L, 87L, 116L, 120L, 117L, 119L, 123L, 127L,
91L, 105L),
hab.visuo.POS = c(1L, 2L, 2L, 1L, 2L, 5L, 2L, 1L, 3L, 0L, 2L, 2L, 1L, 2L,
5L, 1L, 0L, 0L, 0L, 5L, 0L, 2L, 0L, 1L, 0L, 2L, 3L, 1L,
0L, 1L, 0L, 1L, 0L, 3L, 1L, 4L, 1L, 0L, 2L, 0L, 1L, 0L,
0L, 2L, 3L, 2L, 7L, 1L, 2L, 3L),
tamanho.POS = c(8L, 8L, 8L, 7L, 8L, 7L, 7L, 8L, 8L, 8L, 7L, 8L, 8L, 8L,
8L, 7L, 8L, 8L, 8L, 7L, 8L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
0L, 7L, 0L, 7L, 8L, 8L, 7L, 7L, 7L, 8L, 7L, 8L, 7L, 7L,
8L, 6L, 8L, 7L, 8L, 7L, 8L, 7L),
forma.POS = c(6L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 6L, 5L, 7L, 6L, 6L, 7L,
5L, 5L, 7L, 5L, 7L, 8L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 6L,
0L, 7L, 0L, 5L, 7L, 7L, 7L, 7L, 2L, 7L, 5L, 5L, 5L, 7L,
6L, 7L, 7L, 6L, 6L, 7L, 2L, 6L),
nomeou.cor.POS = c(78L, 78L, 85L, 73L, 70L, 76L, 77L, 78L, 82L, 74L, 56L,
70L, 79L, 78L, 65L, 67L, 87L, 78L, 82L, 82L, 79L, 84L,
74L, 78L, 71L, 79L, 61L, 70L, 0L, 75L, 0L, 74L, 83L, 77L,
76L, 77L, 75L, 77L, 68L, 79L, 79L, 61L, 67L, 76L, 79L,
78L, 81L, 68L, 53L, 71L),
nomeou.incor.POS = c(12L, 12L, 5L, 17L, 20L, 14L, 13L, 12L, 8L, 16L, 34L,
20L, 11L, 12L, 25L, 23L, 3L, 12L, 8L, 8L, 11L, 6L, 16L,
12L, 19L, 11L, 29L, 20L, 0L, 15L, 0L, 16L, 7L, 13L, 14L,
13L, 15L, 13L, 22L, 11L, 11L, 29L, 23L, 14L, 11L, 12L,
9L, 22L, 37L, 19L),
columbia = as.factor(c("49,3", "55,38", "67,69", "53,33", "38,46",
"53,84", "63,07", "61,53", "63,07", "35,38",
"43,33", "38,33", "55,38", "67,69", "47,69",
"29,23", "60", "53,84", "61,66", "73,33", "47,
69", "46,66", "41,66", "56,66", "47,69", "61,66",
"33,33", "40", "60", "36,66", "51,66", "43,33",
"58,46", "60", "56,92", "38,33", "40", "60",
"53,33", "56,92", "61,53", "53,84", "44,61",
"47,69", "44,61", "66,15", "56,66", "58,33", "65",
"51,66")),
escrita = as.factor(c("2", "2", "2", "2", "2", "1,5", "2,25", "2",
"2", "2", "2", "2", "2", "2,75", "2", "2,5",
"1,75", "2", "3", "4,5", "1,75", "1,75", "2",
"2", "2", "2", "1", "1,5", "2", "2", "2", "2,5",
"2", "2", "2", "1,75", "1,75", "2", "2", "2,
25", "2", "2", "1,75", "1", "2", "0", "2,5", "2",
"1,25", "1,5")),
columbia.POS = as.factor(c("53,8", "66,7", "76,7", "31,7", "43,1", "58,
5", "56,7", "55", "70", "35,4", "28,3", "43,
3", "61,7", "68,3", "55,4", "28,3", "55", "56,7",
"68,3", "66,7", "41,5", "56,7", "50", "65",
"58,5", "75", "48,3", "31,7", "0", "53,3", "0",
"50", "65", "66,7", "73,8", "51,7", "45", "66,
7", "48,3", "69,2", "73,8", "43,3", "56,7", "47,
7", "51,7", "73,3", "66,7", "38,3", "43,3", "33,
3")),
escrita.POS = as.factor(c("2", "3", "3", "1,5", "1,5", "2", "3", "3",
"4", "2", "1,5", "3", "2", "4", "1,5", "3",
"2", "3", "4", "4", "1,5", "2", "3", "1,5", "2",
"2", "1,5", "1,5", "0", "1,5", "0", "2", "3",
"1,5", "1,5", "1,5", "1,5", "2", "1,5", "3",
"2", "2", "2", "1,5", "1,5", "1,5", "5", "3", "1,
5", "1,5"))
)

This happens when you use as.integer on non-integer variables.
It seems your numbers have "," in them (ex: 51,66 ) and also new line character which is creating problem so first make your factors "clean" with gsub() and then run as.integer()
databianca[,4:length(names(databianca))] <- apply(databianca[, 4:length(names(databianca))], 2, function(x) as.integer(gsub("[\n,]", "", x)))
This should also work
databianca[,4:length(names(databianca))] <- as.integer(gsub("[\n,]", "", as.matrix(databianca[,4:length(names(databianca))])))

Related

Why is geom_line() not connecting through geom_point()?

Question: why is geom_line() not connecting through geom_point()?
I have:
Written with
ggplot(a,
aes(x = month, color = year, fill = year)) +
scale_color_manual(values = colsze) +
scale_fill_manual(values = alpha(colsze, .2)) +
scale_x_discrete(labels = c("January", "February", "March", "April", "May",
"June", "July", "August", "Septemer",
"October", "November", "December")) +
geom_point(aes(y = n), size = 4, shape=19) +
geom_line(aes(y = n)) +
scale_y_continuous(breaks = seq(0, 120, 10), limits = c(0, 120)) +
facet_wrap(.~year)
I cannot figure out why this does not work? E.g. following tutorials like this
geom_line() seems to appear in the legend but not in plot.
a <- structure(list(month = structure(c(4L, 1L, 4L, 7L, 1L, 9L, 2L,
8L, 8L, 10L, 7L, 10L, 9L, 9L, 9L, 2L, 10L, 7L, 4L, 2L, 2L, 3L,
11L, 11L, 12L, 9L, 12L, 10L, 10L, 10L, 11L, 5L, 10L, 10L, 10L,
10L, 10L, 12L, 11L, 7L, 12L, 6L, 9L, 9L, 9L, 7L, 9L, 4L, 12L,
12L, 11L, 3L, 3L, 11L, 11L, 11L, 7L, 11L, 12L, 12L, 12L, 2L,
4L, 1L, 11L, 11L, 1L, 4L, 8L, 2L, 10L, 5L, 5L, 6L, 7L, 11L, 11L,
11L, 11L, 11L, 12L, 11L, 10L, 7L, 12L, 9L, 9L, 7L, 10L, 8L, 8L,
5L, 9L, 10L, 9L, 3L, 8L, 10L, 10L, 8L), .Label = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), class = "factor"),
year = structure(c(3L, 3L, 2L, 1L, 4L, 4L, 4L, 1L, 1L, 1L,
3L, 1L, 2L, 1L, 1L, 3L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 3L,
1L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 2L,
4L, 4L, 2L, 2L, 2L, 2L, 1L, 3L, 1L, 1L, 2L, 1L, 1L, 2L, 2L,
2L, 4L, 1L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 1L, 2L, 2L, 1L, 4L,
2L, 1L, 1L, 4L, 4L, 2L, 2L, 4L, 4L, 2L, 3L, 3L, 3L, 4L, 4L,
1L, 1L, 3L, 4L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 1L, 1L, 1L
), .Label = c("2017", "2018", "2019", "2020"), class = "factor"),
n = c(92L, 95L, 83L, 95L, 70L, 88L, 94L, 103L, 103L, 98L,
95L, 98L, 90L, 89L, 89L, 76L, 98L, 97L, 79L, 103L, 103L,
111L, 104L, 104L, 73L, 89L, 73L, 107L, 107L, 107L, 88L, 111L,
107L, 107L, 107L, 107L, 107L, 73L, 104L, 78L, 87L, 92L, 90L,
90L, 90L, 78L, 89L, 92L, 98L, 98L, 85L, 111L, 111L, 85L,
85L, 85L, 97L, 104L, 73L, 73L, 73L, 71L, 92L, 99L, 85L, 104L,
99L, 83L, 103L, 94L, 90L, 90L, 90L, 92L, 97L, 85L, 85L, 88L,
88L, 85L, 73L, 89L, 107L, 97L, 87L, 89L, 89L, 95L, 96L, 103L,
103L, 75L, 90L, 90L, 90L, 88L, 87L, 98L, 98L, 103L)), row.names = c(NA,
-100L), groups = structure(list(month = structure(c(1L, 1L, 1L,
2L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 7L, 7L, 7L,
7L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
12L, 12L, 12L), .Label = c("1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12"), class = "factor"), year = structure(c(2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 3L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 3L, 4L), .Label = c("2017", "2018", "2019", "2020"), class = "factor"),
.rows = structure(list(c(64L, 67L), 2L, 5L, 20:21, 62L, 16L,
c(7L, 70L), c(22L, 52L, 53L), 96L, 19L, c(3L, 68L), c(1L,
48L, 63L), 72:73, 92L, 32L, c(42L, 74L), 4L, c(40L, 46L
), c(11L, 88L), c(18L, 57L, 75L, 84L), c(8L, 9L, 69L,
90L, 91L, 100L), 97L, c(14L, 15L, 26L, 47L, 86L, 87L),
c(13L, 43L, 44L, 45L, 93L, 95L), 6L, c(10L, 12L, 17L,
98L, 99L), c(71L, 94L), c(28L, 29L, 30L, 33L, 34L, 35L,
36L, 37L, 83L), 89L, c(23L, 24L, 39L, 58L, 66L), c(51L,
54L, 55L, 56L, 65L, 76L, 77L, 80L), 82L, c(31L, 78L,
79L), 49:50, c(25L, 27L, 38L, 59L, 60L, 61L, 81L), c(41L,
85L)), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 36L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Try this:
ggplot(a,
aes(x = as.numeric(month), color = year, fill = year)) +
# scale_color_manual(values = colsze) +
# scale_fill_manual(values = alpha(colsze, .2)) +
scale_x_continuous(breaks = c(1,2,3,4,5,6,7,8,9,10,11,12),
labels = c("January", "February", "March", "April", "May",
"June", "July", "August", "Septemer",
"October", "November", "December")) +
geom_point(aes(y = n), size = 4, shape=19) +
geom_line(aes(y = n)) +
scale_y_continuous(breaks = seq(0, 120, 10), limits = c(0, 120)) +
facet_wrap(.~year)
I commented on those two lines because in your reproducible example there is no variable colsze.
The problem is that month is a factor and must first be converted to numeric. For a better visualization, evaluate whether to rotate the labels on the x axis by 45 °

How to establish factorized color gradient in a heatmap

I have this plot which I've generated using ggplot2 in R.
The code which produces the plot is as follows:
ggplot(mockdata) +
facet_grid(~ type, scales='free_x', space="free_x") +
geom_tile(aes(variable, Measurement, fill = mockdata$plotval), colour = "dark red") +
scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() +
theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28)) +
labs(title="", x="", y="", fill="")
As evident from both the plot and the code, the color scheme ranges from -20 to 20. plotval in my data actually represents -log10 of a P-value and so is always a positive number, but I've split it up into positive and negative by multiplying it with 1 or -1 depending of the direction of effect (the direction column in mockdata).
What I want to do is to split my gradient colorbar in two so that it ranges just from 0 to 20 but I would still like each tile to be red or blue and I would like the intensity of both the blue and the red can be seen side by side on the colorbar. I also want to increase the size of the colorbar but I guess that is strictly another question
dput(mockdata)
structure(list(Measurement = structure(c(20L, 19L, 18L, 17L,
16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L,
2L, 1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L,
9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 20L, 19L, 18L, 17L, 16L,
15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L,
1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L,
8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 20L, 19L, 18L, 17L, 16L, 15L,
14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L,
20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L,
7L, 6L, 5L, 4L, 3L, 2L, 1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L,
13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 20L,
19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L,
6L, 5L, 4L, 3L, 2L, 1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L,
12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), .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",
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
"36", "37", "38", "39", "40", "41", "42"), class = "factor"),
category = structure(c(3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 8L), .Label = c("x1", "x2", "x3", "x4",
"x5", "x6", "x7", "x8", "x9"), 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 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, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 8L, 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, 9L), .Label = c("A",
"B", "C", "a", "b", "c", "d", "e", "f"), class = "factor"),
Pval = c(6760.4856938748, 2.03462777623711e-09, 41.184282929783,
0.0391464204769019, 0.305206224642095, 0.000215483198859283,
6.32953239686189e-08, 101.908214964603, 1172.4434810623,
0.256516710363617, 3.19424561429881, 2447.68986203703, 0.00034685390841221,
0.0766588456064384, 22877715.3007328, 9841.61882613746, 3652.68781929919,
51891.7266692688, 0.194178098258849, 1026.89550294706, 7389.35002875074,
8.72167728953903e-05, 0.615710674200898, 90613.5488833331,
0.0001007185458695, 0.329336214710806, 58860.1232768614,
0.000150880187013758, 0.00043394288165634, 589.188126109299,
13.3763683418565, 467886.366054549, 248.393646104426, 189074.428816151,
303206.257085955, 204105.426845067, 6538706.25322294, 5.70435826167575,
1223246.72689726, 98.8553275302565, 3.01666336637105e-10,
0.000421841985089921, 0.00158775289621832, 889227.11315509,
6.7862868179393, 11.0180525555011, 458.619294036059, 387926.980522322,
1367759.72124845, 127.106985922082, 21709768.964503, 249.583742407546,
494318.314688319, 6.84445784041661e-05, 0.0181257313976045,
2.85258646415982e-06, 0.0475591361913438, 0.000132886078114762,
530521.346229848, 0.220839329870672, 0.0796092084099546,
2.16536480257164e-05, 4.27202841139916e-07, 0.442895659664418,
0.241251203711246, 0.0679836008245645, 3.24194712523739,
12.3229213534339, 4.00236477738573, 3.65965017262511, 12.6636282554106,
484.9507381133, 33.498634194894, 9133015.34747621, 854.088489555501,
10.5754804155603, 251.152169076775, 66.5138965438843, 7.31026529212285,
12.5369593061199, 883.371244544336, 2188355.36818768, 6.82570529049995,
7.51815377289302, 2.10761714681621e-05, 0.222448185938361,
5.45130776104763, 327.118134199018, 60383.600655089, 1520.1609648366,
1481664.13388885, 34818.2575270259, 4060744.53269009, 0.000329878093050015,
3.50190559072499e-09, 0.365265550723291, 0.259371482496831,
5905.41200098933, 0.0213725080705001, 0.0019791757328749,
107933.355503732, 3.39775429580098e-12, 0.0330528161763248,
0.386107904764506, 1.99602333972179e-09, 0.000228093064728933,
5.19580674670871, 563.142766773764, 7356479.18831348, 315961.427593756,
261.353018397329, 44.4265659338378, 59015.8931254825, 33849.8516454425,
12037.241852193, 22886385.0415054, 9597313.08865641, 34.1796031059148,
3426361.9581678, 24812.0886306435, 16.2435772262661, 5335252.58800916,
15.4267958636366, 6.23003438750091, 6074284.64243093, 3625.64806417381,
6.8968999739466e-10, 6.28467534994529e-12, 8.67182915364106e-08,
0.134069855760784, 0.000162817646946365, 9.75584343397934e-07,
0.000117617735155071, 0.000113004292729558, 1421451.62202609,
9547535.94782668, 0.247463394835223, 1592.67069635606, 19898.2670219713,
454400.812967405, 0.00323291029572383, 9988.88694126955,
143.08805454549, 136687.142590513, 0.00242354231190896, 0.533822212459591,
0.00344026378149421, 484468.802936889, 2061965.14117208,
31.6052901005199, 0.0682189910835192, 14225946.1418798, 22656006.1427591,
8.68723197155215e-10, 0.00908810773970795, 1.32803636893067e-10,
3.2838750723857e-09, 0.0867179882627688, 0.0978011155313282,
0.349647525170572, 925.768941042692, 677721.086653615, 14806.4581572174,
3994895.41282463, 0.13512052318472, 0.0284052204027308, 14772.9294370976,
832124.265001897, 0.0506818824206226, 653153.199476183, 2412.38065661841,
2.58407754903887, 312946.331847625, 73.8532458134753, 0.0333143376007489,
0.148231771582762, 1077.07159652263, 303929.715210286, 248.050418381286,
1.32713216726536e-08), effect = c(-0.0163190960149514, 0.0239828026929405,
0.041119697076967, 0.0209172832321608, 0.0175906130298972,
0.0834614832205, 0.0454379665875109, 0.0386831358592724,
0.111290387209505, 0.0417666009957902, 0.0181594607740641,
0.0314977892715717, 0.0517389743010281, 0.0151006425358355,
0.102545733351982, 0.0141441804980859, 0.0616852086092345,
0.0224337526553543, 0.0769583981642733, 0.0152865895595402,
0.016476178103406, 0.0850464780157665, 0.259761646456551,
0.0758096357842209, 0.0879871401163749, 0.060577639895305,
-0.119584728001896, -0.0429780663246056, -0.0982076786158141,
-0.00853408648534678, -0.0398300310844788, -0.0130306262269616,
-0.0117783024812117, -0.0277653734748019, 0.0044146808370715,
-0.00858548331854399, -0.019241126838373, -0.00578884017923847,
0.00226822950309142, 0.0428684006392723, 0.0693526182788657,
0.0497740775628015, 0.516295204419154, 0.00640383203849196,
-0.00759227766834665, -0.0144951201040298, -0.0301134012710303,
-0.00177543890387751, 0.0844729727849364, 0.035083882999979,
-0.0452607458985876, -0.0112498347652378, 0.00432465949184261,
-0.0612693958135787, -0.161890494475421, -0.109885795143899,
-0.0367297743779374, -0.128480388877075, -0.0862830814238638,
-0.0767455417397432, 0.0879696206150576, 0.0341978476362303,
0.272396625438286, 0.0743011988057289, 0.0929358487669378,
0.0631542372947326, 0.0120936625302536, -0.0102721783333691,
0.0231718474438414, 0.031607051653089, -0.0305949081690051,
0.00330395034884568, 0.0373053051750641, -0.00385151004793588,
-0.0157412786940113, -0.0132287150372285, -0.0120506314486498,
-0.025044061802607, 0.00470372186431196, -0.0110743076334009,
-0.0386239261622541, 0.00837054636189714, 0.057136428153608,
0.0312680735700298, 0.0118706124991179, 0.128234673021361,
0.097326263380819, -0.00329520180332474, -0.071533496180363,
0.003789002995193, 0.0046909737332724, 0.0337729519954883,
0.0327930393051356, 0.0358750303006964, 0.104755410676543,
0.110139205213869, 0.0104813831771491, 0.038564461505739,
0.0174699813771993, 0.0667967652778607, 0.00452704565399326,
0.169065310950391, 0.0860051315499004, 0.0113816127441823,
0.054028218719922, 0.0828069492580835, 0.0566902515183901,
-0.0177524070698768, -0.0386299683572492, 0.0342109822338447,
0.00547588201891631, 0.0272576717302203, 0.0173982318869093,
0.00713163621583953, 0.0226481978020165, 0.0472525993298041,
0.0273573262972059, 0.025199426283408, 0.0744462951082736,
0.0153618732415838, -0.0200185553769581, 0.00188477410376072,
-0.00934913057903759, 0.00600234114364721, 0.0179457906957483,
0.00430101870279759, 0.174154071440548, 0.173673770147469,
0.0951350053551374, 0.0914389204777777, 0.135948060156079,
0.0277299892479554, 0.16803169351595, 0.0775422760432586,
0.0510707831236068, 0.0160622114639729, 0.0134597473251168,
0.0683573948296253, 0.0886360341968248, -0.00352396978265606,
-0.0826520910456777, 0.0044728665513685, -0.159909011556907,
0.0111881761993282, 0.131640942119481, 0.0316929149347357,
0.195644963948708, 0.067380251345993, 0.0259465675571933,
0.0455342345905025, 0.0971025906472932, 0.0201913183727302,
0.0349939607223961, 0.195861080450541, 0.0655886359004071,
0.146886606410868, 0.0892874895878602, 0.0796735966091277,
0.146650136993383, 0.120645912644686, -0.0752509299371392,
-0.00163803947679698, -0.0574957284606062, 0.00698636531233787,
0.0526791440398665, 0.018473633833928, -0.103073001462617,
-0.0948950576502364, -0.102463654498151, -0.0758695690445602,
-0.00213230587204453, -0.00568008091184311, -0.0182279768547043,
0.0899803113373462, 0.177084057032596, 0.0913169797144365,
0.0116673552151304, 0.0409140209831297, 0.0411965956436703,
0.0200981129338266), direction = c(-1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1,
1, 1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, -1,
-1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, -1, -1,
-1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1,
-1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1), plotval = c(-3.82997789810028,
8.69151503091653, -1.61473150877182, 1.40730794339407, 0.515406613246166,
3.66658658595408, 7.1986283729153, -2.00820919450685, -3.06909191624479,
0.590884338223415, -0.504368307179268, -3.38875638907046,
3.45985340742856, 1.11543772500508, -7.35941265115998, -3.99306654044701,
-3.56261255637583, -4.71509812185044, 0.711799756620258,
-3.01152625196879, -3.86860623929587, 4.05939998667948, 0.210623317244372,
-4.95719313989901, 3.99689055300952, 0.482360510453302, -4.76982116735253,
3.82136778630483, 3.36256743134917, -2.77025398593507, -1.1263382193229,
-5.67014039027971, -2.39514048241807, -5.27663279708947,
-5.48173815932764, -5.30985455208272, -6.81549182742701,
-0.756206793523825, -6.08751406236996, -1.99500007936184,
9.52047315064185, 3.37485019798332, 2.79921708639208, -5.94901269617059,
-0.831632210867015, -1.04210483960289, -2.66145232147358,
-5.58874998605655, -6.13600981017556, -2.104169420452, -7.33665520171442,
-2.39721629249818, -5.69400670154053, 4.16466094727295, 1.7417044600378,
5.5447611828998, 1.32276604199993, 3.87652051578144, -5.72470286300328,
0.655923579381524, 1.09903669444412, 4.66446892684065, 6.36936586766657,
0.35369857573924, 0.617530511087391, 1.16759583626924, -0.510805927414134,
-1.09071367676328, -0.602316667904953, -0.563439573001041,
-1.1025581531972, -2.68569762468031, -1.52502710035885, -6.96061418745396,
-2.93150286897326, -1.02430010532252, -2.3999369332979, -1.82291239059371,
-0.863933137944786, -1.09819221618344, -2.94614325802066,
-6.34011784869935, -0.83414753306763, -0.876111204330165,
4.67620827682094, 0.652771131566519, -0.736500701409775,
-2.51470462044497, -4.78091900630239, -3.18188957639156,
-6.17074976819802, -4.54180703314166, -6.60860566825564,
3.48164652465895, 8.45569556641358, 0.4373912854565, 0.586077775650145,
-3.77125020229067, 1.67014451025219, 2.70351564266601, -5.03315567890536,
11.4688080297742, 1.48079153170835, 0.413291306999458, 8.69983438476552,
3.64188791943946, -0.71565298951438, -2.75061851023672, -6.86667001067782,
-5.49963406741059, -2.41722752018175, -1.64764274462453,
-4.77096898396776, -4.52955676963248, -4.08052698645697,
-7.35957720025387, -6.98214966282064, -1.53376701537969,
-6.53483323957859, -4.3946633238282, -1.21068167752515, -6.72715498514415,
-1.18827573272272, -0.794490443811766, -6.78349513920237,
-3.55938564553853, 9.16134607255797, 11.2017171519269, 7.06188928688985,
0.872668857855968, 3.78829852600649, 6.01073517804921, 3.92952718761996,
3.94690505852234, -6.15273208341116, -6.97989130222874, 0.606489033485156,
-3.20212598952403, -4.29881525452187, -5.65743909969887,
2.49040634568619, -3.99951709761578, -2.15560337896229, -5.13572766479016,
2.61554939365603, 0.272603359083682, 2.4634082567147, -5.68526581616783,
-6.31428131898601, -1.49975978101014, 1.1660947078266, -7.15308116016609,
-7.35518335376065, 9.06111858154676, 2.04152653303234, 9.87679003143666,
8.48361337299155, 1.06189080571935, 1.00965619156887, 0.456369541379321,
-2.96650260638842, -5.83105099851421, -4.17045118365708,
-6.60150541388087, 0.869278681851812, 1.54660183658146, -4.16946662342416,
-5.92018818635659, 1.29514726297355, -5.81501505858544, -3.38244583747527,
-0.41230554284251, -5.49546986540307, -1.86836958709421,
1.47736881752115, 0.829058700916523, -3.03224457325269, -5.48277316313018,
-2.39453996388795, 7.87708582419226), type = c("1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2")), .Names = c("Measurement",
"category", "variable", "Pval", "effect", "direction", "plotval",
"type"), row.names = c(23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 65L,
66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L,
79L, 80L, 81L, 82L, 83L, 84L, 107L, 108L, 109L, 110L, 111L, 112L,
113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L,
124L, 125L, 126L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L,
157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L,
168L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 200L,
201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L, 209L, 210L, 233L,
234L, 235L, 236L, 237L, 238L, 239L, 240L, 241L, 242L, 243L, 244L,
245L, 246L, 247L, 248L, 249L, 250L, 251L, 252L, 275L, 276L, 277L,
278L, 279L, 280L, 281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L,
289L, 290L, 291L, 292L, 293L, 294L, 317L, 318L, 319L, 320L, 321L,
322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L, 330L, 331L, 332L,
333L, 334L, 335L, 336L, 359L, 360L, 361L, 362L, 363L, 364L, 365L,
366L, 367L, 368L, 369L, 370L, 371L, 372L, 373L, 374L, 375L, 376L,
377L, 378L), class = "data.frame")
Thank you ever so much for your help

facet_grid weird rearrangement of values [duplicate]

This question already has an answer here:
Issue when passing variable with dollar sign notation ($) to aes() in combination with facet_grid() or facet_wrap()
(1 answer)
Closed 6 years ago.
I have this plot that I've generated using ggplot2 in R.
The code that produces this plot is:
plot <- ggplot(mockdata, aes(variable, Measurement)) +
geom_tile(aes(fill = mockdata$plotval), colour = "dark red") + facet_grid(~type, scales='free', space='free') + scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() + theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28, face = "italic")) +
labs(title="", x="", y="", fill="") + theme(strip.text.x=element_blank(),strip.text.y=element_text(size=20, angle=0))
As you can see, I've added a horizontal space based on capitalization of my x-axis variables. Now however, I would like to also add vertical spacing based on another factor variable. I simply change the code to be:
ggplot(mockdata, aes(variable, Measurement)) +
geom_tile(aes(fill = mockdata$plotval), colour = "dark red") + facet_grid(category~type, scales='free', space='free') + scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() + theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28, face = "italic")) +
labs(title="", x="", y="", fill="") + theme(strip.text.x=element_blank(),strip.text.y=element_text(size=20, angle=0))
The above gives exactly the look I'm going after but notice that the values are all messed up! For example 20:b and 20:c are blue in the first plot but red in the second plot. The first plot contains the correct values.
I've been going crazy going over my factor labelling for hours but I can't seem to find the problem. I would like a solution as how I can add the vertical space to my first plot, using facet_grid or no, it doesn't matter, and special bonus points will be given to anyone who can explain what went wrong in my second plot.
dput(mockdata)
structure(list(Measurement = structure(c(20L, 19L, 18L, 17L,
16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L,
2L, 1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L,
9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 20L, 19L, 18L, 17L, 16L,
15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L,
1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L,
8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 20L, 19L, 18L, 17L, 16L, 15L,
14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L,
20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L,
7L, 6L, 5L, 4L, 3L, 2L, 1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L,
13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 20L,
19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L,
6L, 5L, 4L, 3L, 2L, 1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L,
12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), .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",
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
"36", "37", "38", "39", "40", "41", "42"), class = "factor"),
category = structure(c(3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 3L, 4L, 4L, 4L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 8L), .Label = c("x1", "x2", "x3", "x4",
"x5", "x6", "x7", "x8", "x9"), 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 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, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 8L, 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, 9L), .Label = c("A",
"B", "C", "a", "b", "c", "d", "e", "f"), class = "factor"),
Pval = c(42.7006323958918, 0.0125450399252769, 5675.14711907005,
0.0931416002606762, 0.00232094737764211, 0.000216076621032813,
0.00396622838836271, 6.52388877927194, 11.5722289932645,
0.000334263699328083, 27.0431686878052, 327952.740805895,
0.0297787229949035, 0.585782163292434, 36.9115682159781,
4165.1649504391, 52.9995249740471, 6186.78975112519, 7.85106982537471e-05,
294938.11483059, 9197376.02201148, 1.56761799701167e-05,
0.687511811844484, 2951357.69281822, 0.281822039246925, 0.550068489025187,
12173.6665259036, 0.000654862439823334, 0.00448573254652099,
7319.35702087325, 315.359225529075, 5848068.52630072, 13904253.2299896,
6.66288479812568, 69897.571357397, 10503715.2130853, 46675.3343032854,
80.0136913688775, 2.74356992078766, 348201.375999632, 1.07521153836152e-06,
1.98853436745688e-05, 0.400197989005822, 3511901.4371568,
164.269778446034, 3.24558993609286, 65394.4866129748, 55.4132232221762,
4.83069482078633, 38.6041603684776, 37.9912973942591, 104522.510922666,
6345.6890512069, 0.0193603214641399, 9.18538323079328e-09,
0.000103476856048387, 0.000555384674445469, 3.22868352890832e-05,
113007.480780211, 0.000160838575168945, 0.00217855024162056,
8.64496348775897e-06, 7.06449122162668e-07, 0.619370137929941,
0.270921839221627, 0.011388566962421, 937781.722037049, 652.688753412217,
47171.0329654517, 8.70296668276766, 8276277.85721442, 28353.82586081,
175700.845731391, 1551633.67731154, 689.167798328347, 109.943340419374,
11.3781857520997, 1617599.77065294, 37.1569915088865, 4203.89382661281,
117832.471263455, 1251784.09345768, 535.751672862479, 165398.434479864,
2.55163228104252e-09, 9.8013680674497e-10, 129.319151038722,
556.223625027009, 869.236740102022, 3092.82667967769, 4257.32149187776,
772478.486004829, 15.7002509424478, 0.000219697884882267,
0.000427755809814034, 0.192409289349257, 0.280314538898884,
372.959096370547, 0.00465127237377936, 2.10935354960679e-05,
232459.76019099, 5.77106623224624e-08, 0.0483934897590051,
0.0146558784756261, 1.47248474221649e-08, 0.000222461186585379,
852431.075531575, 528.683263593354, 8298.0240769841, 14.7493364766307,
6.2911608490762, 4536740.5530938, 106.089248136605, 99.5504144351031,
114043.212772726, 253658.532323196, 140833.560642241, 3335.43395071637,
36.6239752632138, 198.244201535186, 14553.9246345935, 2.47559453576517,
1469.76758256116, 4809.5093947632, 2111515.77618631, 3211.80355220151,
0.0046904155982774, 8.1507521777127e-07, 0.000265158173925916,
0.0331951291090042, 9.47798135335384e-10, 0.00629215449841144,
8.27956294929558e-05, 0.000192338169994771, 554785.490344723,
793.314954892235, 0.106735781318401, 14029944.8981012, 527616.898199668,
478322.816562339, 0.00139976568423574, 520.741670562665,
1289142.03044888, 5.80426527938221, 0.00134726649987191,
0.370746844567892, 0.000167132571538772, 3182343.07410991,
82449.5457770316, 2687893.44907279, 0.178274475177317, 50.1455573308552,
414876.812590266, 2.60936167209485e-09, 2.32090832789666e-08,
8.42629935014704e-10, 0.000255374171026513, 1.10761356981753e-06,
5.37793701940977e-05, 0.557162933357393, 829.829809862024,
6184566.2549798, 2782.96376300611, 898.722084215034, 0.0049389188839104,
0.736095078701914, 73.501019996704, 280617.802045198, 0.00108189739567757,
139765.97782083, 18925.6574878606, 39945.1297996732, 11654.4707728977,
5.25244181877881, 0.000951333348397858, 0.701154190998343,
3783700.00624337, 1048363.72082207, 627347.0256674, 0.00056428282172991
), effect = c(-0.00504202541860286, 0.0572728382379748, 0.19355610290193,
0.0908419036193052, 0.0753460915818811, 0.0304533379241126,
0.338999529965851, 0.00450925741610117, 0.0142515711297281,
0.0236642558989115, 0.112084463317692, 0.0355492039503297,
0.046179641636787, 0.0833907727035694, 0.0119081147311255,
0.0409084851564839, 0.00974046553876251, 0.00623992766439915,
0.0828237061289605, 0.0460014467529953, 0.0220280811514705,
0.072554252236383, 0.181702209021198, 0.0496502124621067,
0.103706638295506, 0.0514006496664137, -0.09126288844347,
-0.0579011936709518, -0.0313363166032359, -0.0137049341579666,
-0.0538731912627118, -0.0871985756458715, -0.00540516899377108,
-0.0335420569797512, 0.00837464914354496, -0.00433782674791291,
-0.0186401470510755, -0.00957092739050277, 0.0233458885691129,
0.0363082683534594, 0.0949001163895009, 0.0980889928637072,
0.272094653271255, 0.0116498581551015, -0.0239706620213576,
-0.00948922481425107, -0.0463841533571947, -0.000196251500025392,
0.0332074957195669, 0.0306732835672796, -0.078594298822456,
-0.0013592321904609, 0.00823043498271145, -0.0792124313370092,
-0.114084449997195, -0.0535820005729329, -0.0193882875198498,
-0.145900537042366, -0.042750018055737, -0.0353791699868627,
0.0917785141337663, 0.198537943334598, 0.21948014556882,
0.0524090722153662, 0.014529019941343, 0.0986667789371684,
0.013579010005435, -0.0140683606411563, 0.0151127260881476,
0.00348067883725744, -0.0239177792519331, 0.00323518849038519,
0.0248013017682591, -0.0205422166822478, -0.00862063024472445,
-0.0253349156677723, -0.00206032852686476, -0.034685899461899,
0.0291777396060294, -0.00790295204452705, -0.0172764198936056,
0.093523378677899, 0.0966298831466818, 0.0519375468929764,
0.184676539092511, 0.0738557771574706, 0.0610706031136215,
-0.00852528463886119, -0.035804039040301, 0.0096352117901668,
0.0521074840389658, 0.0364758835127577, 0.0267540711492766,
0.134747323978599, 0.109492562370398, 0.0133001291007968,
0.0732780309339287, 0.0303602020342369, 0.0149482630094048,
0.0273990078144707, 0.00299717221567407, 0.100866435181815,
0.114870729540125, 0.0567497200005222, 0.0912124964999035,
0.127694568835571, 0.0999233369545313, -0.0228848084434401,
-0.0199126446791925, 0.0455760222896934, 0.00550931423553266,
0.0169695986729115, 0.00547424764931202, 0.0360297252475284,
0.00940059675048105, 0.00779317291104235, 0.0292808745009825,
0.0146692995987833, 0.107736041982938, 0.0379549009324052,
-0.0245307848578086, 0.00373489473816007, -0.0277668840806,
0.00541292380436789, 0.0109495216225972, 0.0154749859461561,
0.517201286005927, 0.213654141088575, 0.105933886134205,
0.083967352311383, 0.0390362727543106, 0.0541759578921366,
0.0799491572647821, 0.0713475409375969, 0.0830535251532681,
0.0634176325142384, 0.0340343110430986, 0.0741195089506451,
0.0422227419333998, -0.00201868398387451, -0.102142939489265,
0.00990816502366215, -0.0723986947049853, 0.00438771610893309,
0.0614558366658632, 0.112141419906449, 0.206453004881553,
0.0458149211724056, 0.0370232303908095, 0.0104171190238791,
0.174352387892548, 0.0173287004770711, 0.0570322589545976,
0.0919422210688935, 0.206073359134817, 0.144707153996313,
0.0866876754204277, 0.139499693175196, 0.0501563760935562,
0.113150966215879, -0.0615705734418705, -0.0016999262242578,
-0.161924930685107, 0.0055290101159364, 0.106581585830427,
0.0128140174669214, -0.0703636721519287, -0.018257094814349,
-0.100875941503327, -0.0160441944317892, -0.00386698165039997,
-0.00342265706742182, -0.0322326223948505, 0.0280294325939845,
0.0984244986939011, 0.0512777173537761, 0.0352523410798283,
0.13292883383322, 0.0807717506474815, 0.0508596294487826),
direction = c(-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1,
-1, -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1,
1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
1, 1, 1, -1, 1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, -1,
1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1,
-1, 1, 1, 1, 1, 1, 1, 1), plotval = c(-1.63043430696911,
1.90152795218119, -3.7539771243977, 1.03085630474974, 2.63433470610105,
3.6653922201862, 2.40162228131836, -0.814506548481021, -1.06341701895514,
3.47591078475207, -1.43205757717356, -5.51581126479104, 1.52609393009486,
0.23226385645368, -1.56716249691816, -3.61963220517785, -1.72427197710882,
-3.79146535741686, 4.10507116012102, -5.46973090002207, -6.96366394237768,
4.80475975925492, 0.162719836007062, -6.4700218474543, 0.550025046893605,
0.259583233135038, -4.08542140092089, 3.18384991817596, 2.34816662398371,
-3.86447293152949, -2.49880554038012, -6.7670124529409, -7.14314766872669,
-0.823662304342601, -4.84446208612616, -7.02134293822782,
-4.66908743718463, -1.90316430670692, -0.438316032811778,
-5.54183048300149, 5.96850608374351, 4.70146689878517, 0.397725098023318,
-6.54554231876607, -2.21555767139982, -0.511293648040455,
-4.81554113464696, -1.74361341251133, -0.684009601793095,
-1.58663411108907, -1.57968412486682, -5.01920983414714,
-3.80247878716975, 1.71308743582189, 8.03690271942929, 3.98515677498429,
3.25540610855334, 4.49097452184084, -5.05310719351909, 3.79360978286461,
2.66183241993435, 5.06323683664087, 6.15091911019143, 0.208049737379102,
0.567155984737416, 1.94353092029908, -5.97210176380931, -2.81470612936045,
-4.67367538666088, -0.939667320932661, -6.91783506268839,
-4.45261166774866, -5.24477385196537, -6.19078919714086,
-2.83832497664739, -2.04116892804232, -1.05607301946984,
-6.20887107667165, -1.57004054321228, -3.62365173896932,
-5.07126498609874, -6.09752942863009, -2.72896353578257,
-5.21853139457582, 8.59318191216903, 9.00871330158534, -2.11166284486924,
-2.74524943113182, -2.93913807439764, -3.4903555830589, -3.62913644718831,
-5.88788639290012, -1.19590659394104, 3.65817412418273, 3.368804083124,
0.715773964440957, 0.552354376375251, -2.57166120394098,
2.33242822790069, 4.67585062188463, -5.36634778544318, 7.23874394143661,
1.31521305884959, 1.83398814476682, 7.83194919664647, 3.65274575062554,
-5.93065927361594, -2.72319556226042, -3.91897469061421,
-1.1687724832959, -0.79873078913878, -6.65674394356368, -2.02567137153985,
-1.99804307236447, -5.05706944355335, -5.40424947528169,
-5.14870615952883, -3.52315234504154, -1.56376548199677,
-2.29720049345194, -4.16298012165971, -0.393679515429061,
-3.16724866427777, -3.68210077741043, -6.32459433060349,
-3.50674897408984, 2.32878867454675, 6.08880231131442, 3.57649498051523,
1.47892563787053, 9.02328415005142, 2.20120062219246, 4.0819925875773,
3.71593452037349, -5.74412509417193, -2.89944564130995, 0.971689966478527,
-7.1470559653621, -5.72231869642199, -5.67972109772494, 2.8539446575791,
-2.71662233198973, -6.1103007681231, -0.763747253307228,
2.87054648892738, 0.430922536570879, 3.77693890461838, -6.50274699717143,
-4.91618826741447, -6.42941204883235, 0.748910833398874,
-1.70023246253767, -5.61791916284656, 8.58346572108797, 7.63434201313085,
9.07436311632148, 3.59282303012629, 5.9556117321782, 4.26938428807087,
0.254017783801932, -2.91898903187407, -6.79130924645919,
-3.44450755141886, -2.95362541370008, 2.30636810656965, 0.133066085813731,
-1.86629336596673, -5.44811521866938, 2.96581392463054, -5.14540146724019,
-4.27705097606589, -4.60146383671481, -4.06649255708194,
-0.720361250431011, 3.02166727906391, 0.154186465719317,
-6.57791669570072, -6.02051198354564, -5.797507843288, 3.24850317060798
), type = c("1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
"2", "2", "2")), .Names = c("Measurement", "category", "variable",
"Pval", "effect", "direction", "plotval", "type"), row.names = c(23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L,
37L, 38L, 39L, 40L, 41L, 42L, 65L, 66L, 67L, 68L, 69L, 70L, 71L,
72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L,
107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L,
118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 149L, 150L,
151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L,
162L, 163L, 164L, 165L, 166L, 167L, 168L, 191L, 192L, 193L, 194L,
195L, 196L, 197L, 198L, 199L, 200L, 201L, 202L, 203L, 204L, 205L,
206L, 207L, 208L, 209L, 210L, 233L, 234L, 235L, 236L, 237L, 238L,
239L, 240L, 241L, 242L, 243L, 244L, 245L, 246L, 247L, 248L, 249L,
250L, 251L, 252L, 275L, 276L, 277L, 278L, 279L, 280L, 281L, 282L,
283L, 284L, 285L, 286L, 287L, 288L, 289L, 290L, 291L, 292L, 293L,
294L, 317L, 318L, 319L, 320L, 321L, 322L, 323L, 324L, 325L, 326L,
327L, 328L, 329L, 330L, 331L, 332L, 333L, 334L, 335L, 336L, 359L,
360L, 361L, 362L, 363L, 364L, 365L, 366L, 367L, 368L, 369L, 370L,
371L, 372L, 373L, 374L, 375L, 376L, 377L, 378L), class = "data.frame")
Thank you very much for your help.
Do it like here (in aes fill should be the column NAME not the VECTOR):
plot <- ggplot(mockdata, aes(variable, Measurement, fill = plotval)) +
geom_tile(colour = "dark red") +
facet_grid(category~type, scales='free', space='free') +
scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") +
theme_minimal() +
theme(axis.text.x=element_text(size=28, angle=90),
axis.text.y=element_text(size=28, face = "italic"),
strip.text.x=element_blank(),
strip.text.y=element_text(size=20, angle=0)) +
labs(title="", x="", y="", fill="")

ddply function based on two variables

How can I modify the below code to calculate the average of time per ID in each level of Type.
df <- structure(list(ID = structure(c(1L, 2L, 2L, 3L, 3L, 4L, 4L, 4L,
4L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 5L, 6L), .Label = c("R1", "R2",
"R3", "R4", "R5", "R6"), class = "factor"), cost = c(123L, 12L,
14L, 15L, 16L, 17L, 18L, 9L, 6L, 6L, 7L, 8L, 1L, 111L, 121L,
141L, 181L, 1611L), Time = c(123L, -12L, NA, -15L, NA, 17L, 18L,
-9L, 88L, 666L, 7L, 78L, 188L, 111L, 121L, 141L, 189L, 161L),
number = c(14L, 12L, 15L, 17L, NA, 17L, 22L, 95L, NA, 67L,
7L, 8L, 7L, 118L, NA, 140L, 180L, 1611L),type = c("A", "A", "B", "B", "B", "C", "C",
"M", "A", "M", "D", "D", "C", "A", "D", "B", "B", "M")), .Names = c("ID",
"cost", "Time", "number","type"), row.names = c(NA, -18L), class = "data.frame")
library(dplyr)
df %>%
group_by(id) %>%
summarise(N.Time = sum(!is.na(Time)),
Time_Average = round(mean(Time[Time >=0 & !is.na(Time)]), 2))

Axis Color of Date Histogram in R

I have successfullly created a histogram using a date field.
hist(df.sat$created_at, breaks="hours", freq=T, xlab="Time",
main="Sat Volume")
My issue is that when I attempt to fill in the bars using col="red" both the bars and both the x/y axes change to red, when I only want the bars. What is the best way way only fill in the bars?
Here are some data:
> dput(df.sat$created_at[sample(c(1:9000), 50)])
structure(list(sec = c(41, 3, 13, 11, 49, 55, 19, 21, 6, 15,
54, 45, 45, 39, 50, 27, 35, 25, 22, 35, 42, 31, 45, 29, 1, 3,
8, 47, 38, 2, 13, 29, 34, 42, 15, 19, 3, 39, 41, 12, 34, 50,
15, 27, 0, 29, 47, 26, 21, 5), min = c(46L, 38L, 4L, 35L, 26L,
56L, 9L, 52L, 51L, 15L, 49L, 3L, 41L, 59L, 30L, 30L, 30L, 53L,
25L, 51L, 23L, 38L, 30L, 3L, 43L, 33L, 36L, 52L, 0L, 21L, 27L,
22L, 51L, 31L, 0L, 37L, 3L, 2L, 12L, 3L, 45L, 13L, 59L, 10L,
11L, 7L, 41L, 21L, 5L, 20L), hour = c(14L, 16L, 18L, 15L, 15L,
16L, 16L, 18L, 18L, 13L, 18L, 16L, 14L, 13L, 16L, 15L, 18L, 17L,
18L, 18L, 16L, 17L, 17L, 19L, 15L, 18L, 17L, 18L, 19L, 17L, 16L,
17L, 18L, 20L, 18L, 15L, 14L, 14L, 18L, 18L, 19L, 19L, 16L, 15L,
17L, 17L, 15L, 17L, 17L, 17L), mday = c(9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), mon = c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), year = c(111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L,
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L,
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L,
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L,
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L), wday = c(6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L), yday = c(98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L,
98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L,
98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L,
98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L,
98L), isdst = 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)), .Names = c("sec", "min", "hour", "mday",
"mon", "year", "wday", "yday", "isdst"), class = c("POSIXlt",
"POSIXt"), tzone = c("America/New_York", "EST", "EDT"))
You'll have to get around it a bit by plotting the histogram first and the axes later :
hist(Data, breaks="hours", freq=T, xlab="Time", col="red",
main="Sat Volume",axes=F)
Axis(Data,col="black",side=1)
axis(2,col="black")
Reason to use the generic Axis(), is that it takes into account that your variable is a TimeDate class. The default axis() doesnt.
EDIT :
FYI, this behaviour is only to be seen with histograms where DateTime classes are used on the X axis. The default hist() function doesn't change the color of the axis when using a fill color for the bars.
Plot the histogram without axes and then add them in later:
hist(dat, breaks="hours", freq=TRUE, col = "red", axes = FALSE)
axis.POSIXct(side = 1, dat)
axis(2)

Resources