We will now learn additional features and options that are available when making markers and pop-ups for interactive maps using leaflet() and the R
Packages
We need one package, which is shown below
library(leaflet)
Change Marker Color
We will start by showing the default marker and its color below
#Make Markers
leaflet() %>%
addProviderTiles("CartoDB") %>%
addMarkers(lat = c(34.05223,30), lng = c(-118.2437,-110))
The code above is a review. leaflet() is the function for making the map. addProviderTiles() is the function for selecting the type of map, and addMarkers() is the function for setting the coordinates of each marker.

Now that we know our baseline, we will adjust it. The code below uses the addCircleMarkers() function. Inside this function, we also set the radius and color of our circle marker.
#change to circle & change color
leaflet() %>%
addProviderTiles("CartoDB") %>%
addCircleMarkers(lat = c(34.05223,30), lng = c(-118.2437,-110),
radius = 2, color = 'red')

You can continue to manipulate this code to change the marker size and color.
Manipulating Pop-Ups
We have used pop-ups before. In the code below, we use circles as our marker. For our data, we select only the first 10 cities in the dataset (you can pull city names and their location from the internet). We also set the radius and the pop-up argument to ~city, which instructs R to retrieve all city names from the select data.
leaflet() %>%
addProviderTiles("CartoDB") %>%
addCircleMarkers(data = uscities[1:10,],
radius = 2, popup = ~city)

The example below is essentially the same, but with the manipulation of color.
# Change circle color to #2cb42c
leaflet() %>%
addProviderTiles("CartoDB") %>%
addCircleMarkers(data = uscities[1:10,], radius = 2,
color = "#2cb42c", popup = ~city)

Next, we will look at how to manipulate the information that is displayed in a pop-up
Add Info to Popup
The code below is mostly the same. The exception is the information for the pop-up argument. Here, we are using the paste0(). Inside this function, we have the city name followed by a break “<br/>” and the county name. What this is doing is telling R to display the city name followed by the county name on a separate line. Below is the code, followed by the map
# Make the institution name in each popup
leaflet() %>%
addProviderTiles("CartoDB") %>%
addCircleMarkers(data = uscities[1:10,], radius = 2,
popup = ~paste0( city, "<br/>", county_name))

Again, we have mostly the same code, but this time we are going to bold the city name using <b>. All this is showing that you can manipulate pop-ups with basic HTML.
# Make the institution name in each popup bold
leaflet() %>%
addProviderTiles("CartoDB") %>%
addCircleMarkers(data = uscities[1:10,], radius = 2,
popup = ~paste0("<b>", city, "</b>",
"<br/>", county_name))

Lastly, we use a label instead of a marker this time. Labels only work when you hover over them, while pop-ups require that you click on them. For this label, we want the city name followed by the state name in parentheses. Notice the syntax carefully. We have a space after the first quotation mark. Also notice how the parentheses are setup allong with the commas. If this is unclear, you might have to manipulate it until you are comfortable
# Use paste0 to add sector information to the label inside parentheses
leaflet() %>%
addProviderTiles("CartoDB") %>%
addCircleMarkers(data = uscities[1:10,],
radius = 2,
label = ~paste0(city, " (", state_id, ")"))

Conclusion
Leaflet() allows you to manipulate markers and pop-ups in many different ways. This can allow the data analysis to communicate insights in a way that is powerful and clear.
























































