我如何在 R studio 中分层比较两个 ggplots(换句话说,两个图相互重叠并通过我的输入进行比较)?
how can I compare between two ggplots in R studio in layers ( in other words having two plots over each other and compare them through my inputs )?
我有两个输出图,我想让它们彼此重叠,这样我就可以通过选定的输入对它们进行比较input$sel
和 input$sel2
.
我的意思是和在彼此之上是一个应该在背景中另一个应该是另一个层在它上面是透明的,这样可以同时看到两个图。
如果需要数据,这里是 data.csv 文件:
https://impfdashboard.de/static/data/germany_vaccinations_by_state.tsv
server <- function(input, output, session) {
#Summarize Data and then Plot
data <- reactive({
req(input$sel)
df <- germany_vaccinations_k %>% group_by(code) %>% summarise( output = get(input$sel))
print(df)
})
data2 <- reactive({
req(input$sel2)
df <- germany_vaccinations_k %>% group_by(code) %>% summarise( output = get(input$sel2))
print(df)
})
#Plot 1
output$plot <- renderPlot({
g <- ggplot( data(), aes( y = output ,x = code ,ho = factor(code) ) )
g + geom_bar( stat = "sum" )
output$plot2 <- renderPlot({
g <- ggplot( data(), aes( y = output ,x = code ,ho = factor(code) ) )
g + geom_bar( stat = "sum" )
})
ui <- basicPage(
#first-input
selectInput(inputId = "sel", label = "Möglichkeit auswählen",
list("vaccinationsTotal","peopleFirstTotal","peopleFullTotal","peopleBoosterTotal")),
#second-input
selectInput(inputId = "sel2", label = "Möglichkeit auswählen",
list("vaccinationsTotal","peopleFirstTotal","peopleFullTotal","peopleBoosterTotal")),
#the both outputs
plotOutput("plot")
plotOutput("plot2")
)
我们可以结合这两个情节并使用 alpha
参数。我添加了两个滑块,使用户能够控制两个图的透明度级别。
output$plot <- renderPlot({
ggplot() +
geom_bar(data = data(), aes(y = output, x = code), stat = "sum", alpha = .5) +
geom_bar(data = data2(), aes(y = output, x = code), stat = "sum", alpha = .5, fill = "lightblue")
})
应用程序:
germany_vaccinations_k <- read_tsv("germany_vaccinations_by_state.tsv")
ui <- basicPage(
# first-input
selectInput(
inputId = "sel", label = "Möglichkeit auswählen",
list("vaccinationsTotal", "peopleFirstTotal", "peopleFullTotal", "peopleBoosterTotal")
),
# second-input
selectInput(
inputId = "sel2", label = "Möglichkeit auswählen",
list("vaccinationsTotal", "peopleFirstTotal", "peopleFullTotal", "peopleBoosterTotal")
),
sliderInput("alpha_sel", "Select First Alpha", min = .01, max = 1, value = 0.5),
sliderInput("alpha_sel2", "Select Second Alpha", min = .01, max = 1, value = 0.8),
# the both outputs
plotOutput("plot")
)
server <- function(input, output, session) {
# Summarize Data and then Plot
data <- reactive({
req(input$sel)
df <- germany_vaccinations_k %>%
group_by(code) %>%
summarise(output = get(input$sel))
print(df)
})
data2 <- reactive({
req(input$sel2)
df <- germany_vaccinations_k %>%
group_by(code) %>%
summarise(output = get(input$sel2))
print(df)
})
output$plot <- renderPlot({
ggplot() +
geom_bar(data = data(), aes(y = output, x = code), stat = "sum", alpha = input$alpha_sel) +
geom_bar(data = data2(), aes(y = output, x = code), stat = "sum", alpha = input$alpha_sel2, fill = "lightblue")
})
}
shinyApp(ui, server)
我有两个输出图,我想让它们彼此重叠,这样我就可以通过选定的输入对它们进行比较input$sel
和 input$sel2
.
我的意思是和在彼此之上是一个应该在背景中另一个应该是另一个层在它上面是透明的,这样可以同时看到两个图。
如果需要数据,这里是 data.csv 文件: https://impfdashboard.de/static/data/germany_vaccinations_by_state.tsv
server <- function(input, output, session) {
#Summarize Data and then Plot
data <- reactive({
req(input$sel)
df <- germany_vaccinations_k %>% group_by(code) %>% summarise( output = get(input$sel))
print(df)
})
data2 <- reactive({
req(input$sel2)
df <- germany_vaccinations_k %>% group_by(code) %>% summarise( output = get(input$sel2))
print(df)
})
#Plot 1
output$plot <- renderPlot({
g <- ggplot( data(), aes( y = output ,x = code ,ho = factor(code) ) )
g + geom_bar( stat = "sum" )
output$plot2 <- renderPlot({
g <- ggplot( data(), aes( y = output ,x = code ,ho = factor(code) ) )
g + geom_bar( stat = "sum" )
})
ui <- basicPage(
#first-input
selectInput(inputId = "sel", label = "Möglichkeit auswählen",
list("vaccinationsTotal","peopleFirstTotal","peopleFullTotal","peopleBoosterTotal")),
#second-input
selectInput(inputId = "sel2", label = "Möglichkeit auswählen",
list("vaccinationsTotal","peopleFirstTotal","peopleFullTotal","peopleBoosterTotal")),
#the both outputs
plotOutput("plot")
plotOutput("plot2")
)
我们可以结合这两个情节并使用 alpha
参数。我添加了两个滑块,使用户能够控制两个图的透明度级别。
output$plot <- renderPlot({
ggplot() +
geom_bar(data = data(), aes(y = output, x = code), stat = "sum", alpha = .5) +
geom_bar(data = data2(), aes(y = output, x = code), stat = "sum", alpha = .5, fill = "lightblue")
})
应用程序:
germany_vaccinations_k <- read_tsv("germany_vaccinations_by_state.tsv")
ui <- basicPage(
# first-input
selectInput(
inputId = "sel", label = "Möglichkeit auswählen",
list("vaccinationsTotal", "peopleFirstTotal", "peopleFullTotal", "peopleBoosterTotal")
),
# second-input
selectInput(
inputId = "sel2", label = "Möglichkeit auswählen",
list("vaccinationsTotal", "peopleFirstTotal", "peopleFullTotal", "peopleBoosterTotal")
),
sliderInput("alpha_sel", "Select First Alpha", min = .01, max = 1, value = 0.5),
sliderInput("alpha_sel2", "Select Second Alpha", min = .01, max = 1, value = 0.8),
# the both outputs
plotOutput("plot")
)
server <- function(input, output, session) {
# Summarize Data and then Plot
data <- reactive({
req(input$sel)
df <- germany_vaccinations_k %>%
group_by(code) %>%
summarise(output = get(input$sel))
print(df)
})
data2 <- reactive({
req(input$sel2)
df <- germany_vaccinations_k %>%
group_by(code) %>%
summarise(output = get(input$sel2))
print(df)
})
output$plot <- renderPlot({
ggplot() +
geom_bar(data = data(), aes(y = output, x = code), stat = "sum", alpha = input$alpha_sel) +
geom_bar(data = data2(), aes(y = output, x = code), stat = "sum", alpha = input$alpha_sel2, fill = "lightblue")
})
}
shinyApp(ui, server)