0

I am using the following code to plot some data points and it works well in ggplot. However, when I feed this into ggplotly, the visualization and Y-axis labels change completely. Y-axis label shift to right and gets flipped, and the lines in the center get thinner.

Code

library(ggplot2)
library(tidyverse) 
library(plotly)

file2 <- read.csv( text = RCurl::getURL("https://gist.githubusercontent.com/gireeshkbogu/806424c1777ff721a046b3e30e85af5a/raw/50ac0b4696f514677b4987b90305fdf879fbcd84/reproducible.examples.txt"), sep="\t")

p <- ggplot(data=subset(file2,!is.na(datetime)), 
           aes(x=datetime, y=Count, 
               color=Type, 
               group=Subject)) + 
  geom_point(size=4, alpha=0.6) +
  scale_y_continuous(breaks=c(0,1))+
  theme(axis.text.x=element_text(angle=90, size = 5))+
  facet_grid(Subject ~ ., switch = "y") +
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+
  theme(strip.text.y.left = element_text(angle = 0, size=5)) +
  scale_color_manual(values=c("red", "#990000", "#330000", "#00CC99", "#0099FF"))

ggplotly(p)

Ggplot image enter image description here

Ggplotly image enter image description here

Reproducible Example

Subject datetime    Type    Count
user1   4/16/20 15:00   A1  1
user1   3/28/20 13:00   A1  1
user2   4/29/20 15:00   A1  1
user2   5/02/20 09:00   A1  1
user1   2/19/20 18:00   A2  1
user1   4/20/20 16:00   A2  1
M--
  • 25,431
  • 8
  • 61
  • 93
ferrelwill
  • 771
  • 2
  • 8
  • 20
  • You should provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It should be [minimal, but complete and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). – M-- Jun 03 '20 at 19:18
  • 1
    Add this ```file2 <- read.csv( text = RCurl::getURL("https://gist.githubusercontent.com/gireeshkbogu/806424c1777ff721a046b3e30e85af5a/raw/50ac0b4696f514677b4987b90305fdf879fbcd84/reproducible.examples.txt"), sep = "\t")``` before your ggplot function to make your question reproducible. Please also read the first link I shared with you in details, to get a better understanding of reproducible example. Short info is that most of the times, to share your data, you need to run something like `dput(head(data))` and avoid copy pasting. Cheers. – M-- Jun 03 '20 at 20:06

1 Answers1

1

Converting ggplot to plotly turns out to be surprisingly complicated! Many ggplot features are silently dropped or incorrectly translated over to plotly.

If I am not mistaken, switch = "y" within your facet_grid is being silently dropped.

In addition, you have too many facets in your plot. Looks like "Subject" is creating 30+ facets. I know that it is tempting to try and fit as much data into one plot, but you are really pushing the limits of what you can do with facets here.

I made some modifications. See if this is something you can work with:

library(ggplot2)
library(tidyverse) 
library(plotly)
library(RCurl)

# your original file
file2 <- read.csv( text = RCurl::getURL("https://gist.githubusercontent.com/gireeshkbogu/806424c1777ff721a046b3e30e85af5a/raw/50ac0b4696f514677b4987b90305fdf879fbcd84/reproducible.examples.txt"), sep="\t")
head(file2)

# scaling down the dataframe so that you have fewer facets per plot
file3 <- file2 %>% 
  as_tibble() %>% 
  na.omit() %>% 
  filter(Subject %in% c("User1",  "User2",  "User3",  "User4")) %>% 
  arrange(Subject, datetime)
head(file3)

# sending the smaller data frame to ggplot
p_2 <- ggplot(data=file3, 
              aes(x=datetime, y=Count, color=Type, group=Subject)) + 
  geom_point(size=4, alpha=0.6) +
  scale_y_continuous(breaks=c(0,1))+
  theme(axis.text.x=element_text(angle=90, size = 5)) +
  facet_grid(Subject ~ .) +        # removing "Switch" ; it is being dropped by plotly
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        legend.position = "left") +    # move legend to left on ggplot
  theme(strip.text.y.left = element_text(angle = 0, size=5)) +
  scale_color_manual(values=c("red", "#990000", "#330000", "#00CC99", "#0099FF"))
p_2

ggplotly(p_2) %>% 
  layout(title = "Modified & Scaled Down Plot",  
         legend = list(orientation = "v", # fine-tune legend directly in plotly,
                       y = 1, x = -0.1))  # you may need to fiddle with these 

The modified code yields me this plot. You will probably need to make a few small groups by "Subject" and call a plot for each group.

modified ggplotly image

Piranha
  • 116
  • 6
  • Also, if you haven't already come across these...try taking a look through these posts: https://github.com/ropensci/plotly/issues/1086 https://stackoverflow.com/questions/43848724/main-title-and-legend-position-issue-on-ggplotly – Piranha Jun 05 '20 at 01:23
  • Unfortunately, this doesn't help. The output is the same as the one in the question when you use all the data for plotting. – ferrelwill Jun 05 '20 at 11:15