【问题标题】:Letters appearing outside plots when using multiple facet plotting with ggplot and ggplotly使用 ggplot 和 ggplotly 进行多面绘图时出现在绘图之外的字母
【发布时间】:2020-09-14 11:20:23
【问题描述】:

我想同时使用 ggplot 和 plotly(准确地说是 ggplotly)创建一个平面图。几乎一切正常。以下代码:

library(dplyr)
library(plotly)
library(ggplot2)

Year <- c(2000:2008)
Name <- c('A', 'B')
Size <- rep(c('Small', 'Medium', 'Large'), each=6)
City <- c('NY', 'PARIS', 'BERLIN')
Frequency <- sample(x = c(100:1000), size = 144)
Rel_Freq <- sample(x = c(1:100), size = 144, replace = TRUE)

StackData <- data.frame(Year, Name, Size, City, Frequency, Rel_Freq)

StackData$Size <- factor(StackData$Size, levels = c("Small", "Medium", "Large"))
ggplotly(ggplot(StackData, aes(x= Year, y= Frequency, shape = Name, col = Name)) +
  geom_point(size = 3)+
  scale_shape_manual(values= c(17, 6))+
  scale_color_manual(values = c("#37D9E1", "#3D3D3F")) +
  facet_grid(City ~ Size, scales="free_y")+
  theme_bw()+
  theme(legend.position = "bottom",
        panel.background = element_rect(fill = "transparent"),
        axis.text.x = element_text(angle = 30, hjust = 1),
        strip.text.x = element_text( size = 12, face = "bold" ), 
        strip.text.y = element_text( size = 12, face = "bold" ))+
  scale_fill_manual(values = c("#D3D3D3", "#A9A9A9", "#696969"), guide=FALSE)+
  scale_y_continuous(trans = "log10",
                     labels = scales::unit_format(
                       unit = "", 
                       scale = 1))+
  labs(y= "",
       x= ""),
  tooltip = c("x","y","colour"),
  autosize = T, width = 680, height = 530) %>%
  layout(showlegend = FALSE,
         margin = list(l = 0, r = 25, t = 50, b = 130),
         annotations = list(x = .5, y = -0.25, #position of text adjust as needed
                            text = "Super cool Plot",
                            showarrow = F, 
                            xref='paper', 
                            yref='paper',
                            xanchor='auto',
                            yanchor='bottom',
                            xshift=0,
                            yshift=0,
                font=list(size=9, color="black")))

结果

如图所示,右上角出现了一个字母。经过一些更改后,我意识到这是我将 ggplot 中的颜色和形状重定向到的变量的第一个字母(在本例中为“名称”)。

如果没有这封信,我怎样才能得到相同的情节?也许更有趣的是,为什么会发生这种情况?

提前致谢,

【问题讨论】:

    标签: r ggplot2 plot plotly data-visualization


    【解决方案1】:

    那个奇怪的“N”来自你在 ggplot 中主题的图例部分:

    theme(legend.position = "bottom")
    

    其实这是一个比较棘手的问题。 ggplotly 实际上并不能正确地从 ggplot 中传输所有内容。关于这个主题有一个github问题,但我相信问题仍然存在。 看: (legend.position 在 ggplotly 中总是“正确”,除非 legend.position = 'none' )https://github.com/ropensci/plotly/issues/1049

    在您的情况下,ggplotly 忽略了 legend.position = "bottom" 参数。

    选项 1: 看起来您可能实际上并不想要图表中的图例。在这种情况下,您最好在 ggplot 和 ggplotly 之间同步图例调用:

    # ggplot portion
    theme(legend.position = "none")
    # plotly portion:
    layout(showlegend = FALSE)
    

    选项 2: 仅在情节中格式化图例。从上面的 github 问题链接中,这是建议的想法之一:

    ggplotly(
      ggplot(df, aes(year, freq, color = clas)) +
        geom_line() +
        theme(legend.position = 'top')
    ) %>%
      layout(legend = list(
          orientation = "h"
        )
      )
    

    我使用选项 1 修改了您的代码并提出了以下内容。奇怪的“N”现在不见了!

    library(dplyr)
    library(plotly)
    library(ggplot2)
    
    Year <- c(2000:2008)
    Name <- c('A', 'B')
    Size <- rep(c('Small', 'Medium', 'Large'), each=6)
    City <- c('NY', 'PARIS', 'BERLIN')
    Frequency <- sample(x = c(100:1000), size = 144)
    Rel_Freq <- sample(x = c(1:100), size = 144, replace = TRUE)
    
    StackData <- data.frame(Year, Name, Size, City, Frequency, Rel_Freq)
    
    StackData$Size <- factor(StackData$Size, levels = c("Small", "Medium", "Large"))
    StackData
    
    ggplotly(ggplot(StackData, aes(x= Year, y= Frequency, shape = Name, col = Name)) +
               geom_point(size = 3)+
               scale_shape_manual(values= c(17, 6))+
               scale_color_manual(values = c("#37D9E1", "#3D3D3F")) +
               facet_grid(City ~ Size, scales="free_y")+
               theme_bw()+
               theme(legend.position = "none",        ## this is the only change to your code
                     panel.background = element_rect(fill = "transparent"),
                     axis.text.x = element_text(angle = 30, hjust = 1),
                     strip.text.x = element_text( size = 12, face = "bold" ), 
                     strip.text.y = element_text( size = 12, face = "bold" ))+
               scale_fill_manual(values = c("#D3D3D3", "#A9A9A9", "#696969"), guide=FALSE)+
               scale_y_continuous(trans = "log10",
                                  labels = scales::unit_format(
                                    unit = "", 
                                    scale = 1))+
               labs(y= "",
                    x= ""),
             tooltip = c("x","y","colour"),
             autosize = T, width = 680, height = 530) %>%
      layout(showlegend = FALSE,
             margin = list(l = 0, r = 25, t = 50, b = 130),
             annotations = list(x = .5, y = -0.25, #position of text adjust as needed
                                text = "Super cool Plot",
                                showarrow = F, 
                                xref='paper', 
                                yref='paper',
                                xanchor='auto',
                                yanchor='bottom',
                                xshift=0,
                                yshift=0,
                                font=list(size=9, color="black")))
    

    【讨论】:

    • 这是一个 2 周(或多或少)的问题,我一直试图通过查看 SO 和其他页面来解决。最后我放弃并发布了这个问题。感谢您对问题的解释和解决方法的超级完整答案!
    • 乐于助人!当您有机会时,请将答案标记为“已接受”。这样以后其他人遇到这个问题的时候,就知道这个问题有解决方案了。
    • 刚刚在 SO 中学习了一个新命令。完毕。已标记答案。谢谢!
    猜你喜欢
    • 2021-11-03
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    相关资源
    最近更新 更多