I having trouble with adding states’ names into the graph. I refered to this plotly website for plotting. The last map in the this website which has text “Africa” on the graph,is what I want to achieve:
https://plotly.com/r/choropleth-maps/
Please help me with this issue. I would deeply appreciate!
library(shiny)
library(ggplot2)
library(openxlsx)
library(readxl)
library(dplyr)
library(tidyr)
library(shinyBS)
library(shinyWidgets)
library(bslib)
library(plotly)
library(tidyr)
library(scales)
extended_data <- data.frame(
Date = c(
"8/13/24", "8/13/24", "8/13/24", "8/13/24", "8/13/24", "8/13/24", "8/13/24", "8/13/24", "8/13/24", "8/13/24",
"8/13/24", "8/13/24", "8/13/24", "8/13/24", "8/13/24", "8/13/24", "8/13/24", "8/13/24", "8/14/24", "8/14/24"
),
Type = c("D", "B", "M", "F", "D", "B", "M", "F", "D", "B", "M", "F", "D", "B", "M", "F", "D", "B", "M", "F"),
state = c(
"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA",
"HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD"
),
value = c(
1, 1, 0, 1, 0, 0, 0, 0, 1, 0,
0, 1, 0, 1, 1, 1, 1, 1, 0, 0
),
party = c(
"Republican", "Republican", "Democratic", "Republican", "Democratic", "Democratic", "Democratic", "Democratic", "Republican", "Democratic",
"Democratic", "Republican", "Democratic", "Republican", "Republican", "Republican", "Republican", "Republican", "Democratic", "Democratic"
),
Democratic = rep(0.50326087, 20), # Simplifying for the example
StateFull = c(
"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia",
"Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland"
),
Percent_byStateParty = c(
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 0, 0, 1, 1
),
abb = c(
"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA",
"HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD"
),
party2 = c(
"Republican", "Republican", "Republican", "Republican", "Republican", "Republican", "Republican", "Republican", "Republican", "Democratic",
"Democratic", "Democratic", "Democratic", "Democratic", "Democratic", "Democratic", "Democratic", "Democratic", "Democratic", "Democratic"
)
)
# Define UI for application that draws a histogram
# Define UI for application that draws a histogram
ui <- fluidPage(
# theme = bslib::bs_theme(primary = "orange"),
# Define UI for application that draws a histogram
tabPanel("US Map",
icon = icon("map-marker"),
titlePanel("Map2"),
# Organize elements horizontally using fluidRow and column
fluidRow(
column(
4,
fileInput("file2", "Choose your CSV File", accept = ".csv")
),
column(
4,
radioButtons("voicechoice", "Choice of Voice",
choices = c("D", "F", "B", "M"),
selected = "D"
),
bsTooltip(
id = "voicechoice",
title = "Select type"
)
),
fluidRow(
column(
4,
dateInput("Date_map", "Date:", value = "2024-08-13"),
pickerInput(
inputId = "USMap_state",
label = "Select States (Max 4)",
choices = state.name, # Make sure averaged_data$Type is available
multiple = TRUE,
options = list(
maxItems = 4, placeholder = "Enter state name",
onInitialize = I('function() { this.setValue(""); }')
)
)
) # Close column
),
fluidRow(
column(
12,
plotlyOutput(outputId = "Plot_Map")
)
),
)
), #
)
# View(averaged_data)
server <- function(input, output) {
## ----------
USA_map <- reactive({
req(input$voicechoice)
req(input$Date_map)
filter(extended_data, Type %in% input$voicechoice) %>%
filter(Date %in% input$Date_map) %>%
mutate(new_variable = runif(n(), min = 0, max = 50)) %>%
mutate(party_numeric = ifelse(party2 == "Republican", 1, 0)) %>%
mutate(hover = paste(
state, "<br>",
"State:", state, "<br>",
"Date:", Date, "<br>",
"Type:", Type, "<br>",
" Party:", party2
))
})
output$Plot_Map <- renderPlotly({
input$voicechoice
input$Date_map
# specify some map projection/options
g <- list(
scope = "usa",
projection = list(type = "albers usa"),
showlakes = TRUE,
lakecolor = toRGB("white")
)
fig <- plot_geo(USA_map(), locationmode = "USA-states")
fig <- fig %>% add_trace(
z = ~party_numeric, text = ~hover, locations = ~state
)
fig <- fig %>% colorbar(title = "Map")
fig <- fig %>% layout(
title = "Map)",
geo = g
)
fig
})
observe({
data <- USA_map()
print(data, n = 8, width = Inf) # Look at this output in your R console or log
})
}
# Run the application
shinyApp(ui = ui, server = server)
I tried following codes, but it showed error:
fig <- plot_geo(USA_map(), locationmode = 'USA-states', marker = list(line = l),
textposition = 'middle center',
textfont = list(color = 'black', size = 8)
)
The error shows:Warning: ‘choropleth’ objects don’t have these attributes: ‘textposition’, ‘textfont’
Valid attributes include:
‘autocolorscale’, ‘coloraxis’, ‘colorbar’, ‘colorscale’, ‘customdata’, ‘customdatasrc’, ‘featureidkey’, ‘geo’, ‘geojson’, ‘hoverinfo’, ‘hoverinfosrc’, ‘hoverlabel’, ‘hovertemplate’, ‘hovertemplatesrc’, ‘hovertext’, ‘hovertextsrc’, ‘ids’, ‘idssrc’, ‘legendgroup’, ‘legendgrouptitle’, ‘legendrank’, ‘locationmode’, ‘locations’, ‘locationssrc’, ‘marker’, ‘meta’, ‘metasrc’, ‘name’, ‘reversescale’, ‘selected’, ‘selectedpoints’, ‘showlegend’, ‘showscale’, ‘stream’, ‘text’, ‘textsrc’, ‘transforms’, ‘type’, ‘uid’, ‘uirevision’, ‘unselected’, ‘visible’, ‘z’, ‘zauto’, ‘zmax’, ‘zmid’, ‘zmin’, ‘zsrc’, ‘key’, ‘set’, ‘frame’, ‘transforms’, ‘_isNestedKey’, ‘_isSimpleKey’, ‘_isGraticule’, ‘_bbox’
Snow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.