Hi!
I would like to show a plot on my shinyApp. I had it working, but had to make some adjustments to the function that creates the actual ggplot. The function works perfectly well by itself in a separate script, but throws an error when called by renderPlot.
In the shiny app the call is:
output$plot <- renderPlot(generatePlot(completeOutputTable))
The error is:
Warning: Error in : Aesthetics must be either length 1 or the same as the data (4): x, ymin, ymax
Stack trace (innermost first):
107: check_aesthetics
106: f
105: l$compute_aesthetics
104: f
103: by_layer
102: ggplot2::ggplot_build
101: print.ggplot
100: print
89: <reactive:plotObj>
78: plotObj
77: origRenderFunc
76: output$plot
1: runApp
Warning: Error in : Aesthetics must be either length 1 or the same as the data (4): x, ymin, ymax
Stack trace (innermost first):
80: <Anonymous>
79: stop
78: plotObj
77: origRenderFunc
76: output$plot
1: runApp
The code for generatePlot is:
generatePlot <- function(table){
print("in generate plot")
titleName <- paste("LOR Comparison")
aesthetics <- aes(
x=table$id,
y=table$`Confidence Interval (95%)`,
ymin=table$lb,
ymax=table$ub,
color=table$`Comparator Drug`
)
plotTheme <- theme(axis.text.x = element_text(angle =0, vjust = 1,size =14,
hjust = 0.5),
legend.text=element_text(size=12),
legend.position = "bottom",
panel.background = element_rect(fill = 'grey',color='black'),
plot.title = element_text(size = 12, hjust = 0.5),
aspect.ratio=0.7)
lorPlot <- ggplot(table, aesthetics) +
geom_pointrange(size=1,shape=4,stroke=2) +
geom_hline(yintercept = 0,size=1) +
ggtitle(titleName) +
plotTheme +
coord_flip() +
scale_y_continuous(limits = c(-2,6), breaks=seq(-2,6, by=1)) +
scale_x_continuous(labels = paste(table$`Investigatory Drug`,
table$`Invest. Dose (mg)`,
"mg")) +
labs(x = "Investigatory Dose", y = "Investigatory LOR vs Comparator") +
labs(color = "Comparator drugs:") # the label for color
return(lorPlot)
}
I would like to understand what the difference is between calling the function on its own in a separate script and what goes wrong when I am incorporating it into the shinyApp.
Thanks for helping.