R packages
Course Name: Data Visualisation
Course Code: CSE3020
Slot: E2
Semester: FALL 20-21
Digital Assignment one
Submitted by:
LAMESGEN ADUGNAW (18BCE2428)
Submitted to:
Dr. ANNAPURNA JONNALAGADDA
Date of submission: 27-09-2020
Question:
Choose a data visualization package from R. Prepare a cheat sheet that discusses various functions and their corresponding aesthetics.
packages:
ggmap
stringr
igraph
ggmap:
Description: ggmap plots the raster object produced by get-map
Usage: ggmap(ggmap, extent = “panel”, base_layer, maprange = FALSE,
legend = “right”, padding = 0.02, darken = c(0, “black”), b,
fullpage, expand, …)
Arguments:
ggmap an object of class ggmap (from function get_map)
extent how much of the plot should the map take up? “normal”, “device”, or “panel”
(default)
base_layer a ggplot(aes(…), …) call; see examples
maprange logical for use with base_layer; should the map define the x and y limits
legend “left”, “right” (default), “bottom”, “top”, “bottomleft”, “bottomright”, “topleft”,“topright”,
“none” (used with extent = “device”
padding distance from legend to corner of the plot (used with legend, formerly b
darken vector of the form c(number, color), where number is in [0, 1] and color is
a character string indicating the color of the darken. 0 indicates no darkening, 1indicates a black-out.
b Deprecated, renamed to ‘padding‘. Overrides any ‘padding‘ argument
expand Deprecated, equivalent to ‘extent = “panel”‘ when ‘TRUE‘ and ‘fullpage‘ is‘FALSE‘.
When ‘fullpage‘ is ‘FALSE‘ and ‘expand‘ is ‘FALSE‘,
equivalent to‘extent=”normal”‘. Overrides any ‘extent‘ argument.
stringr
Detect Matches
str_detect(string, pattern)
Detect the presence of a pattern match in a string. str_detect(car, “a”)
str_which(string, pattern)
Find the indexes of strings that contain a pattern match. str_which(bike, “a”)
str_count(string, pattern)
Count the number of matches in a string. str_count(fruit, “a”)
str_locate(string, pattern)
Locate the positions of pattern matches in a string. Also str_locate_all. str_locate(fruit, “a”)
Subset Strings
str_sub(string, start = 1L, end = -1L)
Extract substrings from a character vector. str_sub(fruit, 1, 3); str_sub(fruit, -2)
str_subset(string, pattern)
Return only the strings that contain a pattern match. str_subset(fruit, “b”)
str_extract(string, pattern)
Return the first pattern match found in each string, as a vector. Also str_extract_all to return every pattern match. str_extract(fruit, “[aeiou]”)
str_match(string, pattern)
Return the first pattern match found in each string, as a matrix with a column for each ( ) group in pattern. Also str_match_all. str_match(sentences, “(a|the) ([^ ]+)”)
str_length(string)
The width of strings (i.e. number of code points, which generally equals the number of characters). str_length(fruit)
str_pad(string, width, side = c(“lef”, “right”, “both”), pad = ” “)
Pad strings to constant width. str_pad(fruit, 17)
str_trunc(string, width, side = c(“right”, “lef”, “center”), ellipsis = “…”) Truncate the width of strings, replacing content with ellipsis. str_trunc(fruit, 3)
str_trim(string, side = c(“both”, “lef”, “right”))
Trim whitespace from the start and/or end of a string. str_trim(fruit)
Mutuate Strings
str_sub() <- value.
Replace substrings by identifying the substrings with str_sub() and assigning into the results. str_sub(fruit, 1, 3) <- “str”
str_replace(string, pattern, replacement)
Replace the first matched pattern in each string. str_replace(fruit, “a”, “-“)
str_replace_all(string, pattern, replacement)
Replace all matched patterns in each string. str_replace_all(fruit, “a”, “-“)
str_to_lower(string, locale = “en”)
Convert strings to lower case. str_to_lower(sentences)
str_to_upper(string, locale = “en”)
Convert strings to upper case. str_to_upper(sentences)
str_to_title(string, locale = “en”)
Convert strings to title case. str_to_title(sentences)
Join and Split
str_c(…, sep = “”, collapse = NULL)
Join multiple strings into a single string. str_c(letters, LETTERS)
str_c(…, sep = “”, collapse = “”)
Collapse a vector of strings into a single string. str_c(letters, collapse = “”)
str_dup(string, times)
Repeat strings times times. str_dup(fruit, times = 2)
str_split_fixed(string, pattern, n)
Split a vector of strings into a matrix of substrings (splitting at occurrences of a pattern match). Also str_split to return a list of substrings. str_split_fixed(fruit, ” “, n=2)
str_glue(…, .sep = “”, .envir = parent.frame())
Create a string from strings and {expressions} to evaluate. str_glue(“Pi is {pi}”)
str_glue_data(.x, …, .sep = “”, .envir = parent.frame(), .na = “NA”)
Use a data frame, list, or environment to create a string from strings and {expressions} to evaluate. str_glue_data(mtcars, “{rownames(mtcars)} has {hp} hp”)
Wrap strings into nicely formatted paragraphs. str_wrap(sentences, 20)
igraph
igraph does simple non-interactive 2D plotting to R devices. Actually it is an implementation of the plot generic function,
so you can write plot(graph) instead of plot. igraph(graph) . As it used the standard R devices it supports every output format for which R has an output device.
Display graph vertices
ollowing are the basic operations we perform on graphs.
Display graph vertices
Display graph edges
Add a vertex
Add an edge
Creating a graph
To display the graph vertices we simple find the keys of the graph dictionary. We use the keys() method.
class graph:
def init(self,gdict=None):
if gdict is None:
gdict = []
self.gdict = gdict
# Get the keys of the dictionary
def getVertices(self):
return list(self.gdict.keys())
# Create the dictionary with graph elements
graph_elements = { “a” : [“b”,”c”],
“b” : [“a”, “d”],
“c” : [“a”, “d”],
“d” : [“e”],
“e” : [“d”]
}
g = graph(graph_elements)
print(g.getVertices())