r - 为什么 ggplotly 会丢失我的 scale_size_continuous 图例?
问题描述
我正在开发一个闪亮的应用程序来使用 ggplot2 交互式显示一个气泡图,我想向它添加绘图功能。以下为可重现的示例创建数据:
# Load packages
library(shiny)
library(shinythemes)
library(tidyverse)
# create data
Name <- c(rep(c("Red Pine", "Sugar Maple"), each = 125))
mean.SDI <- c(rnorm(250, 150, 150))
mean.SDI <- mean.SDI + abs(min(mean.SDI))
DI <- c(rep(c(rep(c(1:5), 25)), 2))
PI <- c(rep(c(rep(c(1:5), each = 25)), 2))
GrowthRate <- c(rnorm(250, 2.5, 1))
GrowthRate[GrowthRate < 0] <- 0
n <- as.integer(runif(250, min = 1, max = 50))
trend_data <- tibble(Name, mean.SDI, DI, PI, GrowthRate, n)
以下不使用 plotly 的脚本会产生所需的结果:
# Define UI
ui <- fluidPage(theme = shinytheme("lumen"),
titlePanel("Growth rates"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "Species", label = strong("Species"),
choices = unique(trend_data$Name)[order(unique(trend_data$Name))],
selected = "Red Pine"),
sliderInput(inputId = "SDI", label = strong("Stand density index"),
min = 0, max = 1100, value = c(0, 800), dragRange = TRUE)
),
mainPanel(
fluidRow(column(6, plotOutput(outputId = "bubbleplot", height = "400px", width = "600px"))
)
)
)
)
# Define server function
server <- function(input, output) {
# Subset data
DI_PI <- reactive({
trend_data %>%
filter(
Name == input$Species,
mean.SDI >= input$SDI[1] & mean.SDI <= input$SDI[2]
) %>%
group_by(DI, PI) %>%
summarize(GrowthRate = mean(GrowthRate),
n = as.numeric(sum(n))) %>%
mutate(nAlpha = n > 50)
})
# Create scatterplot object the plotOutput function is expecting
output$bubbleplot <- renderPlot({
p <- ggplot(DI_PI(), aes(x = DI, y = PI, size = GrowthRate, alpha = nAlpha)) +
geom_point(col = '#236AB9') +
xlab("DI Class") +
ylab("PI Class") +
coord_cartesian(xlim = c(1, 5), ylim = c(1,5)) +
scale_size_continuous(name = "Subplot-level Growth Rate \n (ft2 per acre per year)",
range = c(0.1, 15)) +
scale_alpha_discrete(labels = c("Less than 50 subplots", "At least 50 subplots"),
name = "")
print(p)
})
}
# Create Shiny object
shinyApp(ui = ui, server = server)
但是,当我添加情节时(因为我希望情节是交互式的),我失去了大小图例:
# Define UI
ui <- fluidPage(theme = shinytheme("lumen"),
titlePanel("Growth rates"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "Species", label = strong("Species"),
choices = unique(trend_data$Name)[order(unique(trend_data$Name))],
selected = "Red Pine"),
sliderInput(inputId = "SDI", label = strong("Stand density index"),
min = 0, max = 1100, value = c(0, 800), dragRange = TRUE)
),
mainPanel(
fluidRow(column(6, plotlyOutput(outputId = "bubbleplot", height = "400px", width = "600px"))
)
)
)
)
# Define server function
server <- function(input, output) {
# Subset data
DI_PI <- reactive({
trend_data %>%
filter(
Name == input$Species,
mean.SDI >= input$SDI[1] & mean.SDI <= input$SDI[2]
) %>%
group_by(DI, PI) %>%
summarize(GrowthRate = mean(GrowthRate),
n = as.numeric(sum(n))) %>%
mutate(nAlpha = n > 50)
})
# Create scatterplot object the plotOutput function is expecting
output$bubbleplot <- renderPlotly({
p <- ggplot(DI_PI(), aes(x = DI, y = PI, size = GrowthRate, alpha = nAlpha)) +
geom_point(col = '#236AB9') +
xlab("DI Class") +
ylab("PI Class") +
coord_cartesian(xlim = c(1, 7), ylim = c(1,5)) +
scale_size_continuous(name = "Subplot-level Growth Rate \n (ft2 per acre per year)",
range = c(0.1, 15)) +
scale_alpha_discrete(labels = c("Less than 5 subplots", "At least 5 subplots"),
name = "")
ggplotly(p)
})
}
# Create Shiny object
shinyApp(ui = ui, server = server)
ggplotly 似乎在显示图例方面存在问题。有没有人知道对于“scale_size”类型的传说有一个很好的解决方案?
使用 RStudio 版本 1.1.453 和 plotly v 4.8.0
R info: platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 5.0
year 2018
month 04
day 23
svn rev 74626
language R
version.string R version 3.5.0 (2018-04-23) 昵称 Joy在玩
********编辑:尝试删除“alpha”美学以限制只有一个图例(对于“大小”),但现在根本没有绘制图例:
# EDIT server function
server <- function(input, output) {
# Subset data
DI_PI <- reactive({
trend_data %>%
filter(
Name == input$Species,
mean.SDI >= input$SDI[1] & mean.SDI <= input$SDI[2]
) %>%
group_by(DI, PI) %>%
summarize(GrowthRate = mean(GrowthRate),
n = as.numeric(sum(n))) %>%
mutate(nAlpha = n > 50)
})
# Create scatterplot object the plotOutput function is expecting
output$bubbleplot <- renderPlotly({
p <- ggplot(DI_PI(), aes(x = DI, y = PI,
#alpha = nAlpha,
size = GrowthRate)) +
geom_point(col = '#236AB9') +
xlab("DI Class") +
ylab("PI Class") +
coord_cartesian(xlim = c(1, 7), ylim = c(1,5)) +
scale_size_continuous(name = "Subplot-level Growth Rate \n (ft2 per acre per year)",
range = c(0.1, 15)) #+
#scale_alpha_discrete(labels = c("Less than 5 subplots", "At least 5 subplots"),
#name = "")
ggplotly(p)
})
}
# Create Shiny object
shinyApp(ui = ui, server = server)
解决方案
推荐阅读
- vpn - 是否可以通过IP地址访问谷歌搜索?
- prolog - 在 eplex 库中是否对模运算有任何明确的支持
- php - 在 PHP 中打印所有定义的变量而没有换行符
- java - 为什么我的桌子没有在我的卡片中滚动?
- r - 如何从以字符形式给出的日期中提取“月份”?
- google-sheets - 在 Flutter / Dart 中阅读 Google 表格
- excel - 尝试设置工作簿时出现 Vba 错误 - Excel
- php - 如何使用存储的数据显示选中的复选框
- java - java.io.IOException:文件名、目录名或卷标语法不正确
- python - 使用 Python 发送电子邮件