Count aesthetics differently calculated for R's Date and POSIXct
I noticed something very strange while working with Date
and POSIXct
objects. See the following code:
library(tidyverse)
library(Rmisc)
test <- structure(list(
date = structure(c(
16863, 16866, 16862, 16743,
16741, 16819, 16820, 16969, 16896, 16636, 16855, 16715, 16842,
16899, 16859, 16860, 16827, 16823, 16912, 16878, 16848, 16839,
16901, 16833, 16896, 16841, 16735, 16800, 16781, 16903
), class = "Date"),
group = structure(c(
1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 2L, 2L
), .Label = c("0", "1"), class = "factor")
), row.names = c(
NA,
-30L
), class = c("tbl_df", "tbl", "data.frame"))
test$posix <- as.POSIXct(test$date)
p1 <- ggplot(
test,
aes(x = date, group = group, colour = group, fill = group)
) +
stat_density(aes(y = ..count..), alpha = 0.4)
p2 <- ggplot(
test,
aes(x = posix, group = group, colour = group, fill = group)
) +
stat_density(aes(y = ..count..), alpha = 0.4)
multiplot(p1, p2)
This results in the following plot: see y-axis. (The count < 1 because the sample size is so small.)
Why would the scales differ on those two graphs when geom_density
with ..count..
is called? Same when ..density..
is called. The only difference between the two plots is calling upon x
aesthetics either with Date
or with POSIXct
. I'm quite puzzled.
r ggplot2 posixct
add a comment |
I noticed something very strange while working with Date
and POSIXct
objects. See the following code:
library(tidyverse)
library(Rmisc)
test <- structure(list(
date = structure(c(
16863, 16866, 16862, 16743,
16741, 16819, 16820, 16969, 16896, 16636, 16855, 16715, 16842,
16899, 16859, 16860, 16827, 16823, 16912, 16878, 16848, 16839,
16901, 16833, 16896, 16841, 16735, 16800, 16781, 16903
), class = "Date"),
group = structure(c(
1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 2L, 2L
), .Label = c("0", "1"), class = "factor")
), row.names = c(
NA,
-30L
), class = c("tbl_df", "tbl", "data.frame"))
test$posix <- as.POSIXct(test$date)
p1 <- ggplot(
test,
aes(x = date, group = group, colour = group, fill = group)
) +
stat_density(aes(y = ..count..), alpha = 0.4)
p2 <- ggplot(
test,
aes(x = posix, group = group, colour = group, fill = group)
) +
stat_density(aes(y = ..count..), alpha = 0.4)
multiplot(p1, p2)
This results in the following plot: see y-axis. (The count < 1 because the sample size is so small.)
Why would the scales differ on those two graphs when geom_density
with ..count..
is called? Same when ..density..
is called. The only difference between the two plots is calling upon x
aesthetics either with Date
or with POSIXct
. I'm quite puzzled.
r ggplot2 posixct
2
You should look at output (and thenstr()
s ofdensity(as.numeric(x$date)) ; density(as.numeric(as.POSIXct(x$date)))
then?density
and also consider that the area under the curve is 1 but both width and height are important when considering said area.
– hrbrmstr
Nov 16 '18 at 11:03
add a comment |
I noticed something very strange while working with Date
and POSIXct
objects. See the following code:
library(tidyverse)
library(Rmisc)
test <- structure(list(
date = structure(c(
16863, 16866, 16862, 16743,
16741, 16819, 16820, 16969, 16896, 16636, 16855, 16715, 16842,
16899, 16859, 16860, 16827, 16823, 16912, 16878, 16848, 16839,
16901, 16833, 16896, 16841, 16735, 16800, 16781, 16903
), class = "Date"),
group = structure(c(
1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 2L, 2L
), .Label = c("0", "1"), class = "factor")
), row.names = c(
NA,
-30L
), class = c("tbl_df", "tbl", "data.frame"))
test$posix <- as.POSIXct(test$date)
p1 <- ggplot(
test,
aes(x = date, group = group, colour = group, fill = group)
) +
stat_density(aes(y = ..count..), alpha = 0.4)
p2 <- ggplot(
test,
aes(x = posix, group = group, colour = group, fill = group)
) +
stat_density(aes(y = ..count..), alpha = 0.4)
multiplot(p1, p2)
This results in the following plot: see y-axis. (The count < 1 because the sample size is so small.)
Why would the scales differ on those two graphs when geom_density
with ..count..
is called? Same when ..density..
is called. The only difference between the two plots is calling upon x
aesthetics either with Date
or with POSIXct
. I'm quite puzzled.
r ggplot2 posixct
I noticed something very strange while working with Date
and POSIXct
objects. See the following code:
library(tidyverse)
library(Rmisc)
test <- structure(list(
date = structure(c(
16863, 16866, 16862, 16743,
16741, 16819, 16820, 16969, 16896, 16636, 16855, 16715, 16842,
16899, 16859, 16860, 16827, 16823, 16912, 16878, 16848, 16839,
16901, 16833, 16896, 16841, 16735, 16800, 16781, 16903
), class = "Date"),
group = structure(c(
1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 2L, 2L
), .Label = c("0", "1"), class = "factor")
), row.names = c(
NA,
-30L
), class = c("tbl_df", "tbl", "data.frame"))
test$posix <- as.POSIXct(test$date)
p1 <- ggplot(
test,
aes(x = date, group = group, colour = group, fill = group)
) +
stat_density(aes(y = ..count..), alpha = 0.4)
p2 <- ggplot(
test,
aes(x = posix, group = group, colour = group, fill = group)
) +
stat_density(aes(y = ..count..), alpha = 0.4)
multiplot(p1, p2)
This results in the following plot: see y-axis. (The count < 1 because the sample size is so small.)
Why would the scales differ on those two graphs when geom_density
with ..count..
is called? Same when ..density..
is called. The only difference between the two plots is calling upon x
aesthetics either with Date
or with POSIXct
. I'm quite puzzled.
r ggplot2 posixct
r ggplot2 posixct
edited Nov 16 '18 at 15:54
Henrik
42.4k994110
42.4k994110
asked Nov 16 '18 at 9:22
KimKim
1,5001228
1,5001228
2
You should look at output (and thenstr()
s ofdensity(as.numeric(x$date)) ; density(as.numeric(as.POSIXct(x$date)))
then?density
and also consider that the area under the curve is 1 but both width and height are important when considering said area.
– hrbrmstr
Nov 16 '18 at 11:03
add a comment |
2
You should look at output (and thenstr()
s ofdensity(as.numeric(x$date)) ; density(as.numeric(as.POSIXct(x$date)))
then?density
and also consider that the area under the curve is 1 but both width and height are important when considering said area.
– hrbrmstr
Nov 16 '18 at 11:03
2
2
You should look at output (and then
str()
s of density(as.numeric(x$date)) ; density(as.numeric(as.POSIXct(x$date)))
then ?density
and also consider that the area under the curve is 1 but both width and height are important when considering said area.– hrbrmstr
Nov 16 '18 at 11:03
You should look at output (and then
str()
s of density(as.numeric(x$date)) ; density(as.numeric(as.POSIXct(x$date)))
then ?density
and also consider that the area under the curve is 1 but both width and height are important when considering said area.– hrbrmstr
Nov 16 '18 at 11:03
add a comment |
1 Answer
1
active
oldest
votes
Ah, it was a pretty silly question. Silly me.
> as.numeric(test$posix)
[1] 1456963200 1457222400 1456876800 1446595200 1446422400
[6] 1453161600 1453248000 1466121600 1459814400 1437350400
[11] 1456272000 1444176000 1455148800 1460073600 1456617600
[16] 1456704000 1453852800 1453507200 1461196800 1458259200
[21] 1455667200 1454889600 1460246400 1454371200 1459814400
[26] 1455062400 1445904000 1451520000 1449878400 1460419200
while
> as.numeric(test$date)
[1] 16863 16866 16862 16743 16741 16819 16820 16969 16896 16636
[11] 16855 16715 16842 16899 16859 16860 16827 16823 16912 16878
[21] 16848 16839 16901 16833 16896 16841 16735 16800 16781 16903
So as @hrbrmstr said, it is just a matter of unit.
I'll leave this post to serve as a warning, to show that conversion to POSIXct
class can have unexpected consequences. I was (unnecessarily) trying to use scale_x_datetime
at the time.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334833%2fcount-aesthetics-differently-calculated-for-rs-date-and-posixct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Ah, it was a pretty silly question. Silly me.
> as.numeric(test$posix)
[1] 1456963200 1457222400 1456876800 1446595200 1446422400
[6] 1453161600 1453248000 1466121600 1459814400 1437350400
[11] 1456272000 1444176000 1455148800 1460073600 1456617600
[16] 1456704000 1453852800 1453507200 1461196800 1458259200
[21] 1455667200 1454889600 1460246400 1454371200 1459814400
[26] 1455062400 1445904000 1451520000 1449878400 1460419200
while
> as.numeric(test$date)
[1] 16863 16866 16862 16743 16741 16819 16820 16969 16896 16636
[11] 16855 16715 16842 16899 16859 16860 16827 16823 16912 16878
[21] 16848 16839 16901 16833 16896 16841 16735 16800 16781 16903
So as @hrbrmstr said, it is just a matter of unit.
I'll leave this post to serve as a warning, to show that conversion to POSIXct
class can have unexpected consequences. I was (unnecessarily) trying to use scale_x_datetime
at the time.
add a comment |
Ah, it was a pretty silly question. Silly me.
> as.numeric(test$posix)
[1] 1456963200 1457222400 1456876800 1446595200 1446422400
[6] 1453161600 1453248000 1466121600 1459814400 1437350400
[11] 1456272000 1444176000 1455148800 1460073600 1456617600
[16] 1456704000 1453852800 1453507200 1461196800 1458259200
[21] 1455667200 1454889600 1460246400 1454371200 1459814400
[26] 1455062400 1445904000 1451520000 1449878400 1460419200
while
> as.numeric(test$date)
[1] 16863 16866 16862 16743 16741 16819 16820 16969 16896 16636
[11] 16855 16715 16842 16899 16859 16860 16827 16823 16912 16878
[21] 16848 16839 16901 16833 16896 16841 16735 16800 16781 16903
So as @hrbrmstr said, it is just a matter of unit.
I'll leave this post to serve as a warning, to show that conversion to POSIXct
class can have unexpected consequences. I was (unnecessarily) trying to use scale_x_datetime
at the time.
add a comment |
Ah, it was a pretty silly question. Silly me.
> as.numeric(test$posix)
[1] 1456963200 1457222400 1456876800 1446595200 1446422400
[6] 1453161600 1453248000 1466121600 1459814400 1437350400
[11] 1456272000 1444176000 1455148800 1460073600 1456617600
[16] 1456704000 1453852800 1453507200 1461196800 1458259200
[21] 1455667200 1454889600 1460246400 1454371200 1459814400
[26] 1455062400 1445904000 1451520000 1449878400 1460419200
while
> as.numeric(test$date)
[1] 16863 16866 16862 16743 16741 16819 16820 16969 16896 16636
[11] 16855 16715 16842 16899 16859 16860 16827 16823 16912 16878
[21] 16848 16839 16901 16833 16896 16841 16735 16800 16781 16903
So as @hrbrmstr said, it is just a matter of unit.
I'll leave this post to serve as a warning, to show that conversion to POSIXct
class can have unexpected consequences. I was (unnecessarily) trying to use scale_x_datetime
at the time.
Ah, it was a pretty silly question. Silly me.
> as.numeric(test$posix)
[1] 1456963200 1457222400 1456876800 1446595200 1446422400
[6] 1453161600 1453248000 1466121600 1459814400 1437350400
[11] 1456272000 1444176000 1455148800 1460073600 1456617600
[16] 1456704000 1453852800 1453507200 1461196800 1458259200
[21] 1455667200 1454889600 1460246400 1454371200 1459814400
[26] 1455062400 1445904000 1451520000 1449878400 1460419200
while
> as.numeric(test$date)
[1] 16863 16866 16862 16743 16741 16819 16820 16969 16896 16636
[11] 16855 16715 16842 16899 16859 16860 16827 16823 16912 16878
[21] 16848 16839 16901 16833 16896 16841 16735 16800 16781 16903
So as @hrbrmstr said, it is just a matter of unit.
I'll leave this post to serve as a warning, to show that conversion to POSIXct
class can have unexpected consequences. I was (unnecessarily) trying to use scale_x_datetime
at the time.
answered Nov 16 '18 at 21:29
KimKim
1,5001228
1,5001228
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334833%2fcount-aesthetics-differently-calculated-for-rs-date-and-posixct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You should look at output (and then
str()
s ofdensity(as.numeric(x$date)) ; density(as.numeric(as.POSIXct(x$date)))
then?density
and also consider that the area under the curve is 1 but both width and height are important when considering said area.– hrbrmstr
Nov 16 '18 at 11:03