Skip to content
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

Smart axis type defaults #578

Closed
royr2 opened this issue May 10, 2016 · 4 comments
Closed

Smart axis type defaults #578

royr2 opened this issue May 10, 2016 · 4 comments

Comments

@royr2
Copy link

royr2 commented May 10, 2016

@cpsievert @timelyportfolio I was trying to answer this question on SO and came across this:

library(plotly)

df <- data.frame(x = c(1, 2, 4, 8, 16, 32), y = 1:6)
df$x <- as.factor(df$x)
# Works fine
p <- ggplot(df, aes(x = x, y = y)) + geom_point()
p

image

# Looks wrong
pp <- plot_ly(df, x = x, y = y, mode = "markers")
pp

image

# works fine
ggplotly(p)

image

@royr2 royr2 changed the title Factors on x-axis creates exponential plot Factors treated as numeric May 10, 2016
@royr2
Copy link
Author

royr2 commented May 10, 2016

I am not sure if this is by design? In which case please close the issue...

@cpsievert
Copy link
Collaborator

Currently you need to manually specify that you have a categorical axis.

plot_ly(df, x = x, y = y, mode = "markers") %>% layout(xaxis = list(type = "category"))

Right now we just sent the factor levels as a string to plotly.js

plotly_build(plot_ly(df, x = x, y = y, mode = "markers"))$data[[1]]$x
#> [1] 1000  2000  4000  8000  16000 32000
#> Levels: 1000 2000 4000 8000 16000 32000

Based on those strings, plotly.js (correctly) thinks that you want a numeric axis. In the future, we should set smarter axis type defaults based on the variable mappings.

@cpsievert cpsievert changed the title Factors treated as numeric Smart axis type defaults May 10, 2016
@royr2
Copy link
Author

royr2 commented May 10, 2016

Ah okay got it. I haven't used list(type = "category") before. Good to know. Thanks !

cpsievert added a commit that referenced this issue Aug 11, 2016
Order of categorical axis should reflect factor levels. Fixes #578.
@gorgitko
Copy link

gorgitko commented Nov 9, 2017

Hello, I am still having problem and even layout(xaxis = list(type = "category")) didn't help. My data:

> nrow(neg_summary)
305
> head(neg_summary)

Sentrix_Name Sentrix_ID max      third_quartile median    mean      first_quartile min     
4569632047_A 4569632047 258.6660 104.0361       92.18008  95.01825  82.17424       58.23933
4569632047_B 4569632047 268.4030 110.9371       98.80281  102.25254 88.49149       61.55940
4569632047_C 4569632047 241.0056 107.2028       95.66486  98.47206  85.11318       53.73413
4569632047_D 4569632047 266.8456 111.0126       99.76010  102.21811 88.52625       60.53619
4569632047_E 4569632047 265.3769 109.5106       96.99408  100.01776 85.60737       57.35104
4569632047_F 4569632047 273.5587 118.1988       103.79723 107.54025 91.06809       62.88250

Plotting:

plot_ly(neg_summary, y = ~Sentrix_Name, x = ~max, name = "maximum",
        type = "scatter", mode = "lines+markers") %>%
       layout(yaxis = list(type = "category")) %>%
       add_trace(x = ~third_quartile, name = "third quartile", mode = "lines+markers") %>%
       add_trace(x = ~median, name = "median", mode = "lines+markers") %>%
       add_trace(x = ~first_quartile, name = "first quartile", mode = "lines+markers") %>%
       add_trace(x = ~min, name = "minimum", mode = "lines+markers")

Result:

plotly

Categorical variables at y-axe aren't in the same order as neg_summary$Sentrix_Name and even there aren't all. This should be the order:

> neg_summary$Sentrix_Name
[1] 4569632047_A 4569632047_B 4569632047_C 4569632047_D 4569632047_E 4569632047_F 4569632048_A 4569632048_B 4569632048_C 4569632048_D 4569632048_E
...
305 Levels: 4569632047_A 4569632047_B 4569632047_C 4569632047_D 4569632047_E 4569632047_F 4569632048_A 4569632048_B 4569632048_C ... 5226121006_F

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

No branches or pull requests

3 participants