You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using geom_dotplot and specifying a fill aesthetic, we get a different output depending on whether fill is specified inside the function aes or as a separate vector. In the example below, I would expect all points below x~12 to be the same colour in both graphs.
library(tidyverse)
library(patchwork)
# Use 'dose' inside aesgg1<- ggplot(ToothGrowth, aes(x=len)) +
geom_dotplot(aes(fill=factor(dose)),
binwidth=.5) +
guides(fill="none") # Remove legend# Create separate vector for filldot_cols<- as.integer(factor(ToothGrowth$dose))
gg2<- ggplot(ToothGrowth, aes(x=len)) +
geom_dotplot(fill=dot_cols,
binwidth=.5)
gg1+gg2
As we can see, on the right, there are about 5 points between x=10 and x=12 that should be black but are actually red.
As far as I can tell, this could be a consequence of the data being sorted with respect to x, which means that the rows of the dataset no longer match the elements of the vector dot_cols. As a proof of concept, I recreated the second graph but I sorted the data first (I also used the group aesthetic to try to get as close as possible to the original graph):
library(tidyverse)
# Repeat last graph but reorder data firstToothGrowth2<- arrange(ToothGrowth, len)
dot_cols2<- as.integer(factor(ToothGrowth2$dose))
ggplot(ToothGrowth, aes(x=len, group=factor(dose))) +
geom_dotplot(fill=dot_cols2,
binwidth=.5)
@yutannihilation I'm sorry, I think you misunderstood the point of my post. So let me be more explicit: if I pass a vector to fill where the n-th entry is "black" (or 1), I expect the dot corresponding to the n-th observation to be black. But as my reprex shows, this is not the case.
In other words, I'm reporting what I believe to be a bug in ggplot2.
Ah, sorry I missed your point. I think you are right, but I believe passing multiple values outside aes() is not a recommended usage anyway.
As far as I can tell, this could be a consequence of the data being sorted with respect to x, which means that the rows of the dataset no longer match the elements of the vector dot_cols.
I'm reopening this because there might be some room for better warnings or documents.
you can define a new column in your dataset not as an outside vector and use it.
you can use color values and use scale_fill_identity()
What is exactly your use case and why you need/want an outside vector ?
of course using an outside vector means that you will take ownership of the ordering as opposed to having it part of the data and then sorting is applied to the whole data.
library(tidyverse)
library(patchwork)
ToothGrowth$dot_cols<- ifelse(ToothGrowth$dose=="0.5", "red","blue")
ggplot(ToothGrowth, aes(x=len)) +
geom_dotplot(aes(fill=dot_cols))+
scale_fill_identity()+
facet_grid(~dose)
#> Bin width defaults to 1/30 of the range of the data. Pick better value with `binwidth`.
@smouksassi It's not that I have a personal use case. I usually either use a column for the fill aesthetic, or a vector of length 1, i.e. fill = 'black'.
The "use case" would be more pedagogical. A student actually found the reported bug when writing code they thought was correct. Indeed, using geom_point and the colour aesthetic, the code would behave as expected. I try to teach best practices, but sometimes students don't follow these guidelines.
From my perspective, and the perspective of my students, I think one of the following three solutions would work:
When the dataset is sorted, so are the aesthetics.
Error when passing a vector of length greater than 1 as a fill or colour aesthetic.
Warning when passing a vector of length greater than 1 as a fill or colour aesthetic aesthetic.
I can certainly provide a pull request for one of the last two options if you want.
Thanks, I think we all are agreeing on this option (I'd like to implement this as a general one, not limited to geom_dotplot(), not limited to colour or fill).
Warning when passing a vector of length greater than 1
Note that, we cannot and should not expect how the data gets aggregated and sorted. It's usually obvious when stat is not identity, but StatBindot, which is used in dot plot. is a rare statistical computation in that the length of the computed data is the same as the original (if I understand correctly), so I understand one may fall into a wrong sense...
"Aesthetics must be either length 1 or the same as the data ({n}): ",
glue_collapse(names(which(!good)), ", ", last=" and ")
))
}
In the if statement and before the return statement, there could be a check if (any(ns == n)), and if the condition is TRUE, the warning would be triggered, telling the user that it is not recommended and can lead to undesired behaviour.
that would be too general as the function is also used for cases where that is allowed - better to put the warning in use_defaults()...
But before we go down that road we need to make sure that there are no legitimate case for passing aesthetics as parameters with length > 1... Team, can you weigh in?
I feel we've been at this point before. For some reason people love to use this feature (passing in data as parameters rather than via aes()), and most of the time it works, and sometimes it doesn't, and sometimes it actually solves a real problem. My solution would be to leave the code as is but state very prominently, maybe at multiple places in the documentation, that this practice is not encouraged and may not lead to expected results.
most of the time it works, and sometimes it doesn't, and sometimes it actually solves a real problem
I don't remember well, but I get this point. However, I think almost all of those cases are stat_identity(). Can we warn only when the stat is not identity? Geom$use_default() cannot refer to the layer's information, so it looks difficult, though.
I think there are legitimate use-cases for providing a vector of values outside aes(). Mostly, you'd want to bypass mapping these values to a scale. I'd also expect that these values are parallel to the input data and follow recycling rules (1 or nrow(data), otherwise error). I you want to prevent passing aesthetics as vectors outside aes(), I can get behind that, if there is an alternate way of bypassing scales.
For example, if this is my preferred outcome for a plot:
Then I want a way to pass values verbatim as (unmapped) colour aesthetic, without bothering with a scale. Normally you'd use scale_*_identity() to do that, but that doesn't work when another layer does use the scale for that aesthetic.
Maybe, all scales should just ignore AsIs class objects in their training, mapping, and censoring, but that discussion might be worth separating from this one.
@teunbrand can you open a new issue to discuss special treatment of I() input?
As for this, providing a vector of values outside of aes() is always undefined behaviour. The only reason why we don't throw an error is because it would break too many old plots. Allowing it for some stats would only lead to more confusion IMO
StatBindot, which is used in dot plot. is a rare statistical computation in that the length of the computed data is the same as the original
This is sort of true and false at the same time. The stats computes a summary per bin, which the geom expands again into invidual dots based on the count per bin. So while yes, the number of points in the output is the same as the input, there is no reasonable way to preserve non-constant information about individual observations.
Activity
turgeonmaxime commentedon Apr 29, 2022
Just realized I forgot to add my session info:
Details
Created on 2022-04-28 by the reprex package (v2.0.1)
yutannihilation commentedon Apr 29, 2022
turgeonmaxime commentedon Apr 29, 2022
@yutannihilation I'm sorry, I think you misunderstood the point of my post. So let me be more explicit: if I pass a vector to
fillwhere the n-th entry is"black"(or1), I expect the dot corresponding to the n-th observation to be black. But as my reprex shows, this is not the case.In other words, I'm reporting what I believe to be a bug in
ggplot2.yutannihilation commentedon Apr 29, 2022
Ah, sorry I missed your point. I think you are right, but I believe passing multiple values outside
aes()is not a recommended usage anyway.I'm reopening this because there might be some room for better warnings or documents.
smouksassi commentedon Apr 29, 2022
you can define a new column in your dataset not as an outside vector and use it.
you can use color values and use scale_fill_identity()
What is exactly your use case and why you need/want an outside vector ?
of course using an outside vector means that you will take ownership of the ordering as opposed to having it part of the data and then sorting is applied to the whole data.
Created on 2022-04-29 by the reprex package (v2.0.1)
turgeonmaxime commentedon Apr 29, 2022
@smouksassi It's not that I have a personal use case. I usually either use a column for the
fillaesthetic, or a vector of length 1, i.e.fill = 'black'.The "use case" would be more pedagogical. A student actually found the reported bug when writing code they thought was correct. Indeed, using
geom_pointand thecolouraesthetic, the code would behave as expected. I try to teach best practices, but sometimes students don't follow these guidelines.From my perspective, and the perspective of my students, I think one of the following three solutions would work:
I can certainly provide a pull request for one of the last two options if you want.
yutannihilation commentedon Apr 30, 2022
Thanks, I think we all are agreeing on this option (I'd like to implement this as a general one, not limited to
geom_dotplot(), not limited tocolourorfill).Note that, we cannot and should not expect how the data gets aggregated and sorted. It's usually obvious when
statis notidentity, butStatBindot, which is used in dot plot. is a rare statistical computation in that the length of the computed data is the same as the original (if I understand correctly), so I understand one may fall into a wrong sense...turgeonmaxime commentedon May 5, 2022
Would it make sense to add the warning in the function
check_aesthetics? Or would that be too general?ggplot2/R/geom-.r
Lines 210 to 222 in a6c22e7
In the
ifstatement and before thereturnstatement, there could be a checkif (any(ns == n)), and if the condition isTRUE, the warning would be triggered, telling the user that it is not recommended and can lead to undesired behaviour.thomasp85 commentedon May 12, 2022
that would be too general as the function is also used for cases where that is allowed - better to put the warning in
use_defaults()...But before we go down that road we need to make sure that there are no legitimate case for passing aesthetics as parameters with length > 1... Team, can you weigh in?
clauswilke commentedon May 12, 2022
I feel we've been at this point before. For some reason people love to use this feature (passing in data as parameters rather than via
aes()), and most of the time it works, and sometimes it doesn't, and sometimes it actually solves a real problem. My solution would be to leave the code as is but state very prominently, maybe at multiple places in the documentation, that this practice is not encouraged and may not lead to expected results.yutannihilation commentedon May 12, 2022
I don't remember well, but I get this point. However, I think almost all of those cases are
stat_identity(). Can we warn only when thestatis notidentity?Geom$use_default()cannot refer to the layer's information, so it looks difficult, though.teunbrand commentedon Jan 4, 2023
I think there are legitimate use-cases for providing a vector of values outside
aes(). Mostly, you'd want to bypass mapping these values to a scale. I'd also expect that these values are parallel to the input data and follow recycling rules (1 ornrow(data), otherwise error). I you want to prevent passing aesthetics as vectors outsideaes(), I can get behind that, if there is an alternate way of bypassing scales.For example, if this is my preferred outcome for a plot:
Then I want a way to pass values verbatim as (unmapped) colour aesthetic, without bothering with a scale. Normally you'd use
scale_*_identity()to do that, but that doesn't work when another layer does use the scale for that aesthetic.Created on 2023-01-04 by the reprex package (v2.0.1)
Maybe, all scales should just ignore
AsIsclass objects in their training, mapping, and censoring, but that discussion might be worth separating from this one.thomasp85 commentedon Jan 9, 2023
@teunbrand can you open a new issue to discuss special treatment of
I()input?As for this, providing a vector of values outside of
aes()is always undefined behaviour. The only reason why we don't throw an error is because it would break too many old plots. Allowing it for some stats would only lead to more confusion IMOteunbrand commentedon Mar 15, 2024
About the following point:
This is sort of true and false at the same time. The stats computes a summary per bin, which the geom expands again into invidual dots based on the count per bin. So while yes, the number of points in the output is the same as the input, there is no reasonable way to preserve non-constant information about individual observations.