【问题标题】:Plotly is not reading ggplot output wellPlotly 没有很好地读取 ggplot 输出
【发布时间】:2020-09-22 14:33:23
【问题描述】:

我正在使用以下代码绘制一些数据点,它在 ggplot 中运行良好。但是,当我将其输入 ggplotly 时,可视化和 Y 轴标签会完全改变。 Y 轴标签向右移动并被翻转,中心的线条变细。

代码

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 图像

ggplotly 图像

可重现的例子

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

【问题讨论】:

  • 在您的 ggplot 函数之前添加此 file2 &lt;- read.csv( text = RCurl::getURL("https://gist.githubusercontent.com/gireeshkbogu/806424c1777ff721a046b3e30e85af5a/raw/50ac0b4696f514677b4987b90305fdf879fbcd84/reproducible.examples.txt"), sep = "\t") 以使您的问题可重现。另请详细阅读我与您分享的第一个链接,以更好地了解可重现的示例。简短的信息是,在大多数情况下,要共享您的数据,您需要运行 dput(head(data)) 之类的东西并避免复制粘贴。干杯。

标签: ggplot2 ggplotly


【解决方案1】:

将 ggplot 转换为 plotly 非常复杂!许多 ggplot 功能被默默地删除或错误地转换为 plotly。

如果我没记错的话,你的facet_grid 中的switch = "y" 正在被悄悄删除。

此外,你的情节中有太多方面。看起来“主题”正在创建 30 多个方面。我知道尝试将尽可能多的数据放入一个图中很诱人,但您确实在推动您可以在此处使用方面的极限。

我做了一些修改。看看这是不是你可以使用的东西:

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 

修改后的代码产生了这个情节。您可能需要按“主题”创建几个小组,并为每个小组调用一个情节。

【讨论】:

猜你喜欢
  • 2012-10-18
  • 2018-06-13
  • 2011-08-05
  • 2014-12-14
  • 1970-01-01
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
  • 2020-08-13
相关资源
最近更新 更多