Chaning labels in choropleth hcmap - r

I have the following dataset:
structure(list(code = structure(1:6, .Label = c("?elino", "?tip",
"?uto Orizari", "Aerodrom", "Aracinovo", "Berovo", "Bitola",
"Bogdanci", "Bogovinje", "Bosilovo", "Brod", "Brvenica", "Butel",
"Ca?ka", "Cair", "Ce?inovo-Oble?evo", "Centar", "Centar ?upa",
"Cucer Sandevo", "Debar", "Debarca", "Delcevo", "Demir Hisar",
"Demir Kapija", "Dojran", "Dolneni", "Drugovo", "Gazi Baba",
"Gjorce Petrov", "Gostivar", "Gradsko", "Ilinden", "Jegunovce",
"Karbinci", "Karpo?", "Kavadartsi", "Kicevo", "Kisela Voda",
"Kocani", "Konce", "Kratovo", "Kriva Palanka", "Krivoga?tani",
"Kru?evo", "Kumanovo", "Lipkovo", "Lozovo", "Makedonska Kamenica",
"Mavrovo and Rostusa", "Negotino", "Northeastern", "Novatsi",
"Novo Selo", "Ohrid", "Oslomej", "Pelagonia", "Phecevo", "Plasnica",
"Polog", "Prilep", "Probistip", "Radovis", "Rankovce", "Resen",
"Saraj", "Skopje", "Sopiste", "Southeastern", "Struga", "Studenicani",
"Sveti Nikole", "Tearce", "Tetovo", "Valandovo", "Vardar", "Vasilevo",
"Veles", "Vev?ani", "Vinitsa", "Vrane?tica", "Zajas", "Zelenikovo",
"Zrnovci"), class = "factor"), value = c(48L, 1810L, 205L, 1507L,
38L, 66L), OPSTINA_NAZIV = c("ЖЕЛИНО", "ШТИП", "ШУТО ОРИЗАРИ",
"АЕРОДРОМ", "АРАЧИНОВО", "БЕРОВО"), `postal-code` = c("ZE", "ST",
"SO", "AD", "AR", "BR")), .Names = c("code", "value", "OPSTINA_NAZIV",
"postal-code"), row.names = c(NA, 6L), class = "data.frame")
and I'm plotting a choropleth map with the hcmap function below:
hcmap("countries/mk/mk-all.js", data = data_fake,
name = "Manucipalities", value = "value", joinBy = c("name", "code"),
borderColor = "transparent") %>%
hc_colorAxis(dataClasses = color_classes(c(seq(0, 2000, by = 500), 13000))) %>%
hc_legend(layout = "vertical", align = "right",
floating = TRUE, valueDecimals = 0, valueSuffix = "") %>%
hc_mapNavigation(enabled = TRUE)
However, at the moment the labels that appear on the map are from the "code" variable, which contain encoding problems. I want to plot the labels from the "OPSTINA_NAZIV" label.
Any ideas how I can do this?
I tried:
dataLabels = list(enabled = TRUE, format = '{point.OPSTINA_NAZIV}')
But it didn't work out.

You can access to the mapData info using the options accesor. Example {point.options.OPSTINA_NAZIV}:
hcmap("countries/mk/mk-all.js", data = data_fake,
name = "Manucipalities", value = "value", joinBy = c("name", "code"),
borderColor = "transparent" ,
dataLabels = list(enabled = TRUE, format = "{point.options.OPSTINA_NAZIV}"))

Related

How do you plot different types of WKT geometry from a dataframe in R / Leaflet?

