闪亮的应用程序无法使用ggplots选择输入级别

时间:2015-10-02 10:57:41

标签: r ggplot2 levels shiny

我很难在水平上对闪亮的情节(ggplots)进行分组,但没有找到任何帮助的例子...... 我希望能够选中带有复选框的x和y级别,并在选定的级别上进行绘图。

这是我数据的一个子集:

Jahr <- c(2014,2015,2020)
RW<- c(0,45,80)
Aus<- c(0,10,80)
Revit<- c(0,10,50)
Point_size<-c(0,30,0)
df3 <- data.frame(Jahr,RW,Aus,Revit,Point_size)
df3_melt <- melt(df3 , id=c("Jahr","Point_size")) 

我的ui:

ui <- fluidPage(
headerPanel('Gewaesser'),
sidebarPanel(
checkboxGroupInput("jahr", "Jahr", choices = levels(df3_melt$Jahr)),
checkboxGroupInput("thema", "Thema", choices = levels(df3_melt$variable)),
mainPanel(
  plotOutput('plot1')
)
))

和服务器:

server <- function(input, output) {

 #   selectedData <- reactive({
 #     all[,levels(df3_melt$variable]
 #   })
output$plot1 <- renderPlot({
ggplot(df3_melt, aes(x=input$jahr,y=value)) +
  geom_line(aes(group=input$thema,         color=input$thema),size=0.5,linetype="dashed")+ 
  geom_segment(aes(x = 2014, y = 0, xend = 2015, yend = 45),colour="red",size=0.4)+ #ist
  geom_segment(aes(x = 2014, y = 0.5, xend = 2015, yend = 10.5),colour="green",size=0.4)+ #ist
  geom_segment(aes(x = 2014, y = 0, xend = 2015, yend = 9.8),colour="blue",size=0.4)+ #ist
  coord_cartesian(ylim = c(0,101),xlim=c(2014,2020.5))+
  scale_x_continuous(breaks=c(2014,2015, 2016, 2017,2018,2019,2020), labels=c("Baseline","GJ 2015","GJ 2016","GJ 2017","GJ 2018","GJ 2019","Sollwert 2020"),minor_breaks=seq(2015, 2019, by=2))+
  geom_segment(aes(x = 2020, y = 0, xend = 2020, yend = 100,alpha=0.5),colour="chartreuse3",size=4)+ #Zielwert
  geom_segment(aes(x = 2015, y = 0, xend = 2015, yend = 100),colour="black",size=0.5)+
  theme(plot.background = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        panel.border = element_blank(),
        panel.background = element_rect(color="white",fill="white"),
        axis.text.x = element_text(size=6, angle=45, hjust=1, color="black"),
        axis.text.y = element_text(size=6,  hjust=1, color="black"),
        legend.title = element_blank(),
        #legend.key.size=unit(1),
        legend.key = element_rect(colour = 'white', fill = 'white', size = 0.5)
  )+
  guides(fill = guide_legend(label.position = "bottom"))
 })

 }

帮助将非常感激,闪亮的外观令人惊叹,但水平棘手...... 感谢

2 个答案:

答案 0 :(得分:1)

非常感谢你的帮助,我刚刚找到了解决方案,唯一的问题仍然是,它没有同时绘制2个级别......

 ui<-shinyUI(pageWithSidebar(
headerPanel('Gewässer'),
sidebarPanel(
checkboxGroupInput("var", "Indikatoren auswählen zu A1", choice = levels(df3_melt$variable))
),
 mainPanel(
 plotOutput("plot2")
)
))


server<-shinyServer(function(input, output, session) {
output$plot2 <- renderPlot({
par(mar = c(5.1, 4.1, 0, 1))
ggplot(df3_melt[df3_melt$variable == input$var, ],  aes_string(x="Jahr",y="value")) +
  geom_line(aes_string(group="variable", color="variable"),size=0.5,linetype="dashed")
})

 })
  shinyApp(ui = ui, server = server)

答案 1 :(得分:0)

使用aes_string(x = input$jahr, y = "value")代替aes(x=input$jahr,y=value)