0

I want to plot a line graph based on user click input in a heatmap. I am able to do that but the y axis is not arranged properly and is too crowded. After the plot button is pressed the heatmap appears and when a point on the heatmap is pressed the data corresponding to the particular column in the heatmap is plotted as a line graph below it. The line graph looks like

y axis not properly ordered and the y axis is too crowded

But as you can see the y axis is disorganized and is too much crowded. So i wanted to break it and apply some limits.

My code is

    Comp_name <- c("Dum1")
Inc <- c(175.26,175.365,175.65,176.65,176.165,176.1685,175.56)
Exp <- c(175.48,174.53,174.165,173.1651,175.651,174.16541,176.65)
Date <- c(2018-06-01,2018-06-02,2018-06-03,2018-06-04,2018-06-05,2018-06-06,2018-06-07)
Dates <- c(2018-06-01,2018-06-02,2018-06-03,2018-06-04,2018-06-05,2018-06-06,2018-06-07)
Dummy1 <- as.data.frame(cbind(Comp_name,Inc,Exp,Date,Dates))
Comp_name1 <- c("Dum2")
Inc1 <- c(151.26,151.59,151.23,152.46,152.49,151.29,150.81)
Exp1 <- c(152.64,152.84,152.64,152.48,152.35,154.26,153.14)
Date1 <- c(2018-06-01,2018-06-02,2018-06-03,2018-06-04,2018-06-05,2018-06-06,2018-06-07)
Dates1 <- c(2018-06-01,2018-06-02,2018-06-03,2018-06-04,2018-06-05,2018-06-06,2018-06-07)
Dummy2 <- as.data.frame(cbind(Comp_name1,Inc1,Exp1,Date1,Dates1))
Comp_name2 <- c("Dum3")
Inc2 <- c(160.45,161.25,163.56,165.25,163.59,160.89,161.26)
Exp2 <- c(160.19,160.78,162.15,164.89,165.24,163.25,162.48)
Date2 <- c(2018-06-01,2018-06-02,2018-06-03,2018-06-04,2018-06-05,2018-06-06,2018-06-07)
Dates2 <- c(2018-06-01,2018-06-02,2018-06-03,2018-06-04,2018-06-05,2018-06-06,2018-06-07)
Dummy3 <- as.data.frame(cbind(Comp_name2,Inc2,Exp2,Date2,Dates2))
Comp_name3 <- c("Dum4")
Inc3 <- c(156.26,155.12,157.12,158.78,154.26,160.12,161.26)
Exp3 <- c(160.19,160.19,155.19,154.26,150.12,157.26,159.12)
Date3 <- c(2018-06-01,2018-06-02,2018-06-03,2018-06-04,2018-06-05,2018-06-06,2018-06-07)
Dates3 <- c(2018-06-01,2018-06-02,2018-06-03,2018-06-04,2018-06-05,2018-06-06,2018-06-07)
Dummy4 <- as.data.frame(cbind(Comp_name3,Inc3,Exp3,Date3,Dates3))
Data <- cbind(Dummy1,Dummy2,Dummy3,Dummy4)
Data <- as.data.frame(Data)



library(shiny)

ui <- fluidPage(
sidebarLayout(
sidebarPanel(),
mainPanel(selectInput("ploth","Heatmap", "Plot Heatmap Of", choices =c("Income" = "inc2",
                                                                   "Expenditure" = "exp2",
                                                                   "Gross Profit" = "gprofit2",
                                                                   "Net Profit" = "nprofit2")),
                       actionButton("hplotit","Plot Heatmap"),
                       plotlyOutput("HeatPlot"),
                       plotlyOutput("Next")
)    ))

