Adding values to charts in R Leaflet.minicharts - r

I want to add values above my chart, using lealfet.minicharts package.
My current code looks like this
addMinicharts(
87.2180, -45.3496,
chartdata = c(20, 40),
colorPalette = c("darkred", "darkblue"),
width = 45, height = 45, popup = popupArgs(
labels = c("Test1", "Test2")), showLabels = TRUE, labelText = TRUE)
It gives me a chart with a labels "true" and pop-up window with labels and values. But I would like to have values (not labels) on the top of charts or inside on bars (instead of text).
Looking into documentation and cannot figure it out.
Thanks

Resolved myself.
I disabled labelText = TRUE and increased width/height. It fixed everything.
Current code
addMinicharts(
87.2180, -45.3496,
chartdata = c(20, 40),
colorPalette = c("darkred", "darkblue"),
width = 100, height = 100, popup = popupArgs(
labels = c("Test1", "Test2")), showLabels = TRUE)

Related

Add Space Between Panels of Openair windRose Plots

I created some windrose plots using the openair package and I'm pretty happy with how they turned out but aesthetically it would be nice to have some space between panels. Here's an example:
# windrose plot----
library(openair)
data("mydata")
windRose(mydata[1:144,], ws="ws", wd="wd",
paddle = F,
type = 'weekday',
key.header = 'Wind Speed (m/s)',
key.footer = "",
annotate = F,
angle = 30, # angle of "spokes"...sort of bins for wind direction
cols = 'jet',
key.position = 'right',
dig.lab = 2,
statistic = 'prop.count', #“prop.count” sizes bins according to the
# proportion of the frequency of measurements
fontsize = 20,
grid.line = 100,
max.freq = 105, # maximum value for the radial limits
key = list(header = "Wind Speed (m/s)",
footer = '',
labels = c('0 to 2', '2 to 4',
'4 to 6','6 or more'),
breaks = c(0,2,4,6)),
layout = c(6,1)
)
Anyone have any ideas of how to add space between the panels?
After some digging I found that this plot function utilizes trellis plots, here is a good rundown on them: https://www.stat.auckland.ac.nz/~ihaka/787/lectures-trellis.pdf
Specifically the xyplot function is used to create the trellis plot. The help documentation for ?xyplot shows that you can adjust the argument between to achieve spacing between panels. The between argument is a list containing x and y values that represent space between panels. Therefore we can adjust the above code simply by adding the argument between = list(x=0.25, y = 0.25) and can adjust x and y to our preference like this:
library(openair)
data("mydata")
windRose(mydata[1:144,], ws="ws", wd="wd",
paddle = F,
type = 'weekday',
key.header = 'Wind Speed (m/s)',
key.footer = "",
annotate = F,
angle = 30, # angle of "spokes"...sort of bins for wind direction
cols = 'jet',
key.position = 'right',
dig.lab = 2,
statistic = 'prop.count', #“prop.count” sizes bins according to the
# proportion of the frequency of measurements
fontsize = 20,
grid.line = 100,
max.freq = 105, # maximum value for the radial limits
key = list(header = "Wind Speed (m/s)",
footer = '',
labels = c('0 to 2', '2 to 4',
'4 to 6','6 or more'),
breaks = c(0,2,4,6)),
layout = c(6,1),
between = list(x=0.25, y=0.25)
)

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 how to increase distance between grids when using plot function?

I'm learning the Numerical Ecology with R. However, I found all my plots lacking the distance between bars or grids.
myplot
textbook's plot
spe.KM.cascade <-
cascadeKM(
spe.norm2,
inf.gr = 2,
sup.gr = 10,
iter = 100,
criterion = "ssi"
)
dev.new(
title = "CascadeKM",
width = 10,
height = 6,
noRStudioGD = TRUE
)
plot(spe.KM.cascade, sortg = TRUE, border = 'white')
I want to know how to add white borders for every grid.

How to save simpleNetwork output from networkD3 in PDF/JPEG/TIFF format?

I am new to Rstudio and have just plotted a network diagram using simpleNetwork in the networkD3 package. I am looking to save the output in PDF/JPEG/TIFF format for my PhD thesis, however, most suggestions are with respect to saving it on a website (.html).
My code is as follows:
simpleNetwork(data, Source = 1, Target = 2, height = NULL, width = NULL,
linkDistance = 120, charge = -40, fontSize = 12, fontFamily = "serif",
linkColour = "grey", nodeColour = "black", opacity = 1.0, zoom = F)
Everything runs successfully, however I am not able to save the output as a PDF/JPEG/TIFF.
Is there any way I could save it in a picture or pdf format?
Here is what I have done in the past using the 'htmlwidgets' and 'webshot' packages:
g <- simpleNetwork(data, Source = 1, Target = 2, height = NULL, width = NULL,
linkDistance = 120, charge = -40, fontSize = 12, fontFamily = "serif",
linkColour = "grey", nodeColour = "black", opacity = 1.0, zoom = F)
require(htmlwidgets)
saveWidget(g, file="name_of_your_file.html")
require(webshot)
webshot("file:///C:/Users/.../name_of_your_file.html", "name_of_your_pdf.pdf")
sometimes, a picture is worth a thousand words...

R flexdashboard gauge function won't change color

I'm trying to make a gauge with a blue color using the flexdashboard package in R, however I can't seem to change the color of the gauge. It seems that it only comes in three preset colors of success, warning, and danger. My problem is that I can't seem to change success to blue. Here's my code
library(flexdashboard)
gauge(20,
min = 0,
max = 100,
symbol = "%",
sectors = gaugeSectors(success = c(0, 0.4),
warning = c(0.4, 0.6),
danger = c(0.6, 1)
) )
You can modify colors via the colors argument in the gaugeSectors function. As the help states ?gaugeSectors:
Colors can be standard theme colors ("success", "warning", "danger",
"primary", and "info") or any other valid CSS color specifier
So you need to add a colors argument like this: colors = c("blue", rgb(0, 1, 0), "#CC664D")
And also you made a mistake while specifying the value ranges in gaugeSectors: your minimum and maximum values are 0 and 100, so you need to provide values within this range:
success = c(0, 40),
warning = c(40, 60),
danger = c(60, 100)
Please note that the symbol "%" doesn't actually convert the value to percentage, it is just a string that is printed after the value.
gauge(20,
min = 0,
max = 100,
symbol = "%",
sectors = gaugeSectors(success = c(0, 40),
warning = c(40, 60),
danger = c(60, 100),
colors = c("blue", rgb(0, 1, 0), "#CC664D")
)
)

Resources