Giter VIP home page Giter VIP logo

Comments (4)

eliocamp avatar eliocamp commented on June 30, 2024

I have no idea how plotly works, but form what little I've used it, I think it basically hand-crafts translations of each ggplot2 geom to the corresponding plotly mark. If that's the case, then I don't think it'd be possible because the way ggnewscale works is by creating new geoms with new aesthetics for each new scale.

from ggnewscale.

eliocamp avatar eliocamp commented on June 30, 2024

I've skimmed the relevant source code.

For this specific case you can use:

library(ggplot2)
library(ggnewscale)
# Equivalent to melt(volcano)
topography <- expand.grid(x = 1:nrow(volcano),
                          y = 1:ncol(volcano))
topography$z <- c(volcano)

# point measurements of something at a few locations
set.seed(42)
measurements <- data.frame(x = runif(30, 1, 80),
                           y = runif(30, 1, 60),
                           thing = rnorm(30))

# Defines a the to_basic method to the NewGeomContour class and renames the relevant variables. 
# Very hacky. Don't know how it will generalise. Will break if you add more scales.  
to_basic.NewGeomContour <- function(data, prestats_data, layout, params, p, ...) {
  if (!"fill" %in% names(data)) data$fill <- NA
  colnames(data)[colnames(data) == "colour_new"] <- "colour"
  colnames(data)[colnames(data) == "colour_new_plotlyDomain"] <- "colour_plotlyDomain"

  plotly:::prefix_class(data, "GeomPath")
}


ggplot(mapping = aes(x, y)) +
  geom_contour(data = topography, aes(z = z, color = stat(level))) +
  scale_color_viridis_c(option = "D") +
  new_scale_color() +
  geom_point(data = measurements, size = 3, aes(color = thing)) +
  scale_color_viridis_c(option = "A") -> p

plotly::ggplotly(p)

from ggnewscale.

pmoracho avatar pmoracho commented on June 30, 2024

I have no idea how plotly works, but form what little I've used it, I think it basically hand-crafts translations of each ggplot2 geom to the corresponding plotly mark. If that's the case, then I don't think it'd be possible because the way ggnewscale works is by creating new geoms with new aesthetics for each new scale.

I understand the same as you do, that's why I assumed that it should not be so easy, but just in case I was asking you.

My concern arose from this: ggplotly no me grafica el mismo grafico que ggplot

from ggnewscale.

pmoracho avatar pmoracho commented on June 30, 2024
 if (!"fill" %in% names(data)) data$fill <- NA
  colnames(data)[colnames(data) == "colour_new"] <- "colour"
  colnames(data)[colnames(data) == "colour_new_plotlyDomain"] <- "colour_plotlyDomain"

I tried something very similar, an automatic wrapper of each new geom to the plotly function, obviously it solves the warning issue but does not take into account the logic of ggnewscale.

Thanks for your time Elio

from ggnewscale.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.