server <- function(input,output,session) {
        observeEvent(input$hplotit, {
             inc1 <- as.data.frame(cbind(Dummy1 = Data[,2],Dummy2 = Data[,7],
                                         Dummy3 = Data[,12], Dummy4 = Data[,17]))
             inc2 <- as.matrix(inc1)
             exp1 <- as.data.frame(cbind(Dummy1 = Data[,3],Dummy2 = Data[,8],
                                         Dummy3 = Data[,13], Dummy4 = Data[,18]))
             exp2 <- as.matrix(exp1)
             gprofit1 <- as.data.frame(cbind(Dummy1 = Data[,3] - Data[,2],
                                            Dummy2 = Data[,8] - Data[,7],
                                            Dummy3 = Data[,13] - Data[,12],
                                            Dummy4 = Data[,18] - Data[,17]))
             gprofit2 <- as.matrix(gprofit1)
             nprofit1 <- as.data.frame(cbind(Dummy1 = (Data[,3] - Data[,2]) - ((Data[,3] - Data[,2]) * 0.06),
                                             Dummy2 = (Data[,8] - Data[,7]) - ((Data[,8] - Data[,7]) * 0.10),
                                             Dummy3 = (Data[,13] - Data[,12]) - ((Data[,13] - Data[,12]) * 0.18),
                                             Dummy4 = (Data[,18] - Data[,17]) - ((Data[,18] - Data[,17]) * 0.22)))
             nprofit2 <- as.matrix(nprofit1)
             date <- as.character(Data[,4])
             h <- input$ploth
             switch(EXPR = h ,
                    inc2 = output$HeatPlot <- renderPlotly( plot_ly(x = colnames(inc2), y = date,
                                                                    z = inc2, type = "heatmap",
                                                                    colorscale = "Earth")),

                    exp2 = output$HeatPlot <- renderPlotly( plot_ly(x = colnames(exp2), y = date,
                                                                    z = exp2, type = "heatmap", 
                                                                    colors = colorRamp(c("red",
                                                                                         "yellow")))),

                    gprofit2 = output$HeatPlot <- renderPlotly( plot_ly(x = colnames(gprofit2),
                                                                        y = date, z = gprofit2,
                                                                        type = "heatmap",
                                                                        colorscale="Greys")),

                    nprofit2 = output$HeatPlot <- renderPlotly( plot_ly(x = colnames(nprofit2),
                                                                        y = date, z = nprofit2,
                                                                        type = "heatmap")) 
             )       
    })

      output$Next <- renderPlotly({
        event.data <- event_data(event = "plotly_click")[["x"]]
        vars <- as.character(event.data)
        dateof <- event_data(event = "plotly_click")[["y"]]
        dates <- as.character(dateof)
        value <- event_data(event = "plotly_click")[["z"]]
        values <- as.character(value)
        inc1 <- as.data.frame(cbind(Dummy1 = Data[,2],Dummy2 = Data[,7],
                                    Dummy3 = Data[,12], Dummy4 = Data[,17]))
        inc2 <- as.matrix(inc1)
        exp1 <- as.data.frame(cbind(Dummy1 = Data[,3],Dummy2 = Data[,8],
                                    Dummy3 = Data[,13], Dummy4 = Data[,18]))
        exp2 <- as.matrix(exp1)
        gprofit1 <- as.data.frame(cbind(Dummy1 = round(Data[,3] - Data[,2],2),
                                        Dummy2 = round(Data[,8] - Data[,7],2),
                                        Dummy3 = round(Data[,13] - Data[,12],2),
                                        Dummy4 = round(Data[,18] - Data[,17],2)))
        gprofit2 <- as.matrix(gprofit1)
        nprofit1 <- as.data.frame(cbind(Dummy1 = round((Data[,3] - Data[,2]) - ((Data[,3] - Data[,2]) * 0.06),2),
                                        Dummy2 = round((Data[,8] - Data[,7]) - ((Data[,8] - Data[,7]) * 0.10),2),
                                        Dummy3 = round((Data[,13] - Data[,12]) - ((Data[,13] - Data[,12]) * 0.18),2),
                                        Dummy4 = round((Data[,18] - Data[,17]) - ((Data[,18] - Data[,17]) * 0.22),2)))
        nprofit2 <- as.matrix(nprofit1)
        h <- input$ploth
        did <- cbind(Date = (as.character(Data[,4])),get(h))
        mini <- as.numeric(min(levels(Data[,vars]))) - 1
        maxi <- as.numeric(max(levels(Data[,vars]))) + 1
        if(is.null(event.data)) NULL else ggplotly(ggplot(as.data.frame(did[,vars])) + 
                                                     geom_line(aes(x = Data[,4], y = did[,vars], group = 1 ), stat = "identity", col = "blue")+
                                                     geom_point(aes(x = Data[,4], y = did[,vars]), stat = "identity", col = ifelse(Data[,4] == dates, "green", "darkmagenta"))+
                                                     theme(axis.text.x = element_text(angle = 90))+ xlab("Dates")+ylab("(in lakhs)")+
                                                     scale_y_continuous(limits = c(mini,maxi), breaks = seq(mini,maxi,1)))
          })

      }

}                       


# Run the application 
shinyApp(ui = ui, server = server)

With the error being somewhere in

mini <- as.numeric(min(levels(Data[,vars]))) - 1
maxi <- as.numeric(max(levels(Data[,vars]))) + 1
if(is.null(event.data)) NULL else ggplotly(ggplot(as.data.frame(did[,vars])) + 
                                                         geom_line(aes(x = Data[,4], y = did[,vars], group = 1 ), stat = "identity", col = "blue")+
                                                         geom_point(aes(x = Data[,4], y = did[,vars]), stat = "identity", col = ifelse(Data[,4] == dates, "green", "darkmagenta"))+
                                                         theme(axis.text.x = element_text(angle = 90))+ xlab("Dates")+ylab("(in lakhs)")+
                                                         scale_y_continuous(limits = c(mini,maxi), breaks = seq(mini,maxi,1)))

What is the error. My computer gives an error like

Error in [.data.frame: undefined columns selected

Or

Error in : Discrete value supplied to continuous scale

Please help. Thank you.

This is not a duplicate of previous question because the actual code has been slightly altered to produce a minimal reproducible code. The actual code has file.choose() functions and then the cbind has been done to bring it all into one larger data frame. So is there a way to solve the question without removing the cbind function?

rahul yadav
  • 432
  • 3
  • 20

1 Answers1

0

Your use of cbind is the problem here. Because your "Dates" column has non-numeric characters (the dashes), cbind converts every column to the character data type, which results in the plotting problem you see. cbind wants every column to have the same type, whereas data.frame supports columns of heterogenous types.

So, for example, the line that creates Dummy1 should be:

Dummy1 <- as.data.frame(Comp_name,Inc,Exp,Date,Dates)

And your Data object should probably be a list.

jdobres
  • 11,339
  • 1
  • 17
  • 37