cjerzak commited on
Commit
7f0f334
·
verified ·
1 Parent(s): 9bc5dc1

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +49 -18
app.R CHANGED
@@ -4,11 +4,11 @@ library(shinydashboard)
4
  library(dplyr)
5
  library(readr)
6
  library(sf)
7
- #library(cartogram) # optional if you decide to do cartograms later
8
- library(ggplot2)
9
  library(rnaturalearth)
10
  library(rnaturalearthdata)
11
  library(countrycode)
 
 
12
 
13
  # =============================
14
  # UI
@@ -55,7 +55,7 @@ ui <- dashboardPage(
55
  .sidebar-menu, .sidebar-menu li a, .sidebar-menu .fa {
56
  font-family: 'OCR A Extended', monospace !important;
57
  }
58
-
59
  /* Header gradient background */
60
  .main-header .navbar {
61
  background: linear-gradient(to right, #3b6978, #204051) !important;
@@ -82,19 +82,19 @@ ui <- dashboardPage(
82
  border-left-color: #78cdd7 !important;
83
  color: #ffffff !important;
84
  }
85
-
86
  /* Sidebar menu item icons */
87
  .sidebar-menu .fa {
88
  color: #78cdd7 !important;
89
  }
90
-
91
  /* Sidebar menu item text */
92
  .sidebar-menu > li > a {
93
  color: #b8c7ce !important;
94
  font-size: 15px;
95
  font-weight: 500;
96
  }
97
-
98
  /* Customize the boxes */
99
  .box {
100
  border-top: none !important;
@@ -127,8 +127,10 @@ ui <- dashboardPage(
127
  width = 12,
128
  title = strong("Global Leadership Project (GLP)"),
129
  solidHeader = TRUE,
130
- div(style = "height: 80vh; padding: 10px;",
131
- plotOutput("cartogramPlot", height = "100%")
 
 
132
  )
133
  )
134
  )
@@ -159,24 +161,43 @@ server <- function(input, output, session) {
159
  cartogram_sf <- reactive({
160
  merged_sf <- world_sf() %>%
161
  left_join(rankings_data(), by = "iso_a3")
162
- # Filter out NA values in 'Overall' to avoid missing countries
163
  merged_sf <- merged_sf[!is.na(merged_sf$Overall),]
164
- return(merged_sf)
165
  })
166
 
167
- # ---- Plot output ----
168
- output$cartogramPlot <- renderPlot({
169
  req(input$indexChoice)
170
 
171
- index_col <- input$indexChoice
172
  plot_data <- cartogram_sf()
 
173
 
174
- ggplot(plot_data) +
175
- geom_sf(aes_string(fill = index_col), color = "grey20", size = 0.1) +
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  scale_fill_viridis_c(option = "D", na.value = "white") +
177
  coord_sf(expand = FALSE) +
178
- # Force OCR A Extended font in the plot
179
- #theme_void(base_size = 14, base_family = "OCR A Extended") +
180
  theme_void(base_size = 14, base_family = "Courier New") +
181
  labs(
182
  fill = paste(index_col, "Index"),
@@ -192,6 +213,17 @@ server <- function(input, output, session) {
192
  legend.direction = "horizontal",
193
  legend.key.width = unit(2, "cm")
194
  )
 
 
 
 
 
 
 
 
 
 
 
195
  })
196
  }
197
 
@@ -199,4 +231,3 @@ server <- function(input, output, session) {
199
  # Launch the Shiny App
200
  # =============================
201
  shinyApp(ui = ui, server = server)
202
-
 
4
  library(dplyr)
5
  library(readr)
6
  library(sf)
 
 
7
  library(rnaturalearth)
8
  library(rnaturalearthdata)
9
  library(countrycode)
10
+ library(ggplot2)
11
+ library(ggiraph) # <-- for interactive tooltips
12
 
13
  # =============================
14
  # UI
 
55
  .sidebar-menu, .sidebar-menu li a, .sidebar-menu .fa {
56
  font-family: 'OCR A Extended', monospace !important;
57
  }
58
+
59
  /* Header gradient background */
60
  .main-header .navbar {
61
  background: linear-gradient(to right, #3b6978, #204051) !important;
 
82
  border-left-color: #78cdd7 !important;
83
  color: #ffffff !important;
84
  }
85
+
86
  /* Sidebar menu item icons */
87
  .sidebar-menu .fa {
88
  color: #78cdd7 !important;
89
  }
90
+
91
  /* Sidebar menu item text */
92
  .sidebar-menu > li > a {
93
  color: #b8c7ce !important;
94
  font-size: 15px;
95
  font-weight: 500;
96
  }
97
+
98
  /* Customize the boxes */
99
  .box {
100
  border-top: none !important;
 
127
  width = 12,
128
  title = strong("Global Leadership Project (GLP)"),
129
  solidHeader = TRUE,
130
+ # Instead of plotOutput, we use girafeOutput for interactive tooltips
131
+ div(
132
+ style = "height: 80vh; padding: 10px;",
133
+ girafeOutput("cartogramPlot", width = "100%", height = "100%")
134
  )
135
  )
136
  )
 
161
  cartogram_sf <- reactive({
162
  merged_sf <- world_sf() %>%
163
  left_join(rankings_data(), by = "iso_a3")
164
+ # Filter out NA values in 'Overall' (or whichever main column) to avoid missing countries
165
  merged_sf <- merged_sf[!is.na(merged_sf$Overall),]
166
+ return(merged_sf)
167
  })
168
 
169
+ # ---- Plot output (using ggiraph for tooltips) ----
170
+ output$cartogramPlot <- renderGirafe({
171
  req(input$indexChoice)
172
 
 
173
  plot_data <- cartogram_sf()
174
+ index_col <- input$indexChoice
175
 
176
+ # Build a tooltip string showing some key columns for each country.
177
+ # Modify this as desired to include more (or fewer) details.
178
+ plot_data$tooltip_text <- paste0(
179
+ "<b>Country:</b> ", plot_data$Country, "<br/>",
180
+ "<b>Overall:</b> ", ifelse(!is.na(plot_data$Overall), plot_data$Overall, "N/A"), "<br/>",
181
+ "<b>Representation Gap:</b> ", ifelse(!is.na(plot_data$RepresentationGap), plot_data$RepresentationGap, "N/A"), "<br/>",
182
+ "<b>Ethnicity:</b> ", ifelse(!is.na(plot_data$Ethnicity), plot_data$Ethnicity, "N/A"), "<br/>",
183
+ "<b>Gender:</b> ", ifelse(!is.na(plot_data$Gender), plot_data$Gender, "N/A"), "<br/>",
184
+ "<b>Religion:</b> ", ifelse(!is.na(plot_data$Religion), plot_data$Religion, "N/A"), "<br/>",
185
+ "<b>Language:</b> ", ifelse(!is.na(plot_data$Language), plot_data$Language, "N/A")
186
+ )
187
+
188
+ # Create a ggplot with geom_sf_interactive so we can assign tooltips
189
+ p <- ggplot(plot_data) +
190
+ geom_sf_interactive(
191
+ aes_string(
192
+ fill = index_col,
193
+ tooltip = "tooltip_text",
194
+ data_id = "iso_a3" # required for hover/click tracking
195
+ ),
196
+ color = "grey20",
197
+ size = 0.1
198
+ ) +
199
  scale_fill_viridis_c(option = "D", na.value = "white") +
200
  coord_sf(expand = FALSE) +
 
 
201
  theme_void(base_size = 14, base_family = "Courier New") +
202
  labs(
203
  fill = paste(index_col, "Index"),
 
213
  legend.direction = "horizontal",
214
  legend.key.width = unit(2, "cm")
215
  )
216
+
217
+ # Wrap the ggplot in a girafe object
218
+ girafe(
219
+ ggobj = p,
220
+ width_svg = 10,
221
+ height_svg = 6,
222
+ options = list(
223
+ # You can style the tooltip background here
224
+ opts_tooltip(css = "background-color: white; font-family: 'OCR A Extended', monospace;")
225
+ )
226
+ )
227
  })
228
  }
229
 
 
231
  # Launch the Shiny App
232
  # =============================
233
  shinyApp(ui = ui, server = server)