Introduction

This code through explores a few different ways to use plotly to create interactive charts and maps.


Content Overview

Specifically, we’ll explain and demonstrate how to create a basic chart, customized chart, and an interactive map.


Why You Should Care

This topic is valuable because Plotly is easy to use and will help display data in a variety of charts and graphs. This package can be utilized by everyone.


Learning Objectives- charts:

You’ll learn how to create a chart and customize it using the three function: plot_ly, layout, and add_trace.



First let will start with a simple plot using the builtin data set for Chickweight. In this we utilize the first function, plot_ly.

fig<-ChickWeight %>% 
  plot_ly(x=~Time, y=~weight)
fig


The second function, layout, allows us to specify characteristics of x and y. In the following, we have added titles and ranges to the chart above.

fig<-ChickWeight %>% 
  plot_ly(x=~Time, y=~weight) %>% 
  layout(
    title= "Chicken Growth by Age",
    xaxis= list(
      title= "Age (Days)",
      range=c(0,22)
    ),
    yaxis=list( 
      title= "Weight (Grams) ",
      range= c(25,300)
    )
  )
fig


The third fuction, add_trace, allows us to select a plot and customize it further. In the following code, we use a different builtin data set (just for fun). We add interactive details such as a rangeslider which allows the view to focus on a specific area in the x-axis. We also created a line to show the average, this serves as Y bar.

fig<- airquality %>% 
  plot_ly(x= ~Temp, y= ~Ozone, type= "scatter") %>% 
  add_lines(y=~77.8, name="average temp") %>%   #created Y Bar to show average
  layout(
    title= "Air Quality",
    xaxis= list(
      title= "temp",
      rangeslider= list(type="x")  #allows user to control X-axis
    ),
    yaxis=list(title= "Ozone") 
      
  )
fig

Advanced Examples- Maps

For the next section, we will use plotly to create an interactive map. This is suprisingly easy through this package. for the first example, we will use the builtin data set, seals. For maps, we use plot_geo, instead of plot_ly.

fig<- seals %>% 
  plot_geo(lat= ~lat, lon= ~long)
fig


a better example is with world cities.

fig<- world.cities %>% 
  plot_geo(lat= ~lat, lon= ~long)
fig


We can customize it more to highlight information. Specially since this data set doesnt provide much on the map. By using add_markers, we can add text so ever time the user hovers over the map, they see a specific point. In this case, I chose to highlight the population.

fig<- fig %>% 
  add_markers(
    text=~paste("population:", pop) 
  )
fig



Plotly allows us to customize maps even further by changing colors, adding symbols, hovering information, and/or changing size.

fig<- world.cities %>% 
  plot_geo(lat=~lat, lon=~long) %>% 
  add_markers(
    text=~paste("population:", pop),
    color=~pop,
    symbol=I("square"),
    size=I(2),
    hoverinfo= "text", 
    opacity=I(0.8)  
  )
fig



Lastly and more notably, you can create desired customization and save it as a vector. That way, the code looks neater. In the following example, we will zoom in our map from the previous example and focus on the USA.

cust_usa<- list(
  scope= 'usa',
  projection=list(type='albers usa'),
  showland= TRUE,
  landcolor=toRGB("gray"),
  subunitcolor=toRGB("gray"),
  countrycolor=toRGB("gray95"),
  countrywidth=0.5,
  subunitwidth=0.5
)

fig<-fig %>% 
  layout(
    geo=cust_usa,
    title= "Map of world cities-USA edition"
  )
fig


Works Cited

This code through references and cites the following sources: