堆疊溢位的新功能。如果這很混亂,我很抱歉。對于這個問題,我快要失去理智了。。
我正在嘗試創建一個 ggplot 折線圖,該折線圖的顏色基于分組變數.. 所以一條線但顏色不同。
當我在控制臺中運行代碼時,圖表看起來符合預期。但是,當我運行閃亮的應用程式時,它似乎完全忽略了 group=1 引數,并將組分成 2 行不同的行。
這是一個供參考的代表:
library(shiny)
library(ggplot2)
library(plotly)
library(tidyverse)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("Testing Plotly and GGplot"),
sidebarLayout(
sidebarPanel(
),
# Show a test plot
mainPanel(
fluidRow(
width = 7,
plotly::plotlyOutput("test_plot")
)
)
)
)
# Define server logic
server <- function(input, output) {
#initialize dataframe
test_data <- data.frame(Date = as.Date(c("2022-03-24", "2022-3-25", "2022-03-29", "2022-03-30")),
count = c(10, 14, 8, 11),
week_identifier = c("0", "0", "1", "1"))
output$test_plot <- plotly::renderPlotly({
a <- ggplot2::ggplot(test_data, ggplot2::aes(Date, count, color = week_identifier, group =1))
ggplot2::geom_line()
ggplot2::geom_point()
print(a)
plotly::ggplotly(a) %>%
plotly::config(displaylogo = FALSE) %>%
plotly::config(modeBarButtonsToRemove = c("select2d", "lasso2d"))
})
}
# Run the application
shinyApp(ui = ui, server = server)
uj5u.com熱心網友回復:
在您閃亮的應用程式中,您正在繪制而不是 ggplot:這是 ggplot 的單獨渲染:
library(shiny)
library(ggplot2)
library(plotly)
library(tidyverse)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("Testing Plotly and GGplot"),
sidebarLayout(
sidebarPanel(
),
# Show a test plot
mainPanel(
fluidRow(
width = 7,
plotOutput("a"),
plotly::plotlyOutput("test_plot")
)
)
)
)
# Define server logic
server <- function(input, output) {
#initialize dataframe
test_data <- data.frame(Date = as.Date(c("2022-03-24", "2022-3-25", "2022-03-29", "2022-03-30")),
count = c(10, 14, 8, 11),
week_identifier = c("0", "0", "1", "1"))
output$a <- renderPlot(
ggplot(test_data, aes(Date, count, color = week_identifier, group =1))
geom_line()
geom_point()
)
output$test_plot <- plotly::renderPlotly({
a <- ggplot(test_data, aes(Date, count, color = week_identifier, group =1))
geom_line()
geom_point()
a
plotly::ggplotly(a) %>%
plotly::config(displaylogo = FALSE) %>%
plotly::config(modeBarButtonsToRemove = c("select2d", "lasso2d"))
})
}
# Run the application
shinyApp(ui = ui, server = server)

uj5u.com熱心網友回復:
我只想在資料中添加一個新行。新行應該使第一組在第二組開始的地方結束。
test_data <- data.frame(
Date = as.Date(c("2022-03-24", "2022-3-25", "2022-03-29", "2022-03-30")),
count = c(10, 14, 8, 11),
week_identifier = c("0", "0", "1", "1")
) %>%
add_row(Date = as.Date('2022-03-29'), count = 8, week_identifier = '0')
uj5u.com熱心網友回復:
我還在 RStudio 社區上發布了這個: https ://community.rstudio.com/t/shiny-ggplot-output-different-wrong-when-using-group-1/133059/2
有一個使用 geom_segment 而不是 geom_line 的解決方案:
a <- ggplot2::ggplot(test_data, ggplot2::aes(Date, count, color = week_identifier))
ggplot2::geom_segment(aes(xend = lead(Date), yend = lead(count)))
ggplot2::geom_point()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/452998.html
