This code through explores a few different ways to use plotly to create interactive charts and maps.
Specifically, we’ll explain and demonstrate how to create a basic chart, customized chart, and an interactive map.
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.
You’ll learn how to create a chart and customize it using the three function: plot_ly, layout, and add_trace.
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)
)
)
figfig<- 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")
)
figFor 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)
figa better example is with world cities.
fig<- world.cities %>%
plot_geo(lat= ~lat, lon= ~long)
figWe 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"
)
figLearn more about [package, technique, dataset] with the following:
Resource I https://rpubs.com/gaomatth/maps
Resource II https://briansarnacki.com/2017/01/09/an-easy-way-to-map-data-in-r-with-plotly/
Resource III https://plotly.com/r/
Resource IV https://towardsdatascience.com/3-easy-tricks-for-beginners-to-improve-your-plotly-charts-in-r-86d65772a701
This code through references and cites the following sources:
Plotly Graphing Libraries (2023). Source I. https://plotly.com/r/getting-started/
Rstudio (2023). Source II. https://plotly-r.com/overview.html
Erol, Yigit (2020). Source III. https://medium.com/swlh/step-by-step-data-visualization-guideline-with-plotly-in-r-fbd212640de2