Skip to content

Conversion not implemented for geom_jitter #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
scworland opened this issue May 15, 2015 · 11 comments
Closed

Conversion not implemented for geom_jitter #221

scworland opened this issue May 15, 2015 · 11 comments

Comments

@scworland
Copy link

geom_jitter not converted

# boxplot of normalized water use
p = ggplot(NW2)  
p = p + geom_jitter(aes(factor(year),norm_withdrawal,color=factor(norm_withdrawal)), alpha=0.6,
              size=3) + guides(color=FALSE) 
p = p + geom_boxplot(aes(factor(year),norm_withdrawal),alpha=0.8)
p = p + xlab("Years") + ylab("Public Supply withdrawal/county pop")
p = p + ggtitle("Normalized Public Supply wthdrawal for 315 MSAs") + theme_grey(base_size = 16)
p

Warning message:

In layer2traces(L, df, misc) :
  Conversion not implemented for geom_jitter (basic geom_jitter), ignoring. 
@scworland
Copy link
Author

I tried using a slight hack (position_jitter with geom_point) from here:

# boxplot of normalized water use
p = ggplot(NW2)  
p = p + geom_point(aes(factor(year),norm_withdrawal,color=factor(norm_withdrawal)), alpha=0.6,
                   size=3, position = "jitter") + guides(color=FALSE) 
p = p + geom_boxplot(aes(factor(year),norm_withdrawal),alpha=0.8)
p = p + xlab("Years") + ylab("Public Supply withdrawal/county pop")
p = p + ggtitle("Normalized Public Supply wthdrawal for 315 MSAs") + theme_grey(base_size = 16)

But the points are not jittered

@13bzhang
Copy link
Contributor

Thanks for letting us know. I will look into this.

-- Baobao Zhang
On May 15, 2015 1:04 PM, "scottcworland" [email protected] wrote:

I tried using a slight hack (position_jitter with geom_point) from here
http://aarongonzales.net/post/data/plotly_working_examples/:

boxplot of normalized water usep = ggplot(NW2) p = p + geom_point(aes(factor(year),norm_withdrawal,color=factor(norm_withdrawal)), alpha=0.6,

               size=3, position = "jitter") + guides(color=FALSE) p = p + geom_boxplot(aes(factor(year),norm_withdrawal),alpha=0.8)p = p + xlab("Years") + ylab("Public Supply withdrawal/county pop")p = p + ggtitle("Normalized Public Supply wthdrawal for 315 MSAs") + theme_grey(base_size = 16)

But the points are not jittered


Reply to this email directly or view it on GitHub
#221 (comment).

@13bzhang 13bzhang self-assigned this May 19, 2015
@13bzhang
Copy link
Contributor

I think I have a solution. Based on the hack that @scottcworland noted, I added a jitter function within trace_generation.R that's the same as the point function:
https://github.com/ropensci/plotly/blob/baobao-geom_jitter/R/trace_generation.R#L559-L587

p <- ggplot(mpg, aes(displ, hwy))
p + geom_jitter(position = position_jitter())
py$ggplotly(p + geom_jitter())

From ggplot2:
221_ggplot2

Plotly:
221_plotly

Two questions:

  1. If the old code doesn't observe the style guide, if we copy and paste the code, we should make it observe the style guide, righto?
  2. I was thinking maybe for geom_jitter maybe I should add something that pushes the original data (along with the jittered x and y) onto Plotly. Thoughts?

@mkcor @cpsievert @chriddyp

@13bzhang 13bzhang removed their assignment May 19, 2015
@mkcor
Copy link
Contributor

mkcor commented May 19, 2015

Thanks @13bzhang , these are really great questions.

Regarding the first one, the policy is the following: all new code you write should comply with our style guide, but as you run into old code that doesn't comply, you don't have to 'fix' it. You are welcome to, but then make that a separate commit.

@chriddyp
Copy link
Member

Nice! For 2. We don't have a good way of doing this as part of the figure yet. In the future, once we refactor and add the tabular data methods (uploading tabular data to plotly) to the package we could upload the original columns as the "source" data

@13bzhang
Copy link
Contributor

@mkcor Thanks for answering my question. I will make it comply with the style guide.
@chriddyp Good to know. I tried to do it this afternoon but didn't get it to work. So really thanks for letting me know, haha!

@13bzhang
Copy link
Contributor

Hi guys, I have been having trouble writing tests for this geom_jitter thing because it's hard to set a seed for jitter to get the jitter exactly the same. I tried to set.seed to both butt gg2list is not picking up on the seed. So what happens is that the jittered data is slightly off.

Below are my results

> # Test 1
> # set up the data
> set.seed(1001)
> p1 <- ggplot() + geom_jitter(data = mpg, aes(displ, hwy), width = 1)
> head(ggplot_build2(p1)$data[[1]]$x)
[1] 1.838855 1.793010 1.994363 1.993534 2.794121 2.831024
> # test
> test_that("geom_jitter is working", {
+   set.seed(1001)
+   info <- expect_traces(p1, 1, "geom_jitter", 1001)
+   tr <- info$traces[[1]]
+   la <- info$layout
+   expect_identical(tr$type, "scatter")
+   set.seed(1001)
+   built <- ggplot_build2(p1)
+   print(head(tr$x)) # from gg2list
+   print(head(built$data[[1]]$x)) # from ggplot_build2
+   expect_identical(tr$x, built$data[[1]]$x)
+   expect_identical(tr$y, built$data[[1]]$y)
+ })
[1] "running coord_fixed-geom_jitter"
[1] 1.774830 1.763145 1.982322 2.029265 2.800319 2.822188
[1] 1.838855 1.793010 1.994363 1.993534 2.794121 2.831024
Error: Test failed: 'geom_jitter is working'
* Not expected: tr$x is not identical to built$data[[1]]$x. Differences: 
Mean relative difference: 0.008040036.
* Not expected: tr$y is not identical to built$data[[1]]$y. Differences: 
Mean relative difference: 0.0109569.

Carson, do you have any suggestions?
Also, is it ok for me write the test in such a way that says the two sets of jittered data are not statistically different? Like a t-test combined with a K-S test?
@mkcor @cpsievert @chriddyp

@chriddyp
Copy link
Member

If it's too hard to set a seed, then I'd just copy and paste a vector of generated data into the test and call it a day!

@13bzhang
Copy link
Contributor

Ok, that sounds good!

@cpsievert
Copy link
Collaborator

@13bzhang it's probably the case that each time you "build" a plot, you get a different jitter. So you should be careful to compare the data from the same built object

Could you start a pull request so we can make comments/recommendations on commits, etc.?

@cpsievert
Copy link
Collaborator

Fixed via 1993631

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants