Mavic
Mavic

Reputation: 153

Applying theme to plot when using Shiny

I am trying to create a web app using Shiny any display some plots on it.

I have already created many plots in R Studio using ggplot. I am trying to display following plot on the web app I am creating.

enter image description here

My R Server code:

shinyServer(function(input, output){

output$bakePlot <- renderPlotly({
ggplot(sales_bakery, aes(ProductName, ProductSales))+ 
stat_summary(fun.y=sum,geom="bar",colour="red",fill="red",show.legend = FALSE) +
coord_cartesian(ylim = c(7000, 9500)) + ggtitle("January Sales in Bakery") + 
xlab("Category") + ylab("Quantity Sold")
})
})

UI code:

shinyUI(
dashboardPage(
dashboardHeader(title = "Dashboard"),
dashboardSidebar(
  menuItem("Categories"),
    menuSubItem("Cereals"),
    menuSubItem("Bakery"),
  menuItem("Products")
),
dashboardBody(
  fluidRow(
    box(plotlyOutput("bakePlot"))
  )
)
)
)

When I use this code I get following displayed on the web app.

enter image description here

My question is, how do I style this plot on the web app the same way it is in the first picture?

My theme code is:

bakeryMonthlyPlot <- bakeryMonthlyPlot +
  theme(axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_text(colour = "black", size = 14),
    axis.text.y = element_text(colour = "black", size = 14),
    panel.background = element_rect(fill = "white"),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    axis.line = element_line(colour = "black", size = 1),
    legend.position = "right",
    plot.title = element_text(lineheight = 1.8, face = "bold"))

Can someone tell me where I need to put my theme code in order to apply the theme to the plot on the web application?

Any help is greatly appreciated.

Upvotes: 0

Views: 976

Answers (1)

Mavic
Mavic

Reputation: 153

Yes @eipi10, I was able to do it by adding the theme to the bottom of the plot.

For anyone interested in this in the future, the code now looks like this:

shinyServer(function(input, output){

output$bakePlot <- renderPlotly({
ggplot(sales_bakery, aes(ProductName, ProductSales))+ 
stat_summary(fun.y=sum,geom="bar",colour="red",fill="red",show.legend = FALSE) +
coord_cartesian(ylim = c(7000, 9500)) + ggtitle("January Sales in Bakery") + 
xlab("Category") + ylab("Quantity Sold")+
  theme(
        axis.text.x = element_text(angle = 60, hjust = 1),
        axis.text.y = element_text(colour = "black", size = 14),
        panel.background = element_rect(fill = "white"),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        axis.line = element_line(colour = "black", size = 1),
        legend.position = "none",
        plot.title = element_text(lineheight = 1.8, face = "bold"))
})

Upvotes: 1

Related Questions