Hello I have a simple shiny app in which I create a scatterplot of variables found in the iris dataset. What I want is to be able to modify the plot in some ways. First of all I would like to be able to set the limits of the gridlines for each axis(by 1 for example), then set the background color to white and the marker color to blue and add trendline.
Preferred styles: Plot title - Calibri (or similar), 10 pt, bold,
dark greyAxis titles – Calibri light (or similar), 16 pt, bold, dark grey
Axis number labels – Calibri, 11 pt
- Data labels – Calibri, 9 pt, black
- Data markers – dark
blue circles
I do not know if all of these or maybe some are available in ggplot2 in combination with plotly or I have to use only plotly as Im new to it and would like some guidance.Thanks
library(shiny)
library(ggplot2)
library(plotly)
fluidPage(
# App title ----
titlePanel(div("CROSS CORRELATION",style = "color:blue")),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
),
# Main panel for displaying outputs ----
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Correlation Plot",
fluidRow(
column(3, uiOutput("lx1")),
column(3,uiOutput("lx2"))),
plotlyOutput("sc"))
))
))
function(input, output) {
output$lx1<-renderUI({
selectInput("lx1", label = h4("Select 1st Expression Profile"),
choices = colnames(iris[,1:4]),
selected = "Lex1")
})
output$lx2<-renderUI({
selectInput("lx2", label = h4("Select 2nd Expression Profile"),
choices = colnames(iris[,1:4]),
selected = "Lex2")
})
output$sc<-renderPlotly({
p <- ggplot(iris, aes_string(x = input$lx1, y = input$lx2)) +
geom_point()
ggplotly(p) %>%
layout(height = 400)
})
}
Answer
Alright, sorry for the delayed response, but here's a working example that changes all the features you requested. It was a bit more involved than I lead on... Calibri isn't a font that is built into R, so it needs to be loaded in using font extra package. Then, most of the options you wanted to change were simple to do within theme() arguments, but the hover text needed to be changed within the plotly call options. Hope this example helps with your code!
## Note: extrafont is a bit finnicky on Windows,
## so be sure to execute the code in the order
## provided, or else ggplot won't find the font
# Use this to acquire additional fonts not found in R
install.packages("extrafont");library(extrafont)
# Warning: if not specified in font_import, it will
# take a bit of time to get all fonts
font_import(pattern = "calibri")
loadfonts(device = "win")
# Load the packages
library(ggplot2)
library(plotly)
# Just use cars data frame
p <- ggplot(cars, aes(x = speed, y = dist)) +
# Change the point options in geom_point
geom_point(color = "darkblue") +
# Change the title of the plot (can change axis titles
# in this option as well and add subtitle)
labs(title = "Distance vs Speed") +
# Change where the tick marks are
scale_x_continuous(breaks = seq(0, 30, 2.5)) +
# Change how the text looks for each element
theme(title = element_text(family = "Calibri",
size = 10,
face = "bold"),
axis.title = element_text(family = "Calibri Light",
size = 16,
face = "bold",
color = "darkgrey"),
axis.text = element_text(family = "Calibri",
size = 11))
ggplotly(p) %>%
layout(hoverlabel = list(bgcolor = "white",
font = list(family = "Calibri",
size = 9,
color = "black")))
No comments:
Post a Comment