从 ggplot2 转换为 plotly 后激活或停用图例键时绘图缩小或扩展
Posted
技术标签:
【中文标题】从 ggplot2 转换为 plotly 后激活或停用图例键时绘图缩小或扩展【英文标题】:Plot shrank or expanded when activating or deactivating legend keys after converting from ggplot2 to plotly 【发布时间】:2021-11-03 16:57:08 【问题描述】:在下面的 Shiny 应用程序中,当我双击图例键以激活或停用它们时,情节会缩小或扩大。
我不确定这是否是 plotly
或 shiny
中的错误。
感谢任何帮助解决此问题。感谢阅读!
library(ggplot2)
library(plotly)
library(shiny)
# ui
ui <- bootstrapPage(
plotlyOutput('rev')
)
# server
server <- function(input, output)
output$rev <- renderPlotly(
p <- ggplot(mpg, aes(x = displ, y = hwy, color = factor(cyl), shape = class)) +
geom_point(size = 3) +
scale_color_manual(values = c("4" = "#abd9e9",
"5" = "#2c7bb6",
"6" = "#ff7f00",
"8" = "#d7191c")) +
scale_shape_manual(values = c(2, 5, 16, 17, 18, 19, 25)) +
guides(color = guide_legend(title = "Cyl", order = 1),
shape = guide_legend(title = "", order = 2)) +
theme_classic(base_size = 16)
p
ggplotly(p) %>%
# layout(autosize = FALSE) %>% # did not work
layout(xaxis = list(autorange = "reversed",
tickvals = c(2, 3, 5, 6),
ticktext = c("2", "", "5", "6")))
)
# Return a Shiny app object
shinyApp(ui = ui, server = server)
由reprex package (v2.0.1) 于 2021-09-06 创建
【问题讨论】:
【参考方案1】:我跳过了闪亮的部分,因为这个问题也以纯情节为主。问题似乎是空字符串。您可以考虑提交错误报告。
p <- ggplot(mpg, aes(x = displ, y = hwy, color = factor(cyl), shape = class)) +
geom_point(size = 3) +
scale_color_manual(values = c("4" = "#abd9e9",
"5" = "#2c7bb6",
"6" = "#ff7f00",
"8" = "#d7191c")) +
scale_shape_manual(values = c(2, 5, 16, 17, 18, 19, 25)) +
guides(color = guide_legend(title = "Cyl", order = 1),
shape = guide_legend(title = "", order = 2)) +
theme_classic(base_size = 16)
p
ggplotly(p) %>%
layout(xaxis = list(autorange = "reversed",
tickvals = c(2, 3, 5, 6),
# empty strings cause trouble, just include a space
ticktext = c("2", " ", "5", "6")))
【讨论】:
很棒的发现。感谢您的帮助!以上是关于从 ggplot2 转换为 plotly 后激活或停用图例键时绘图缩小或扩展的主要内容,如果未能解决你的问题,请参考以下文章
R语言使用hexSticker包将ggplot2包可视化的结果转换为六角图(六角贴六角形贴纸ggplot2 plot to hex sticker)
R语言ggplot2可视化哑铃图强调从一个点到另一个点的变化数量的变化客户满意度的变化等(Dumbbell Plot)为可视化图像添加标题题注信息
R语言ggplot2可视化:使用R原生plot函数为指定曲线下面的区域着色ggplot2可视化在曲线的特定下方添加分割线ggplot2为指定曲线下面的区域着色
R语言plotly可视化:plotly可视化基本散点图(指定图像类型模式)plotly可视化散点图(为不同分组数据配置不同的色彩)ggplotly使用plotly包呈现ggplot2的可视化结果