当使用带有ggplot和ggplotly的多面图时,字母出现在图的外部

问题描述 投票:0回答:1

我想同时使用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")))

此结果

enter image description here

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

如果没有这封信,我该如何得到相同的情节?也许更有趣,为什么会这样?

谢谢,

r ggplot2 plot plotly data-visualization
1个回答
0
投票

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

theme(legend.position = "bottom")

实际上,这是一个非常棘手的问题。 ggplotly实际上不能正确地传输所有形式的ggplot。这个主题有一个github问题,但我相信问题仍然存在。看到:(legend.position在ggplotly中始终为'right',除非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")))

Image of Modified PLot

© www.soinside.com 2019 - 2024. All rights reserved.