I have a dataframe df that has a Geometry Column which contains WKT. Currently there is a mix of POINT, LINESTRING AND MULTILINESTRING data in there but in the future it might also contain all the WKT types like POLYGON and the others.
If I remove all but the LINESTRING and MULTILINESTRING from my dataframe then my code below works, I get all polylines plotted.
How do I plot Geometry in R Leaflet irrespective if it is a line, polygon, point, etc all from within one column in the dataframe?
I was thinking I would need to either filter the Geometry column into a new dataframe for each type or have a series of if statements. Is this thinking right or is there a simpler way?
library(leaflet)
library(sf)
setwd("C:/Users/XXXX/Documents/R Programming Training/Leaflet Learning")
df <- read.csv("CLAWKT2.csv") # ordinary data frame
df <- st_as_sf(df, wkt = "Geometry") # convert to spatial data frame
m <- leaflet(df) %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
addProviderTiles('Esri.WorldImagery', group = "Sat View") %>%
addPolylines(
data = df$Geometry,
color = "red",
weight = 5,
group = "Polylines"
) %>%
addMiniMap(
minimized = FALSE,
toggleDisplay = TRUE,
position = "bottomleft"
) %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite", "Sat View"),
overlayGroups = c("Polylines"),
position = "topleft",
options = layersControlOptions(collapsed = TRUE)
)
m
dput(df):
structure(list(Asset_ID = 1000001:1000006, System_Name = c("System Name 1",
"System Name 2", "System Name 4", "System Name 4", "System Name 1",
"System Name 3"), Asset_Description = c("Fork", "Spoon", "Fork",
"Fork", "Knife", "Knife"), Asset_Name = c("Asset Name 1", "Asset Name 2",
"Asset Name 3", "Asset Name 4", "Asset Name 5", "Asset Name 6"
), Condition = c(1L, 3L, 5L, 5L, 2L, 3L), Geometry = structure(list(
structure(c(-1.35198486751484, -1.35234525303538, -1.35248940653067,
-1.35260441494862, -1.35263555353458, -1.35258473300277,
-1.35250559836976, -1.35232875476765, -1.35192607680683,
-1.35180865355401, -1.35179195040429, -1.35187451322375,
-1.35185429834619, -1.35178329473176, -1.35126955152303,
-1.35055959559126, -1.34978502894559, -1.34989107305621,
-1.35021695298201, -1.350317183135, -1.35065915038905, -1.35079706278466,
-1.35105996018697, -1.3516517187018, -1.35249688275413, -1.35271774874895,
-1.35292405499795, -1.35366647372453, -1.35375889918796,
-1.354010774701, -1.35412851846741, -1.35462439479792, -1.35471726841881,
-1.35474040831931, -1.35469627169117, -1.35478798316437,
-1.35490481493925, -1.35528387086618, -1.35571095347439,
-1.35577322047125, -1.35581587026861, -1.35590792899297,
-1.35600943689386, -1.3562208820016, -1.3563546466869, -1.35716562572165,
-1.35806128168316, -1.35853813024667, -1.35905337577199,
-1.35985958967845, -1.36013907704664, -1.36251821458615,
-1.36265792115304, -1.36318262266271, -1.36334686922989,
-1.36345340367592, -1.36348827561781, -1.3635893807038, -1.36373218569516,
-1.36422067066856, -1.36435848664855, -1.36440064030488,
-1.36436463781278, -1.36408942106959, -1.36283962792941,
-1.36237310571868, -1.36225206144648, -1.36190504118253,
-1.36153848956157, -1.36141888422639, -1.36124536097223,
-1.3607750850719, -1.36054500378561, -1.36050578814311, -1.36056320617949,
-1.36053267567725, -1.36032906026332, -1.36004399212011,
-1.35874793687755, -1.35834434617168, -1.35795835965026,
-1.35755662752073, -1.35723694491421, -1.35699018364039,
-1.35657916982805, -1.35613059279507, -1.35491430369687,
-1.35428681819522, -1.3535467929927, -1.35333832296981, -1.35280783616645,
-1.35264422520212, -1.35222048431593, -1.3510345432854, -1.35090226316381,
-1.35061671758139, -1.35019375996963, -1.35001911996661,
-1.34986768054462, -1.34954215333471, -1.34938071152082,
-1.34927853682671, -1.3490119966149, -1.34886654105233, -1.34868588491475,
-1.34858222009029, -1.34842466818215, -1.34839532338292,
-1.34842931519985, -1.34838809888369, -1.34827604030248,
-1.34791677906444, -1.34781032734578, -1.34736504577421,
-1.34726622053787, -1.3470650675856, -1.34692453899414, -1.34616525509612,
-1.34597184823335, -1.34580532993775, -1.34559325200233,
-1.3453051671889, -1.34499490126712, -1.34475921505699, -1.34427536175846,
-1.34431764056143, -1.34430733181747, -1.34423416754179,
-1.34408156080249, -1.34367572525899, -1.3434969621119, -1.34317611324474,
-1.34316072830446, -1.34326282023099, -1.34338800930314,
-1.34347021609699, -1.34377275650412, -1.34450422347519,
-1.34484695009675, -1.34532013050026, -1.34563518136781,
-1.34586726368103, -1.34637188063967, -1.34693883347381,
-1.34756878756897, -1.35083266612587, -1.3518708456867, 53.3665188040405,
53.3670648592674, 53.3673765930719, 53.3677833937721, 53.368177381022,
53.3684630599995, 53.3686645977258, 53.3689605490667, 53.3692825246462,
53.3694380677381, 53.3695174717774, 53.3698648273811, 53.3699899982912,
53.3700884896613, 53.37036239008, 53.3705357847352, 53.3712821369098,
53.3716905766854, 53.3722212777486, 53.37245637116, 53.3730419328704,
53.3733631859107, 53.3737744412375, 53.3744916907916, 53.3758470977014,
53.3760272219281, 53.37614518974, 53.3764713881183, 53.3765409873118,
53.3774987928917, 53.3776339773286, 53.3778638955976, 53.377966361556,
53.3781451352678, 53.3785485423452, 53.3788215045086, 53.3789864580418,
53.3792387771138, 53.3797023057244, 53.3798411223343, 53.380190784622,
53.3804727363201, 53.3806887281319, 53.3809668275634, 53.3810374901495,
53.3812480251129, 53.3819834380591, 53.3821711044499, 53.3823362216559,
53.3827733939237, 53.3830906135445, 53.3869018259181, 53.3870171748615,
53.3871517049941, 53.3872284194866, 53.3873944321715, 53.387671577772,
53.3877920559123, 53.387857420131, 53.3879954049968, 53.3880804044196,
53.38818427798, 53.3882986910424, 53.3886376716585, 53.3897048808548,
53.3902984523894, 53.3904118503725, 53.3906481991627, 53.3910594395905,
53.3911391373374, 53.3912106824521, 53.3913300811699, 53.3914442996329,
53.3915213362836, 53.3917980414656, 53.3919855152756, 53.3921835828977,
53.3923292497264, 53.3928638858022, 53.3929844764941, 53.3930590923509,
53.3931808139183, 53.3933147711706, 53.3933805813408, 53.3934317435688,
53.3934495585554, 53.3936132372758, 53.3936531142405, 53.3938010868269,
53.3937892874904, 53.3936920413448, 53.3936970564744, 53.393812180053,
53.3942344053924, 53.3942494193738, 53.3942377586893, 53.3941458576011,
53.3941342355709, 53.394173020098, 53.3943181601263, 53.3943655970331,
53.3943785247569, 53.3943532000783, 53.3943692628634, 53.3944747393774,
53.3947087240509, 53.3949323022387, 53.3950383212475, 53.3952438416935,
53.3953579416292, 53.3954640722243, 53.3956938549797, 53.3958019824157,
53.3966784804635, 53.3967793448177, 53.3968720674158, 53.3969033232058,
53.3968604159691, 53.3968767752972, 53.3969171578455, 53.3970171216256,
53.397200657016, 53.3974205878186, 53.3976499137485, 53.3982781581855,
53.3990752927601, 53.3998384308461, 53.400103758036, 53.4002473029698,
53.400445078686, 53.4005778063306, 53.4011476749186, 53.4013534882906,
53.4016529235755, 53.4018258008763, 53.4018900149294, 53.4020225714569,
53.4022302285786, 53.4022559797624, 53.4022037926322, 53.402195120724,
53.4022331865993, 53.4023688058688, 53.4024547639985, 53.4025090393943,
53.4024180668628, 53.4025186346169), dim = c(147L, 2L), class = c("XY",
"LINESTRING", "sfg")), structure(c(-3.36187363816841, -3.36189102805219,
54.6614176695771, 54.6613396436674), dim = c(2L, 2L), class = c("XY",
"LINESTRING", "sfg")), structure(list(structure(c(-1.68034047322624,
-1.68038289179782, -1.68045906926982, -1.68048512895859,
-1.68053513999523, -1.68058793251902, -1.6806304470928, -1.68068494033555,
-1.68080486828078, -1.68090160208891, -1.68098744619037,
-1.681059243053, -1.68120417228514, -1.68130363603522, -1.68144793922962,
-1.68149890595872, -1.68158988492711, -1.6816730354649, -1.68176169843031,
-1.68178287840857, -1.68180681439964, -1.68184330248185,
-1.68200852094169, -1.68211520890884, -1.68229203857493,
-1.68246049811455, -1.68265612784607, -1.68275449461309,
-1.68286754214383, -1.68296114395775, -1.68301728003276,
55.1690912809677, 55.1691070637455, 55.1691323332199, 55.169140494827,
55.1691515333531, 55.16916064358, 55.1691674400557, 55.1691754997488,
55.1691873362278, 55.1692364993597, 55.1692634651086, 55.1692745764327,
55.1692974575058, 55.1693124957266, 55.1693322244598, 55.1693390426607,
55.169338928231, 55.1693570893883, 55.1693777287495, 55.1693813027131,
55.1693859396785, 55.1693971186269, 55.1695004809808, 55.1695393201381,
55.1696303433464, 55.1697228191642, 55.1698040413541, 55.1698414188748,
55.1698879828757, 55.1699450529148, 55.1699800665141), dim = c(31L,
2L)), structure(c(-1.67991922565183, -1.67992292824692, -1.67992871281608,
-1.67994732968346, -1.67996521661522, -1.67998904788041,
-1.6800139147732, -1.68004425890084, -1.68007241769249, -1.68010884196028,
-1.68012858924333, -1.6801329331177, 55.168869598948, 55.1688759382411,
55.1688828149423, 55.1688994020405, 55.1689122671015, 55.1689300988942,
55.1689468146592, 55.1689633192048, 55.1689780828675, 55.168996915352,
55.169006643819, 55.1690087472255), dim = c(12L, 2L))), class = c("XY",
"MULTILINESTRING", "sfg")), structure(list(structure(c(-1.68034047322624,
-1.68038289179782, -1.68045906926982, -1.68048512895859,
-1.68053513999523, -1.68058793251902, -1.6806304470928, -1.68068494033555,
-1.68080486828078, -1.68090160208891, -1.68098744619037,
-1.681059243053, -1.68120417228514, -1.68130363603522, -1.68144793922962,
-1.68149890595872, -1.68158988492711, -1.6816730354649, -1.68176169843031,
-1.68178287840857, -1.68180681439964, -1.68184330248185,
-1.68200852094169, -1.68211520890884, -1.68229203857493,
-1.68246049811455, -1.68265612784607, -1.68275449461309,
-1.68286754214383, -1.68296114395775, -1.68301728003276,
55.1690912809677, 55.1691070637455, 55.1691323332199, 55.169140494827,
55.1691515333531, 55.16916064358, 55.1691674400557, 55.1691754997488,
55.1691873362278, 55.1692364993597, 55.1692634651086, 55.1692745764327,
55.1692974575058, 55.1693124957266, 55.1693322244598, 55.1693390426607,
55.169338928231, 55.1693570893883, 55.1693777287495, 55.1693813027131,
55.1693859396785, 55.1693971186269, 55.1695004809808, 55.1695393201381,
55.1696303433464, 55.1697228191642, 55.1698040413541, 55.1698414188748,
55.1698879828757, 55.1699450529148, 55.1699800665141), dim = c(31L,
2L)), structure(c(-1.67991922565183, -1.67992292824692, -1.67992871281608,
-1.67994732968346, -1.67996521661522, -1.67998904788041,
-1.6800139147732, -1.68004425890084, -1.68007241769249, -1.68010884196028,
-1.68012858924333, -1.6801329331177, 55.168869598948, 55.1688759382411,
55.1688828149423, 55.1688994020405, 55.1689122671015, 55.1689300988942,
55.1689468146592, 55.1689633192048, 55.1689780828675, 55.168996915352,
55.169006643819, 55.1690087472255), dim = c(12L, 2L))), class = c("XY",
"MULTILINESTRING", "sfg")), structure(c(-1.3612916916826,
53.3912536935424), class = c("XY", "POINT", "sfg")), structure(c(-1.4217339865527,
53.2123973008381), class = c("XY", "POINT", "sfg"))), n_empty = 0L, crs = structure(list(
input = NA_character_, wkt = NA_character_), class = "crs"), class = c("sfc_GEOMETRY",
"sfc"), precision = 0, bbox = structure(c(xmin = -3.36189102805219,
ymin = 53.2123973008381, xmax = -1.34316072830446, ymax = 55.1699800665141
), class = "bbox"), classes = c("LINESTRING", "LINESTRING", "MULTILINESTRING",
"MULTILINESTRING", "POINT", "POINT"))), row.names = c(NA, -6L
), class = c("sf", "data.frame"), sf_column = "Geometry", agr = structure(c(Asset_ID = NA_integer_,
System_Name = NA_integer_, Asset_Description = NA_integer_, Asset_Name = NA_integer_,
Condition = NA_integer_), class = "factor", levels = c("constant",
"aggregate", "identity")))
I have managed to answer my own question!
R is my first programming language and I am in my first two weeks of learning. After learning about Data Frames from the Udemy course I am doing and googling I discovered the st_geometry_type() function within sf.
There are probably much more intelligent ways to solve this, but this works for me:
library(leaflet)
library(sf)
setwd("C:/Users/XXXX/Documents/R Programming Training/Leaflet Learning")
df <- read.csv("datasource.csv", stringsAsFactors = TRUE) # ordinary data frame with strings converted to FACTORS which are essentially categories. see Summary() it has grouped the geometry
df <- st_as_sf(df, wkt = "Geometry") # convert to spatial data frame
df$geometry_types <- st_geometry_type(df) # creates a new column in df data frame called geometry_types where the function st_geometry_type() has worked out the geometry type
all.lines.geometry <- df[df$geometry_types %in% c("LINESTRING","MULTILINESTRING"),] # use the new geometry_type column to filter only for LINESTRING and MULTILINESTRING
all.point.geometry <- df[df$geometry_types == "POINT",] # use the new geometry_type column to filter only for POINT
all.polygons.geometry <- df[df$geometry_types %in% c("POLYGON","MULTIPOLYGON"),] # use the new geometry_type column to filter only for POLYGON and MULTIPOLYGON
m <- leaflet(df) %>%
addTiles(group = "OSM (default)") %>%
addCircles(
data = all.point.geometry, # data source is the filtered vector we created called all.point.geometry
color = "blue",
) %>%
addPolylines(
data = all.lines.geometry, # data source is the filtered vector we created called all.lines.geometry
color = "red",
weight = 5
) %>%
addPolygons(
data = all.polygons.geometry, # data source is the filtered vector we created called all.polygons.geometry
color = "green",
)
m

How do I insert more expansive dataframe columns with text or a table or ggplot2 ouput into a popup in R / Leaflet?

I am at the very beginning of learning my first programming language, which is R. I am really keen to learn leaflet stuff and made some good progress over the last couple of nights.
Getting stuck with populating more expansive popup content within AddPolylines function. I have got it to populate the Asset_Name from my data frame.
However, how would I approach the following few options:
Using two columns from my data frame together in one popup e.g.
addPolylines(
data = df$Geometry,
color = "red",
weight = 5,
popup = df$Asset_Name & df$Asset_Description,
group = "Polylines"
) %>%
This errors in R, I think because I am using the wrong operator.
How could I use html and call columns from data frame in a popup?
e.g.
"Asset Name:" df$Asset_Name </br>
"Asset Description:" df$Asset_Description
I am trying to read the documentation:
https://rstudio.github.io/leaflet/popups.html
However, I am still lost.
Please help, many thanks for reading.
library(leaflet)
library(sf)
setwd("C:/Users/XXXX/Documents/R Programming Training/Leaflet Learning")
df <- read.csv("CLBWKT_LinesOnly.csv") # ordinary data frame
df <- st_as_sf(df, wkt = "Geometry") # convert to spatial data frame
m <- leaflet(df) %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
addProviderTiles('Esri.WorldImagery', group = "Sat View") %>%
addPolylines(
data = df$Geometry,
color = "red",
weight = 5,
popup = df$Asset_Name,
group = "Polylines"
) %>%
addMiniMap(
minimized = FALSE,
toggleDisplay = TRUE,
position = "bottomleft"
) %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite", "Sat View"),
overlayGroups = c("Polylines", "Markers"),
position = "topleft",
options = layersControlOptions(collapsed = TRUE)
)
m
structure(list(Asset_ID = 100001:100004, System_Name = c("System 1",
"System 1", "System 2", "System 3"), Asset_Description = c("Spoon",
"Fork", "Knife", "Spoon"), Asset_Name = c("Asset Name 1", "Asset Name 2",
"Asset Name 3", "Asset Name 4"), Condition = 1:4, Geometry = structure(list(
structure(c(-1.68639290511313, -1.68635836567153, -1.68565219769999,
-1.68565171774367, 55.1667596590412, 55.1667573239408, 55.1667106840643,
55.1667388899525), dim = c(4L, 2L), class = c("XY", "LINESTRING",
"sfg")), structure(list(structure(c(-1.68034047322624, -1.68038289179782,
-1.68045906926982, -1.68048512895859, -1.68053513999523,
-1.68058793251902, -1.6806304470928, -1.68068494033555, -1.68080486828078,
-1.68090160208891, -1.68098744619037, -1.681059243053, -1.68120417228514,
-1.68130363603522, -1.68144793922962, -1.68149890595872,
-1.68158988492711, -1.6816730354649, -1.68176169843031, -1.68178287840857,
-1.68180681439964, -1.68184330248185, -1.68200852094169,
-1.68211520890884, -1.68229203857493, -1.68246049811455,
-1.68265612784607, -1.68275449461309, -1.68286754214383,
-1.68296114395775, -1.68301728003276, 55.1690912809677, 55.1691070637455,
55.1691323332199, 55.169140494827, 55.1691515333531, 55.16916064358,
55.1691674400557, 55.1691754997488, 55.1691873362278, 55.1692364993597,
55.1692634651086, 55.1692745764327, 55.1692974575058, 55.1693124957266,
55.1693322244598, 55.1693390426607, 55.169338928231, 55.1693570893883,
55.1693777287495, 55.1693813027131, 55.1693859396785, 55.1693971186269,
55.1695004809808, 55.1695393201381, 55.1696303433464, 55.1697228191642,
55.1698040413541, 55.1698414188748, 55.1698879828757, 55.1699450529148,
55.1699800665141), dim = c(31L, 2L)), structure(c(-1.67991922565183,
-1.67992292824692, -1.67992871281608, -1.67994732968346,
-1.67996521661522, -1.67998904788041, -1.6800139147732, -1.68004425890084,
-1.68007241769249, -1.68010884196028, -1.68012858924333,
-1.6801329331177, 55.168869598948, 55.1688759382411, 55.1688828149423,
55.1688994020405, 55.1689122671015, 55.1689300988942, 55.1689468146592,
55.1689633192048, 55.1689780828675, 55.168996915352, 55.169006643819,
55.1690087472255), dim = c(12L, 2L))), class = c("XY", "MULTILINESTRING",
"sfg")), structure(c(-2.94368279166516, -2.94320298450354,
-2.9432258084364, -2.94327592253373, -2.94337226543638, -2.94347479608075,
-2.94341228732479, -2.9437491617188, -2.9438547187328, -2.9441710582173,
-2.94411183934458, -2.94402313529268, -2.94392094322372,
-2.94390413426354, -2.94396915978398, -2.94408578851656,
-2.94421922790838, -2.94438187987448, -2.94481032409716,
-2.94532148602622, -2.94571861309263, -2.9458163103159, -2.94576694513058,
-2.94533487138086, -2.9450376750598, -2.94432397728998, -2.94397161839561,
-2.94368279166516, 53.6793020108077, 53.678857027523, 53.6787355609611,
53.6786927163903, 53.6786616359016, 53.6786790221214, 53.6781670777615,
53.6781583610737, 53.678539583114, 53.6782975501525, 53.6781615690503,
53.6780773665502, 53.6780751396856, 53.678008564149, 53.6779474085154,
53.6779101041121, 53.6779393741804, 53.6779047395532, 53.6778801367235,
53.6779003636815, 53.6778456833108, 53.677875234542, 53.6779514282698,
53.6782701826325, 53.6784544569505, 53.6789694863035, 53.6791996753384,
53.6793020108077), dim = c(28L, 2L), class = c("XY", "LINESTRING",
"sfg")), structure(c(-2.77026256525726, -2.77032326917765,
-2.77041002789989, -2.77043165522445, -2.77039592888796,
-2.77006080010197, -2.76950415373603, -2.76906915073792,
-2.76780760934842, -2.76748858307524, -2.76743110189235,
53.9067111927087, 53.9069910447305, 53.9070736254268, 53.9072398520057,
53.9073167479499, 53.9076341040454, 53.908007526416, 53.9081504365657,
53.9084934043941, 53.9086858978806, 53.9087804581627), dim = c(11L,
2L), class = c("XY", "LINESTRING", "sfg"))), n_empty = 0L, crs = structure(list(
input = NA_character_, wkt = NA_character_), class = "crs"), class = c("sfc_GEOMETRY",
"sfc"), precision = 0, bbox = structure(c(xmin = -2.9458163103159,
ymin = 53.6778456833108, xmax = -1.67991922565183, ymax = 55.1699800665141
), class = "bbox"), classes = c("LINESTRING", "MULTILINESTRING",
"LINESTRING", "LINESTRING"))), row.names = c(NA, -4L), class = c("sf",
"data.frame"), sf_column = "Geometry", agr = structure(c(Asset_ID = NA_integer_,
System_Name = NA_integer_, Asset_Description = NA_integer_, Asset_Name = NA_integer_,
Condition = NA_integer_), class = "factor", levels = c("constant",
"aggregate", "identity")))
The are several options to add your popups. Personally I would add a column with the labels to your dataframe. In my code below I use sprintf for that but paste0 would also work.
Note: I also made your example more minimal to focus on the popup issue by getting rid of all the unnecessary layers.
library(leaflet)
library(sf)
df <- st_as_sf(df, wkt = "Geometry") # convert to spatial data frame
df$popup <- with(df, sprintf("Asset Name: %s</br>Asset Description: %s", Asset_Name, Asset_Description))
leaflet(df) %>%
addTiles(group = "OSM (default)") %>%
addPolylines(
color = "blue",
weight = 20,
popup = ~popup,
group = "Polylines"
)

Remove specific markers from legend

Sorry if this question has already been answered but I could not find the solution to what I am after. I have a plot that uses both geom_line and geom_point. The result of this is that in the legend, it adds both a line and a point when they should have one or the other. I want to keep the circles for the data tg1 and tg2 and remove the line and then do the opposite to the data full i.e. keep the line but remove the circle. I have seen that something like this works where you want to remove dots from all of the legend entries but nothing to only do specifics Removing ggplot2's geom_point icons from the legend. Can anyone help? Thanks.
#code for plot
library(ggplot2)
library(tidypaleo)
ggplot(LGRSL, aes(x =mmsl , y = Age))+
coord_flip()+
theme_classic(12)+
geom_point(data=tg1,aes(x=mmslc,y=Year,col="Fort Denison 1"),pch=1,size=2)+
geom_point(data=tg2,aes(x=mmslc,y=Year,col="Fort Denison 2"),pch=1,size=2)+
geom_lineh(data = full, aes(x=Lutregalammslc,y=Year,col="Full budget"))+
scale_colour_manual(values=c("grey15","grey50","black"))
## data
## tg1
structure(list(Year = 1886:1891, SLR = c(6919L, 6935L, 6923L,
6955L, 6956L, 6957L), mmsl = c(-0.158, -0.142, -0.154, -0.122,
-0.121, -0.12), m = c(6.919, 6.935, 6.923, 6.955, 6.956, 6.957
), GIA.correction = c(-0.02814, -0.02793, -0.02772, -0.02751,
-0.0273, -0.02709), SLRc = c(6.89086, 6.90707, 6.89528, 6.92749,
6.9287, 6.92991), mmslc = c(-0.19667, -0.18046, -0.19225, -0.16004,
-0.15883, -0.15762)), row.names = c(NA, 6L), class = "data.frame")
##tg2
structure(list(Year = 1915:1920, SLR = c(7011L, 6929L, 6987L,
6945L, 6959L, 6951L), mmsl = c(-0.066, -0.148, -0.09, -0.132,
-0.118, -0.126), m = c(7.011, 6.929, 6.987, 6.945, 6.959, 6.951
), GIA.correction = c(-0.02205, -0.02184, -0.02163, -0.02142,
-0.02121, -0.021), SLRc = c(6.98895, 6.90716, 6.96537, 6.92358,
6.93779, 6.93), mmslc = c(-0.09858, -0.18037, -0.12216, -0.16395,
-0.14974, -0.15753)), row.names = c(NA, 6L), class = "data.frame")
##full
structure(list(Year = 1900:1905, Lutregala = c(-0.103609677,
-0.118603251, -0.134550791, -0.105553735, -0.103983082, -0.121731984
), Wapengo = c(-0.095213147, -0.096005337, -0.115700625, -0.097696891,
-0.084444784, -0.109161066), Tarra = c(-0.106672829, -0.109537943,
-0.135256365, -0.101357772, -0.089716518, -0.104258351), Lutregalammsl = c(-0.292863465,
-0.307857039, -0.323804579, -0.294807523, -0.29323687, -0.310985772
), Wapengommsl = c(-0.257028279, -0.257820469, -0.277515756,
-0.259512023, -0.246259916, -0.270976198), Tarrammsl = c(-0.30925682,
-0.312121933, -0.337840355, -0.303941762, -0.292300508, -0.306842342
), LgGIAc = c(-0.01921, -0.01904, -0.01887, -0.0187, -0.01853,
-0.01836), WapGIAc = c(-0.02486, -0.02464, -0.02442, -0.0242,
-0.02398, -0.02376), TarGIAc = c(-0.02373, -0.02352, -0.02331,
-0.0231, -0.02289, -0.02268), Lutregalammslc = c(-0.312073465,
-0.326897039, -0.342674579, -0.313507523, -0.31176687, -0.329345772
), Wapmmslc = c(-0.281888279, -0.282460469, -0.301935756, -0.283712023,
-0.270239916, -0.294736198), Tarmmslc = c(-0.33298682, -0.335641933,
-0.361150355, -0.327041762, -0.315190508, -0.329522342)), row.names = c(NA,
6L), class = "data.frame")
##LGRSL
structure(list(depths = c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5), RSL = c(0.047746907,
0.025564293, 0.021733558, 0.007855661, -0.004909879, 0.01747051
), RSLerror = c(0.058158556, 0.057902654, 0.057988654, 0.057957388,
0.057905405, 0.057226072), Age = c(2017.456716, 2013.594255,
2006.92838, 1999.675523, 1994.729181, 1990.518154), Ageerror = c(0.373138707,
0.77640096, 1.430582242, 1.627131115, 3.222393394, 3.239674718
), mmsl = c(0.01993169, -0.002250924, -0.006081659, -0.019959556,
-0.032725096, -0.010344707)), row.names = c(NA, 6L), class = "data.frame")
##LGRSLgp
structure(list(Age = 1892:1897, mean = c(-0.298147401, -0.304630597,
-0.31023294, -0.315506983, -0.321225142, -0.327190675), error = c(0.051858047,
0.04985084, 0.047760525, 0.045624121, 0.043505044, 0.041477551
), min = c(-0.246289354, -0.254779758, -0.262472416, -0.269882862,
-0.277720098, -0.285713124), max = c(-0.350005447, -0.354481437,
-0.357993465, -0.361131103, -0.364730186, -0.368668226), x = c(-0.02125,
-0.02108, -0.02091, -0.02074, -0.02057, -0.0204), meangia = c(-0.276897401,
-0.283550597, -0.28932294, -0.294766983, -0.300655142, -0.306790675
), rate = c(NA, -4.967327, -4.946326, -4.964493, -4.977451, -4.911859
), raterror = c(NA, 3.581013, 3.796417, 4.022157, 4.226762, 4.255126
), mmsl = c(-0.325962618, -0.332445814, -0.338048157, -0.3433222,
-0.349040359, -0.355005892)), row.names = c(NA, 6L), class = "data.frame")
Here is a way.
Override the guide legend with a list of vectors of values for each of the aesthetics involved, shape and linetype. Note the different ways to specify what is to be removed.
I have also simplified the code a bit.
library(ggplot2)
library(dplyr)
colrs <- c("Fort Denison 1" = "grey15",
"Fort Denison 2" = "grey50",
"Full budget" = "black")
legnd <- list(shape = c(1, 1, NA),
linetype = c("blank", "blank", "solid"))
bind_rows(
tg1 %>% mutate(col = "Fort Denison 1"),
tg2 %>% mutate(col = "Fort Denison 2")
) %>%
ggplot(aes(x = mmslc, y = Year, colour = col)) +
geom_point(pch = 1, size = 2) +
geom_lineh(data = full, aes(x = Lutregalammslc, col = "Full budget"))+
scale_colour_manual(values = colrs,
guide = guide_legend(override.aes = legnd)) +
coord_flip() +
theme_classic(base_size = 12)

Need plotly colorbar at the bottom

Here's a dataframe, and the rendered plot using plotly,
I went through the attributes mentioned on plotly R reference guide but couldn't help myself.
require(plotly)
require(dplyr)
df <- structure(list(nn = c(5, 10, 20), Sparsity = c(93.7, 94.3, 94.9
), Method = c("adjcos", "adjcos", "adjcos"), Filtering = c("IBCF",
"IBCF", "IBCF"), error = c("MAE", "MAE", "MAE"), `5` = c(0.716433082,
0.746625993, 0.786221009), `10` = c(0.709316631, 0.737244112,
0.784197414), `20` = c(0.711888414, 0.73875951, 0.783307894),
`30` = c(0.716054462, 0.741430884, 0.78574634), `50` = c(0.722296008,
0.745516396, 0.788986052), `70` = c(0.727236608, 0.749367689,
0.792077439), `100` = c(0.73261171, 0.75337796, 0.795475065
), `150` = c(0.738988515, 0.75764295, 0.799090637), `200` = c(0.743606268,
0.760689524, 0.801416126)), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
plot_ly(x = ~df$Sparsity, y = ~df$nn, z = ~as.matrix(df[,-c(1:5)])) %>%
add_surface()
Plot I obtained
I need the colorbar/scale (referenced here colorbar attributes) at the bottom of the plot, directly rendered from the plot_ly().
as shown below :

How to create new column from existing column value in R dynamically

i have data frame called df,how to create new column from existing list column data frame.
my data frame.
Policy Item
Checked list(Processed = "Valid", Gmail = "yy#gmail", Information = list(list(Descrption = "T1, R1", VID = "YUY")))
Sample list(Processed = "Valid", Gmail = "tt#gmail", Information = list(list(Descrption = "D3, Y3", VID = "RT")))
Processed list(Processed = "Valid", Gmail = "pp#gmail", Information = list(list(Descrption = "Y2, LE", VID = "UIU")))
my expected data frame.
Policy Processed Gmail Descrption VID
Checked Valid yy#gmail "T1,R1" "YUY"
Sample Valid tt#gmail "D3,Y3" "RT"
Processed Valid pp#gmail "Y2,LE" "UIU"
i'm using below code to get my expected dataframe .
na_if_null <- function(x) if (is.null(x)) NA else x
new_cols <- lapply(
Filter(is.list, df),
function(list_col) {
names_ <- setNames(nm = unique(do.call(c, lapply(list_col, names))))
lapply(names_, function(name) sapply(list_col, function(x)
trimws(na_if_null(as.list(x)[[name]]))))
}
)
res <- do.call(
data.frame,
c(
list(df, check.names = FALSE, stringsAsFactors = FALSE),
do.call(c, new_cols)
)
)
But i'm getting below Data frame.please help me to done my post.
Policy Item Item.Processed Item.Gmail Item.Information
Checked list(Processed = "Valid", Gmail = "yy#gmail", Information = list(list(Descrption = "T1, R1", VID = "YUY"))) Processed yy#gmail list(Descrption = "T1, R1", VID = "YUY")
Sample list(Processed = "Valid", Gmail = "tt#gmail", Information = list(list(Descrption = "D3, Y3", VID = "RT"))) Processed tt#gmail list(Descrption = "D3, Y3", VID = "RT")
Processed list(Processed = "Valid", Gmail = "pp#gmail", Information = list(list(Descrption = "Y2, LE", VID = "UIU"))) Processed pp#gmail list(Descrption = "Y2, LE", VID = "UIU")
dput
structure(list(Policy = c("Checked", "Sample", "Processed"), Item = list(
structure(list(Processed = "Valid", Gmail = "yy#gmail", Information = list(
structure(list(Descrption = "T1, R1", VID = "YUY"), .Names = c("Descrption",
"VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
"Gmail", "Information"), class = "data.frame", row.names = 1L),
structure(list(Processed = "Valid", Gmail = "tt#gmail", Information = list(
structure(list(Descrption = "D3, Y3", VID = "RT"), .Names = c("Descrption",
"VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
"Gmail", "Information"), class = "data.frame", row.names = 1L),
structure(list(Processed = "Valid", Gmail = "pp#gmail", Information = list(
structure(list(Descrption = "Y2, LE", VID = "UIU"), .Names = c("Descrption",
"VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
"Gmail", "Information"), class = "data.frame", row.names = 1L))), row.names = c(NA,
3L), class = "data.frame", .Names = c("Policy", "Item"))
Sample data frame
Policy colval Item
Checked list(PID="4",Bdetail ="ui,89") list(Processed = "Valid", Gmail = "yy#gmail", Information = list(list(Descrption = "T1, R1", VID = "YUY")))
Sample list(PID="7",Bdetail ="ju,78") list(Processed = "Valid", Gmail = "tt#gmail", Information = list(list(Descrption = "D3, Y3", VID = "RT")))
Processed list(PID ="8",Bdetail ="nj,45") list(Processed = "Valid", Gmail = "pp#gmail", Information = list(list(Descrption = "Y2, LE", VID = "UIU")))
Here a solution in base R:
dd <-
cbind(
dx$Policy,
do.call(rbind,
lapply(seq_len(nrow(dx)), function(i)unlist(dx$Item[i]))
)
)
colnames(dd) <- c("Policy","Processed","Gmail","Descrption","VID")
dd
# Policy Processed Gmail Descrption VID
# [1,] "Checked" "Valid" "yy#gmail" "T1, R1" "YUY"
# [2,] "Sample" "Valid" "tt#gmail" "D3, Y3" "RT"
# [3,] "Processed" "Valid" "pp#gmail" "Y2, LE" "UIU"
Basically I am using unlist for each item. and Then joining them using the classic d.call(rbind,llist).
edit
in case you want tu use the same names as the original sub lists you can do something like :
colnames(dd) <- c("Policy",gsub(".*[.]","",colnames(dd)[-1]))
data.table solution
library(data.table)
setDT(dx)
dx[, rbindlist(lapply(.SD,function(x)data.table(t(unlist(x))))),Policy]
Easily done with unnest from tidyr:
library(dplyr)
library(tidyr)
df %>%
unnest() %>%
unnest()
Result:
Policy Processed Gmail Descrption VID
1 Checked Valid yy#gmail T1, R1 YUY
2 Sample Valid tt#gmail D3, Y3 RT
3 Processed Valid pp#gmail Y2, LE UIU
Data:
df = structure(list(Policy = c("Checked", "Sample", "Processed"), Item = list(
structure(list(Processed = "Valid", Gmail = "yy#gmail", Information = list(
structure(list(Descrption = "T1, R1", VID = "YUY"), .Names = c("Descrption",
"VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
"Gmail", "Information"), class = "data.frame", row.names = 1L),
structure(list(Processed = "Valid", Gmail = "tt#gmail", Information = list(
structure(list(Descrption = "D3, Y3", VID = "RT"), .Names = c("Descrption",
"VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
"Gmail", "Information"), class = "data.frame", row.names = 1L),
structure(list(Processed = "Valid", Gmail = "pp#gmail", Information = list(
structure(list(Descrption = "Y2, LE", VID = "UIU"), .Names = c("Descrption",
"VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed",
"Gmail", "Information"), class = "data.frame", row.names = 1L))), row.names = c(NA,
3L), class = "data.frame", .Names = c("Policy", "Item"))
Note:
Notice I used two passes of unnest because there are two levels of lists in your original dataframe. unnest automatically flattens all lists in the dataframe and reuses the names, but it does not do it recursively, so you will have to have as many unnest as there are list levels.

Resources