Nancy
Nancy

Reputation: 4089

Plotting points with factor data in ggplotly

I'm trying to use ggplotly() with a scatterplot that has factors for both the x and y axes. However, ggplotly is doing something that I don't understand that dramatically changes the presentation of my data. Here's an example, but note that I intentionally made x and y factors and that they should not be considered numbers:

mydata = data.frame(x = factor(c(1:40)),
                    y = factor(sample(c(1:40), 40, replace = TRUE)),
                    count = sample(c(1:5), 40, replace = TRUE))

myplot = ggplot(mydata, aes(x, y)) + 
  geom_point(aes(size = count))

Using just ggplot it looks great:

enter image description here

However, when I use ggplotly(myplot) I get this, which is obviously very different:

enter image description here

My ultimate goal is to be able to mouseover each point and get the coordinates and the "count" variable. I'm open to alternative methods, but I'd also like to understand what's going on here.

Upvotes: 2

Views: 152

Answers (1)

MLavoie
MLavoie

Reputation: 9836

It seems that there are still a lot of problems with plotly and ggplot2 working together. Sometime it works and sometime you have unexpected results (e.g. facet with legend). So sometime you have to go with the alternative; try this:

plot_ly(mydata, x = x, y = y,
        mode = "markers", size = count)

enter image description here

Upvotes: 1

Related Questions