我正在开发一个Shiny应用程序,以使用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)
以下不使用脚本的脚本会产生所需的结果:
# 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信息:
平台x86_64-w64-mingw32
拱形x86_64
os mingw32
系统x86_64,mingw32
状态
专业3
次要5.0
2018年
第04个月
第23天
svn版本74626
语言R
version.string R版本3.5.0(2018-04-23)
昵称“玩耍中的欢乐”
********编辑:尝试删除“字母”美学,以仅限制一个图例(“大小”),但现在根本没有图例:
# 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)