Bokeh vbar Hover tool - plot

I have a multiple data bar graph and want to show the max value of each bar upon hover. I have already changed the code a couple of times, but never succeeded...
Here is the code :
'''
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Panel, Tabs
from bokeh.models.tools import HoverTool
modalites = ['0-510','511-1002', '1003-2167', '>2168']
valeur1 = list(tables.loc[0])
valeur2 = list(tables.loc[1])
valeur3 = list(tables_2.loc[0])
valeur4 = list(tables_2.loc[1])
source1 = ColumnDataSource({'x' : modalites, 'valeur 1' : valeur1, 'valeur 2' : valeur2})
source2 = ColumnDataSource({'x' : modalites, 'valeur 1' : valeur3, 'valeur 2' : valeur4})
p1 = figure(title ='Répartition des sinistres en fonction des surfaces', x_range = modalites, plot_width=600, plot_height=400)
p2 = figure(title ='Répartition des sinistres en fonction des surfaces', x_range = modalites, plot_width=600, plot_height=400)
from bokeh.transform import dodge
abscisses_1 = dodge(field_name = 'x',
value = -0.25,
range = p1.x_range)
abscisses_2 = dodge(field_name = 'x',
value = 0,
range = p1.x_range)
p1.vbar(x = abscisses_1, top = 'valeur 1', width = 0.2, source = source1, color = 'green', legend = 'pas de sinistre')
p1.vbar(x = abscisses_2, top = 'valeur 2', width = 0.2, source = source1, color = "red", legend = "sinistre")
p1.legend.location = "top_left"
p1.legend.orientation = "horizontal"
p1.xgrid.grid_line_color = None
p2.vbar(x = abscisses_1, top = 'valeur 1', width = 0.2, source = source2, color = 'green', legend = 'pas de sinistre')
p2.vbar(x = abscisses_2, top = 'valeur 2', width = 0.2, source = source2, color = "red", legend = "sinistre")
p2.legend.location = "top_right"
p2.legend.orientation = "horizontal"
p2.xgrid.grid_line_color = None
tab1=Panel(child=p1, title='en %')
tab2=Panel(child=p2, title='en valeur absolue')
tabs=Tabs(tabs=[tab1, tab2])
h2 = HoverTool(tooltips = [( "nb bâtiments concernés:", "#top")])
p2.add_tools(h2)
show(tabs)
'''
If it helps, here is list(tables_2.loc[0]):[2303, 2184, 1909, 1511]
and list(tables_2.loc[1]):[271, 418, 587, 1046]

The problem of your code is in the line where you define your HoverTool. You are using #top, but in your ColumnDataSource top is not defiened.
h2 = HoverTool(tooltips = [( "nb bâtiments concernés:", "#top")])
If your want to use top as the maximum of valeur1 and valeur2 you may have to write something like this.
top = [max(v1,v2) for v1, v2 in zip(valeur3, valeur4)]
source2 = ColumnDataSource({'x' : modalites, 'valeur1' : valeur3, 'valeur2' : valeur4, 'top': top})
In this case top is well defiened and your code should work without changes.
By the way. You have defiened a HoverTool only for p2. If you need one on p1 as well, you have to do similar things on source1.
Output

Related

Remove vertex.tooltips from ndtv d3movie

I'm plotting a dynamic network visulisation using the Statnet, networkd3, and ndtv packages.
In the vignette for this package, the authors explain how to "generate interactive html tooltips to be displayed when the vertices are clicked on":
render.d3movie(
toy_epi_sim,
render.par = render.par,
plot.par = plot.par,
vertex.cex = 0.9,
vertex.col = "ndtvcol",
edge.col = "darkgrey",
vertex.border = "lightgrey",
displaylabels = FALSE,
vertex.tooltip = function(slice){paste('name:',slice%v%'vertex.names','<br>',
'status:', slice%v%'testatus')},
output.mode='htmlWidget')
However, I'd like to remove these tooltips, since by default they just show meaningless node IDs, and I'm already showing node labels instead.
Things like vertex.tooltip = F or vertex.tooltip = NA don't work - how do I turn off vertex tooltips?
This is the code I'm using:
render.d3movie(net, usearrows = T, label = "gmv_label",
displaylabels = T,
bg="#ffffff",
edge.col = "#ff0063",
vertex.col = "colo",
vertex.cex = "amount",
edge.lwd = 5,
launchBrowser=T, filename= XXX,
render.par=list(tween.frames = 30, show.time = F),
plot.par=list(mar=c(0,0,0,0)))

render tmap maps inside for loop in html output of a rmarkdown file

