Converting Hist base plot to ggplot

I am trying to convert a Hist plot to a ggplot in shiny. The current Hist function is as follows:
Hist<-function(var, lab) {h<-hist(var, main=lab, col=farben[1],
xlab=paste0("Mean = ", signif(mean(var, na.rm=T), 3), ", sd = ", signif(sd(var, na.rm=T), 3)))
}

I tried to define a ggplot equivalent function for this:
GGhist<-function(var, mapping, lab) {ggplot() +
geom_histogram(var=var, mapping=mapping) +
xlab(paste0("Mean = ", signif(mean(var, na.rm=T), 3), ", sd = ", signif(sd(var, na.rm=T), 3)))
}

This functions are to be called in a shiny server as follows:

output$plotResult <- renderPlot({
if(length(subset())==0){
noplotatall()
}
else{
switch (input$selectVisualization,
"plot1_hist" = Hist(data1(), Trans(input$selectedVariable1)),
"plot1_gghist" = GGhist(data3(), aes(x=Allg_RckEuro), Trans(input$selectedVariable1)),
)
}

However, I could not figure out how i can define my x and y aes mapping either in the function or in the shiny server. The current error I get when i try the above is:
Error: object 'Allg_RckEuro' not found

This is what the shiny server environment looks like:

shinyServer(function(input, output, session) {

DEFINITIONS

rv <- reactiveValues()
rv$initialized <- FALSE

filterData <- function(column){
isolate ({
filtNo <- which(names(rv$filters) == column)
filt <- input[[paste0('filter', filtNo)]]
if (is.null(filt))
filt <- defaultFilterRange(column)
else {
if (is.numeric(dataset[[column]]) && isDate(column))
filt <- c(as.numeric(as.character(filt[1],"%Y%m%d")), as.numeric(as.character(filt[2],"%Y%m%d")))
}
if (is.numeric(dataset[[column]]))
return(which(unlist(dataset[column]) >= filt[1] & unlist(dataset[column]) <= filt[2]))
else
return(which(is.element(lapply(as.character(unlist(dataset[[column]])),Trans), filt)))
})
}

subset <- reactive({
newSubset <- 1:nrow(dataset)
for (filter in (active <- which(rv$filters==TRUE))) {
input[[paste0('filter', filter)]]
column <- names(rv$filters)[filter]
filtersubset <- filterData(column)
newSubset <- intersect(newSubset, filtersubset)
}
return(newSubset)
})

data1 <- reactive({
req(input$selectedVariable1)
if (is.numeric(dataset[[input$selectedVariable1]]))
return(dataset[subset(), input$selectedVariable1])
else
return(droplevels(dataset[subset(), input$selectedVariable1]))
})

data2 <- reactive({
req(input$selectedVariable2)
if (is.numeric(dataset[[input$selectedVariable2]]))
return(dataset[subset(), input$selectedVariable2])
else
return(droplevels(dataset[subset(), input$selectedVariable2]))
})
data3 <- reactive({
req(input$selectedVariable1)
if (is.numeric(dataset[[input$selectedVariable1]]))
return(dataset[subset(), input$selectedVariable1])
else
return(droplevels(dataset[subset(), input$selectedVariable1]))
})

Can i get help on this?

you need to respect each package argument and way of using it I hope the below get you started.

library(ggplot2)

Hist<-function(var, lab) {h<-hist(var, main=lab, col="red",
                                  xlab=paste0("Mean = ", signif(mean(var, na.rm=T), 3), ",
                                              sd = ", signif(sd(var, na.rm=T), 3)))
}

  GGhist<-function(data,var) {
    ggplot(data=data) +
    geom_histogram( aes_string(x=var),fill="red",bins =  5,color="black") +
      xlab(paste0("Mean = ", signif(mean(data[[var]], na.rm=T), 3),
                  ", sd = ", signif(sd(data[[var]], na.rm=T), 3)))
  }
  Hist (mtcars$mpg,"mpg"  )
  GGhist (data=mtcars, var= "mpg")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.