I'm trying to generate tmaps with a for loop inside an rmarkdown file. They will not show in the html output. I read elsewhere using the tagList function could help but in my case that did not and returned an error (i'm probably doing it wrong). Any ideas?
This attempt with tmap does not work. I'm getting this error: "Error in as.character.default(x) : no method for coercing this S4 class to a vector"
html <- list()
for (i in 1:4) {
tmap_mode("view")
html <- c(html,
list(h3(paste0("Carte_Dept_", i)),
tm_basemap(leaflet::providers$Stamen.Toner) +
tm_shape(communes_dept_pct) +
tm_fill("pct",
palette = "-RdYlGn",
style ="quantile",
title ="% bât. FG",
alpha = 0.6,
popup.vars = c("Commune:" = "nom","% bât. FG:" = "pct"),
group = "Communes") +
tm_borders(col = "black", lwd = 0.5) +
tm_shape(spdf) +
tm_dots(size = 0.05,
col = "red",
title = "Bâtiments FG",
cluster = T,
popup.vars = c("Type bâtiment:" = "tr002_type_batiment_description","Classe GES: " = "classe_estimation_ges",
"Nom méthode:" = "nom_methode_dpe"),
group = "Bâtiments F-G") +
tm_shape(spdf2) +
tm_dots(size = 0.05,
shape = 21,
col = "Domaine de travaux",
title = "Artisans RGE",
legend.show = F,
cluster = F,
popup.vars = c("Nom:" = "Nom de l'entreprise","Adresse: " = "Adresse", "Ville:" = "Ville",
"Domaine:" = "Domaine de travaux", "Effectif:" = "Tranche d'effectif salarié de l'établissement (INSEE)"),
group = "Artisans RGE")
)
)
}
tagList(html)
This does work however, using the most simple leaflet map:
html <- list()
for (i in 1:4) {
html <- c(html,
list(h3(paste0("Carte_Dept_", i)),
leaflet(communes_Occitanie_pct) %>% addProviderTiles('Stamen.Toner') %>%
setView(lng = 1.54, lat = 43.7,
zoom = 7)
)
)
}
tagList(html)
I'd like to use tmap though, so any suggestion could help...

Color markers in shades of red or green based on value R Plotly

I am trying to plot data from an outdoor activity during which headwind was measured. I'd like to show this data on a map where the activity took place with all points that have a positive headwind shown in shades of red and all points with negative headwind shown in shades of green. The shades should correspond to the headwind intensity. For locations where headwind == 0, the plot show show a marker with no fill (only boundary color = black).
The image below shows what I have so far. While I do get a plot with marker color proportional to headwind intensity, I can not get it to the shades or red or green as described above. I've tried playing around with ifelse(headwind > 0, ..., colorway and coloraxis following the examples on Plotly website but haven't been able to get what I want. I've also tried to style the markers explicitly by using
marker = list(color = ~headwind, line = list(color = 'black', width = 0.5')
I would also like to show some arrows to indicated direction along the path. This is not needed on the entire track but a few points.
I've seen some similar questions (e.g. Show negatives in Red in Plotly and R) but they are related to static collars based on positive/negative values rather than 'shades of a color depending on value' - which is what I want.
Code
plot_mapbox(data = track, mode = 'scattermapbox',
lat = ~lat, lon = ~lon, color = ~headwind, coloraxis = 'coloraxis' )%>%
layout(mapbox = list(style = 'light', zoom = 10,
center = list(lon = track[, mean(lon)], lat = track[, mean(lat)])),
margin = list(l = 0, r = 0, t = 0, b = 0),
legend = list(orientation = 'h'),
coloraxis=list(colorscale='Reds'))
Output
Data
Reduced to ~100 points.
structure(list(lat = c(45.2924274, 45.2919851, 45.2932087, 45.2914594,
45.2931871, 45.2905293, 45.2867926, 45.2851495, 45.2806734, 45.2768259,
45.2768488, 45.274695, 45.2725324, 45.2683376, 45.2629192, 45.2602198,
45.2579205, 45.2577605, 45.2569129, 45.2564982, 45.2569011, 45.256326,
45.2548296, 45.2537323, 45.2491721, 45.2440505, 45.2396301, 45.235752,
45.2338643, 45.231723, 45.2295332, 45.2278063, 45.2290903, 45.2274071,
45.2302112, 45.2331995, 45.2313716, 45.2292849, 45.2288604, 45.2275476,
45.224561, 45.2245779, 45.2247184, 45.2256043, 45.2282358, 45.2316037,
45.2339442, 45.2376578, 45.2382877, 45.2350226, 45.2402104, 45.2394283,
45.2408562, 45.2449018, 45.2491195, 45.2527104, 45.257015, 45.262031,
45.2671074, 45.2724731, 45.2757569, 45.2786716, 45.2847369, 45.2911555,
45.296165, 45.3023245, 45.3093462, 45.3128727, 45.3176175, 45.3182203,
45.3172247, 45.3150336, 45.3143277, 45.3137151, 45.3117404, 45.3092333,
45.3056604, 45.3033563, 45.3015722, 45.3011463, 45.2999077, 45.299969,
45.300848, 45.2999944, 45.2979399, 45.2949387, 45.2932506, 45.2922789,
45.2900246, 45.2906673, 45.2925133, 45.292688, 45.2932092, 45.2932651,
45.2925192, 45.2916088, 45.2920727, 45.293156, 45.2933158, 45.2928059,
45.2912818), lon = c(-74.0511001, -74.0510721, -74.0480827, -74.0462761,
-74.0497021, -74.0510732, -74.0562937, -74.0624753, -74.0680052,
-74.0731452, -74.0812064, -74.0878658, -74.0933321, -74.0972941,
-74.1011036, -74.1074512, -74.1138247, -74.1192535, -74.1262533,
-74.1305444, -74.1349428, -74.1364462, -74.142992, -74.1492447,
-74.1498239, -74.1456689, -74.1418102, -74.1448902, -74.13985,
-74.1319085, -74.1219148, -74.1147096, -74.1142337, -74.1137429,
-74.1151895, -74.1128846, -74.1031788, -74.0925199, -74.0834883,
-74.0747626, -74.071491, -74.0625856, -74.0538449, -74.0455299,
-74.0373525, -74.0315021, -74.0245414, -74.0185206, -74.0100138,
-74.0048819, -74.0079128, -74.000793, -73.9929405, -73.9870303,
-73.9807194, -73.9753671, -73.9691101, -73.9631622, -73.9575471,
-73.952474, -73.9547135, -73.9567679, -73.9520204, -73.9469607,
-73.9407129, -73.9385812, -73.9353202, -73.9335782, -73.9332331,
-73.9412661, -73.9464184, -73.9518319, -73.9598527, -73.9665792,
-73.973165, -73.9797307, -73.9858473, -73.9921346, -73.9987069,
-74.0049949, -74.0106624, -74.0172195, -74.0226944, -74.0274091,
-74.0317759, -74.0348736, -74.0381058, -74.0403379, -74.044471,
-74.0499726, -74.0510617, -74.0510679, -74.0497654, -74.0481948,
-74.0470505, -74.0462857, -74.0468899, -74.0477243, -74.0493063,
-74.0506922, -74.0508461), headwind = c(2.76940699529868, -4.29064844726939,
6.32939665765863, -2.70212357865094, -2.09430865625362, -2.94437152200737,
3.68039781522879, -2.75460681090356, -2.07118995329537, -3.89223400006753,
-5.6124577618958, -3.23990330748452, -2.88799894880197, 1.44386194468002,
-1.62757112099828, -3.96534234224143, -5.58548285989353, -5.23546206473328,
-5.04334794153736, -5.56582086006945, -6.39713309671529, -2.73204215445971,
-2.78247155160577, -5.17465320535599, 5.28479649375971, 5.39349119652677,
0.849065796538837, 6.1572886165794, 6.47933760909835, 6.28990051645839,
6.47828306146309, -5.95187923093366, -3.11387204666763, 6.52730933870623,
4.53187908395984, 6.1914653496707, 6.47203610314572, 6.51460622049559,
3.92334577299324, 5.33127767516553, 6.16537629683053, 6.14743204861233,
5.75787435417292, 5.18990225296579, 0.031201973853795, 4.0060340212202,
3.72671304639426, 3.68803811795611, 6.28794462723367, 6.13432766841712,
-3.83797593187167, 6.20074522372471, 2.25019132086052, 2.34232979604033,
2.46012726189235, 2.81030573673775, 2.29081004114046, 2.47674006029921,
1.69579455567582, 1.05875647894816, -5.71328877613563, 0.400325286795701,
0.750303681038721, 0.815375623113835, 4.24236675877916, -1.31079219711205,
0.110210983368552, -0.0828764225562778, -3.91850736541859, -5.18122542043517,
-1.39587089138698, -4.94418957442122, -5.04101231364248, -5.07277220131849,
-3.81628803351557, -3.56330772557761, -2.39925134381422, -4.10818544442121,
-4.05009025319977, -3.84240671983316, -5.08635597161047, -5.12449622177523,
-4.74598802026557, -5.17604073914986, -0.0645565019448928, -1.8515420821822,
-2.87236823446881, -4.11528743158025, -3.97830983132787, -5.13844836391256,
2.23381424164358, 1.08931403267591, 3.36153935085126, 5.1383423986223,
1.63143963861697, 5.06951911447557, -4.33050440827119, -5.04189097530029,
-4.78806209512054, -4.62849978544911, 2.3653185162215)), row.names = c(NA,
-101L), class = c("data.table", "data.frame"))
This is a bit late but could be a solution:
plot_mapbox(data = track, mode = 'scattermapbox',
lat = ~lat, lon = ~lon, color = ~headwind, colors = 'RdBu' )%>%
layout(mapbox = list(style = 'light', zoom = 10,
center = list(lon = track[, mean(lon)], lat = track[, mean(lat)])),
margin = list(l = 0, r = 0, t = 0, b = 0),
legend = list(orientation = 'h'))
Since I dont know whether you would like to have another color, you should look here (https://plotly.com/python/builtin-colorscales/). One option could be to change RdBu to PiYG. Also note that you can reverse the color map by putting a negative (-) before the variable that you define as your color.
If you do this, you can get this result (no minus sign added):
Shown above is the simpler route and allows you to get a color-bar as well. If you want to go the marker styling route, I think you can do it via the following:
plot_mapbox(data = track,
mode = 'scattermapbox',
lat = ~lat,
lon = ~lon,
color = ~headwind,
markers = list (
colorscale = "RdBu",
line = list(
color = 'black',
width = 0.5))) %>%
layout(mapbox = list(style = 'light', zoom = 10,
center = list(lon = track[, mean(lon)], lat = track[, mean(lat)])),
margin = list(l = 0, r = 0, t = 0, b = 0),
legend = list(orientation = 'h')) %>%
hide_colorbar()

R Programming: Using ISO Country Codes in LeafLet

I am working on creating a client dashboard. I have ISO country codes for the clients also I have plotted the same in the map using rworldmap package, but the UI is not very good.
So, I want to use the leaflet package. How can I use these ISO Country Codes ALPHA 2 in creating the map.
Thanks!
Leaflet does not accept ISO Alpa2 code rather accepts ISO Alpha3 codes. After going through almost everywhere I tried this and it solved my problem.
output$myMapOne = renderPlotly({
height = 1000
units="px"
clientName = input$clientSelector
conWiseSub = subset(conData, conData$GCA_CSTMR_DS == clientName)
defOne = aggregate(CNT ~ CODE, conWiseSub, sum)
d = defOne$CODE
e = defOne$CNT
# light grey boundaries
l <- list(color = toRGB("grey"), width = 0.5)
# specify map projection/options
g <- list(
showframe = TRUE,
showcoastlines = FALSE,showland = TRUE,showcountries = TRUE,
countrycolor = toRGB("white"),
landcolor = toRGB("grey85"),
projection = list(type = 'Mercator', scale =1)
)
plot_ly(defOne, z = e, text = d,locations = d, type = 'choropleth',
color = e, colors = 'PuBu', marker = list(line = l), colorbar = list(title = "SOI Distribution")
) %>%
layout( geo = g,title= paste("Region Wise SOI Distribution of", clientName , sep = " "))
})
Click Here to View the Map Created By the Code
Hope this helps!!

Why aren't event.lines showing up when using chart.TimeSeries?

I am trying to plot various events over a times series data set using chart.TimeSeries in the PerformanceAnalytics library. I have tried to follow the example from but I think I am missing something.
ExampleChart MyChart
Neither my vertical event lines nor are my period areas are showing up on the chart. My code is almost identical to the example code. Any ideas?
risk.dates = c(
"2015-04-19"
)
risk.labels = c(
"Jon Oliver's Patent Episode "
)
cycle.dates <- c ("2015-1-01/2015-01-31",
"2015-03-01/2015-03-31",
"2015-05-01/2015-05-31",
"2015-07-01/2015-07-31"
)
chart.TimeSeries(df, colorset = "darkblue",
date.format.in = "%Y-%m-%d",
legend.loc = NULL,
period.areas = cycles.dates,
period.color = "lightblue",
event.lines = risk.dates,
event.labels = risk.labels,
event.color = "red", lwd = 2,
main = "Reddit Sentiment Index",
auto.grid = FALSE)
df <- data.frame(DateTime=
c("2/17/2015","2/18/2015","2/19/2015",
"2/20/2015","2/21/2015","2/22/2015",
"2/23/2015","2/24/2015","2/25/2015",
"2/26/2015","2/27/2015","2/28/2015",
"2/28/2015","2/27/2015"),
AvgSentimentScore=
c("NA","NA","NA","NA","NA","NA","NA","NA","NA",
"8.405975696","8.313470536","8.414560335",
"8.720865075","9.284902246"))

Resources