I have R code which creates a leaflet map with points connected by addPolylines().
library(shiny)
library(leaflet)
station = c("A", "B", "C", "D", "E", "F")
latitude = c(-1.63, -1.62, -1.62, -1.77, -1.85, -1.85)
longitude = c(34.3, 34.4, 34.7, 34.3, 34.5, 34.7)
big = c(0, 20, 60, 90, 50, 10)
small = c(100, 80, 40, 10, 50, 90)
colour = c("blue", "blue", "red", "red", "black", "black")
group = c("A", "A", "B", "B", "C", "C")
df = cbind.data.frame(station, latitude, longitude, big, small, colour, group)
colnames(df) = c("station", "latitude", "longitude", "big", "small", "colour", "group")
myMap = leaflet() %>%
setView(lng = 34.4, lat = -1.653, zoom = 8) %>%
addTiles()%>%
addCircles(data = df,
lng = ~ longitude, lat = ~ latitude,
color = ~ colour,
radius = 2000,
stroke = TRUE,
opacity = 5,
weight = 1,
fillColor = ~ colour,
fillOpacity = 1)
for(group in levels(df$group)){
myMap = addPolylines(myMap,
lng= ~ longitude,
lat= ~ latitude,
data = df[df$group == group,],
color= ~ colour,
weight = 3)
}
myMap
This is exactly what I am wanting and it looks like this:
However, when I put this into an R shiny app, the map will not appear. The ui code is:
fluidPage(
theme = shinythemes::shinytheme("yeti"),
titlePanel(title = "Polyline Map"))
mainPanel("",
helpText("This is the polyline map"),
hr(),
leafletOutput("myMap", height = 400, width = 600)
)
The server code is:
function(input, output, session) {
output$myMap = renderLeaflet({
leaflet() %>%
setView(lng = 34.4, lat = -1.653, zoom = 8) %>%
addTiles()%>%
addCircles(data = df,
lng = ~ longitude, lat = ~ latitude,
color = ~ colour,
radius = 4000,
stroke = TRUE,
opacity = 5,
weight = 1,
fillColor = ~ colour,
fillOpacity = 0.5)
for(group in levels(df$group)){
myMap = addPolylines(myMap,
lng= ~ longitude,
lat= ~ latitude,
data = df[df$group==group,],
color= ~ colour,
weight = 3)
}
}
)}
And the global code is:
library(shiny)
library(leaflet)
station = c("A", "B", "C", "D", "E", "F")
latitude = c(-1.63, -1.62, -1.62, -1.77, -1.85, -1.85)
longitude = c(34.3, 34.4, 34.7, 34.3, 34.5, 34.7)
big = c(0, 20, 60, 90, 50, 10)
small = c(100, 80, 40, 10, 50, 90)
colour = c("blue", "blue", "red", "red", "black", "black")
group = c("A", "A", "B", "B", "C", "C")
df = cbind.data.frame(station, latitude, longitude, big, small, colour, group)
colnames(df) = c("station", "latitude", "longitude", "big", "small", "colour", "group")
Does anyone know why this happens and what I can do to fix it? Thank you!
I was able to get your code working with two very small adjustments:
You refer to myMap in your renderLeaflet function but that is not defined yet, so I modified the first line to myMap <- leaflet() %>%
You do not return anything from the renderLeaflet function, so I added the statement myMap after the for-loop.
Working code is shown below, hope this helps!
library(shiny)
library(leaflet)
station = c("A", "B", "C", "D", "E", "F")
latitude = c(-1.63, -1.62, -1.62, -1.77, -1.85, -1.85)
longitude = c(34.3, 34.4, 34.7, 34.3, 34.5, 34.7)
big = c(0, 20, 60, 90, 50, 10)
small = c(100, 80, 40, 10, 50, 90)
colour = c("blue", "blue", "red", "red", "black", "black")
group = c("A", "A", "B", "B", "C", "C")
df = cbind.data.frame(station, latitude, longitude, big, small, colour, group)
colnames(df) = c("station", "latitude", "longitude", "big", "small", "colour", "group")
ui <- fluidPage(
theme = shinythemes::shinytheme("yeti"),
titlePanel(title = "Polyline Map"),
mainPanel("",
helpText("This is the polyline map"),
hr(),
leafletOutput("myMap", height = 400, width = 600)
)
)
server <- function(input, output, session) {
output$myMap = renderLeaflet({
myMap <- leaflet() %>%
setView(lng = 34.4, lat = -1.653, zoom = 8) %>%
addTiles()%>%
addCircles(data = df,
lng = ~ longitude, lat = ~ latitude,
color = ~ colour,
radius = 4000,
stroke = TRUE,
opacity = 5,
weight = 1,
fillColor = ~ colour,
fillOpacity = 0.5)
for(group in levels(df$group)){
myMap = addPolylines(myMap,
lng= ~ longitude,
lat= ~ latitude,
data = df[df$group==group,],
color= ~ colour,
weight = 3)
}
myMap
})
}
shinyApp(ui,server)
Related
does anyone know how to deal with line charts in log scale where there are zero values in plotly? The lines sort of just disappear.
library(tidyverse)
library(lubridate)
library(plotly)
df2 <- tibble::tribble(
~SAMPLE_DATE, ~REPORT_RESULT_VALUE,
"2018-10-04", 0.05,
"2019-05-05", 0.01,
"2019-10-04", 0,
"2020-06-05", 0.01,
"2020-09-11", 0,
"2021-04-23", 0,
"2022-05-08", 0.06 ) %>%
mutate(SAMPLE_DATE = ymd(SAMPLE_DATE))
plot_ly(data = df2) %>%
add_trace(x = ~SAMPLE_DATE,
y = ~REPORT_RESULT_VALUE,
mode = "lines+markers") %>%
layout(xaxis = list(title = 'Sample date'),
yaxis = list(title = "Concentration (mg/L)",
type = "log"))
I found a similar post in the plotly forum a while ago, but no solution: https://community.plotly.com/t/line-chart-with-zero-in-logarithmic-scale/40084
-----------------------
An extra example based on Jon Spring's edited answer.
df3 <- tibble::tribble(
~SAMPLE_DATE, ~REPORT_RESULT_VALUE, ~CHEMICAL_NAME,
"2018-10-04", 0.05, "a",
"2019-05-05", 0.01, "a",
"2019-10-04", 0, "a",
"2020-06-05", 0.01, "a",
"2020-09-11", 0, "a",
"2021-04-23", 0, "a",
"2022-05-08", 0.06, "a",
"2018-10-04", 95, "b",
"2019-05-05", 90, "b",
"2019-10-04", 80, "b",
"2020-06-05", 91, "b",
"2020-09-11", 90, "b",
"2021-04-23", 90, "b",
"2022-05-08", 96, "b",
"2018-10-04", 9.5, "c",
"2019-05-05", 9.0, "c",
"2019-10-04", 8.0, "c",
"2020-06-05", 9.1, "c",
"2020-09-11", 9.0, "c",
"2021-04-23", 9.0, "c",
"2022-05-08", 9.6, "c") %>%
mutate(SAMPLE_DATE = ymd(SAMPLE_DATE))
ggplotly(
ggplot(df3, aes(SAMPLE_DATE, REPORT_RESULT_VALUE, colour = CHEMICAL_NAME)) +
geom_line() +
geom_point() +
scale_y_continuous(trans = scales::pseudo_log_trans(sigma = 0.1),
breaks = scales::breaks_pretty(n = 10)) +
labs(x = 'Sample date', y = "Concentration (mg/L)")
)
Here ideally I would like to have the labels spread out more.
Here's a way to do it in ggplot2 using the handy scales::pseudo_log_trans function and then using plotly::ggplotly to convert to plotly. pseudo_log_trans is handy when you want a (mostly) log scale but you want to accommodate zeroes or even negative values.
ggplotly(
ggplot(df2, aes(SAMPLE_DATE, REPORT_RESULT_VALUE)) +
geom_line() +
geom_point() +
scale_y_continuous(trans = scales::pseudo_log_trans(sigma = 0.005),
breaks = scales::breaks_pretty(n=10), # EDIT
labels = scales::number_format()) +
labs(x = 'Sample date', y = "Concentration (mg/L)")
)
Would removing zero work for you?
plot_ly(data = df2 %>% filter(REPORT_RESULT_VALUE > 0)) %>%
add_trace(x = ~SAMPLE_DATE,
y = ~REPORT_RESULT_VALUE,
mode = "lines+markers",
na.rm = TRUE) %>%
layout(xaxis = list(title = 'Sample date'),
yaxis = list(title = "Concentration (mg/L)",
type = "log"))
Created on 2022-12-22 with reprex v2.0.2
I don't know why I have a double legend.
How to remove the second legend where it is written Aa
Code:
city = c("paris", "lyon", "lyon", "marseille", "lille", "toulouse", "bordeaux")
start = c("2018-08-04", "2018-07-25", "2018-07-30", "2018-07-29", "2018-08-03", "2018-08-04", "2018-08-03")
max = c(36.4, 37.2, 38.4, 37.4, 36.7, 34.9, 34.8)
duration = c(4, 3, 8, 10, 5, 4, 3)
tab = data.frame(city, start, duration, max)
tab$duration = as.integer(tab$duration)
t <- list(
family = "sans serif",
size = 14,
color = toRGB("grey50"))
bubbleplot <- plot_ly(tab, x = ~start, y = ~max,
text = ~paste(duration, "jours"),
color = ~city, mode='markers+text'
)
bubbleplot = bubbleplot %>% add_markers(
marker = list(size = ~duration*4, opacity = 0.7,
sizemode = "diameter"))
bubbleplot <- bubbleplot %>% add_text(textfont = t, textposition = "inside")
bubbleplot
Use the following,
t <- list(
family = "sans serif",
size = 14,
color = toRGB("grey50"))
bubbleplot <- plot_ly(tab, x = ~start, y = ~max,
text = ~paste(duration, "jours"),
color = ~city, mode='markers+text'
)
bubbleplot = bubbleplot %>% add_markers(
marker = list(size = ~duration*4, opacity = 0.7,
sizemode = "diameter")) %>% add_text(x = ~start, y = ~max,
text = ~paste(duration, "jours"), textfont = t, textposition = "inside", data = tab, inherit = F)
bubbleplot
Let's say, I have a simple polar chart:
R code:
library(plotly)
p <- plot_ly(
type = 'scatterpolar',
r = c(0,1,2,4),
theta = c(0,45,90,120),
size = c(10, 20, 30, 40),
sizes = c(100, 300),
mode = 'markers'
) %>%
layout(
showlegend = FALSE,
polar = list(
angularaxis = list(
showticklabels = TRUE#,
#tickmode="array",
#tickvals = c(22.5, 67.5, 112, 157.5, 202, 247.5, 292, 337.5),
#ticktext = c('A', "B", "C", "D", "E", "F", "G", "H")
),
radialaxis = list(
tickmode="array",
tickvals = c(0, 1, 2, 3, 4, 5, 6, 7),
ticktext = c('', "One", "Two", "Three", "Four", "Five", "Six", "Seven")
)
)
)
ggplotly(p)
Chart plotted:
When I set showticklabels = FALSE, the angle tick labels disappears, and then I want to put A,B,C,D,E,F,G and H at angles c(22.5, 67.5, 112, 157.5, 202, 247.5, 292, 337.5).
I am not able to get the below expected plot using ticktexts and tickvals.
Can someone please help me with getting me to the solution, or if it is possible with add_text or add_annotation ?
Expected plot:
you could remove all grid lines altogether in the angularaxis with showgrid = FALSE, or you can have a line per 22.5 degree starting from 0 and then the ticktext would be something like this c('', 'A', '', 'B', '', 'C', .......) , or you could tediously add the lines you expect to have and then remove the grid lines like this:
p <- plot_ly() %>%
# data points
add_trace(type = 'scatterpolar',
r = c(0,1,2,4),
theta = c(0,45,90,120),
size = c(10, 20, 30, 40),
sizes = c(100, 300),
mode = 'markers') %>%
# straight line from 0 dg to 180 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 180, 360),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# straight line from 45 dg to 225 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 45, 225),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# straight line from 90 dg to 270 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 90, 270),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# straight line from 135 dg to 315 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 135, 315),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# fill circle of radius 1
add_trace(type = 'scatterpolar',
mode = 'lines',
r = 1,
theta =seq(0, 360, 0.1),
line = list(color = 'grey'),
fill = 'toself',
fillcolor = 'grey',
opacity = 0.5) %>%
layout(
showlegend = FALSE,
polar = list(
angularaxis = list(
showticklabels = TRUE,
# remove grid lines and ticks
showgrid = FALSE,
ticks = '',
# if you want the axis to go the other way around
# direction = 'clockwise',
tickmode="array",
tickvals = seq(22.5, 337.5, 45),
ticktext = LETTERS[1:8]
),
radialaxis = list(
tickmode="array",
tickvals = c(0, 1, 2, 3, 4, 5, 6, 7),
ticktext = c('', "One", "Two", "Three", "Four", "Five", "Six", "Seven")
)
)
)
ggplotly(p)
Output chart:
I noticed that the expected output you have there, lists the letters the other way around as you have them in your code. If this is something you also want just change the order of the letters to match the angles like this c("A", "H", "G", "F", "E", "D", "C", "B") (reversed order starting from A)
I'm looking for a way to sort my plotly bar plot by yaxis alphabetically. I tried several ways but the order of y is still from z to a, instead of a to z. Please help me out!
qt <- c("A", "C", "B","B","A", "C", "C")
y <- c("q1", "q2", "q3", "q4", "q5", "q6", "q7")
x1 <- c(20, 10, 15, 15, 20, 10, 15)
x2 <- c(10, 20, 20, 10, 10, 30, 10)
x3 <- c(10, 10, 5, 10, 10, 5, 5)
x4 <- c(20, 25, 25, 35, 55, 40, 35)
x5 <- c(40, 35, 35, 30, 5, 15, 35)
df <- data.frame(qt, y, x1, x2, x3, x4, x5)
df$qt <- factor(df$qt, levels = c("A", "B", "C"))
plot_ly(df) %>%
add_trace(x = ~x1, y = ~y, marker = list(color = 'rgb(202,0,32)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x2, y = ~y, marker = list(color = 'rgb(244,165,130)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x3, y = ~y, marker = list(color = 'rgb(223,223,223)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x4, y = ~y, marker = list(color = 'rgb(146,197,222)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x5, y = ~y, marker = list(color = 'rgb(5,113,176)'), type = 'bar', orientation = 'h') %>%
layout(title="mytitle",
xaxis = list(title = "",
showticklabels = TRUE,
zeroline = FALSE,
domain = c(0.15, 1)),
yaxis = list(title = "",
showticklabels = FALSE,
zeroline = FALSE),
barmode = 'relative',
paper_bgcolor = 'rgb(248, 248, 255)', plot_bgcolor = 'rgb(248, 248, 255)',
autosize=T,
margin = list(l = 150, r = 50, t = 100, b = 50),
showlegend=F) %>%
# labeling the y-axis
add_annotations(xref = 'paper', yref = 'y', x = 0.14, y = df$y,
xanchor = 'right',
text = df$y,
font = list(family = 'Arial', size = 15,
color = 'rgb(67, 67, 67)'),
showarrow = FALSE, align = 'right')%>%
#labeling the y-axis (category)
add_annotations(xref = 'paper', yref = 'qt', x = 0.01, y = df$y,
xanchor = 'right',
text = df$qt,
font = list(family = 'Arial', size = 15,
color = 'rgb(67, 67, 67)'),
showarrow = FALSE, align = 'right')
The primary goal I would like to accomplish is to order this by the variable qt (from A to C). But if this is impossible, ordering the plot by y is also desirable (from q1 to q7). My plot looks like this:
Thank you in advance!
I have a nice graph with DiagrammeR in R studio, but the nodes are too clustered togather. I have searched everywhere but I cannot find a way of increasing the distance between them. Can I be shown?
Here is my code:
library(magrittr)
library(DiagrammeR)
# Create a simple NDF
nodes <- create_nodes(nodes = c("Index", "Surveillance", "Intervention","Lost"),
label = TRUE,
fontsize=55,
type = "lower",
style = "filled",
color = "aqua",
shape = c("circle", "circle",
"rectangle", "rectangle"),
data = c(30.5, 2.6, 9.4, 2.7))
edges <- create_edges(from = c("Index", "Surveillance","Surveillance","Intervention", "Surveillance", "Index" ),
to = c("Surveillance", "Intervention","Surveillance","Intervention", "Lost", "Lost"),
rel = c(99, 6.7, 99, 99, 27, 22),
arrowhead = rep("normal", 6),
color = c("green", "green", "red", "red", "red", "red"))
graph <-
create_graph(
nodes_df = nodes,
edges_df = edges,
graph_attrs <-
c("layout = dot","overlap = FALSE","outputorder = edgesfirst"),
node_attrs <-
c("shape = circle",
"fixedsize = TRUE",
"width = 100",
"penwidth = 1",
"color = DodgerBlue",
"style = filled",
"fillcolor = Aqua",
"alpha_fillcolor = 0.5",
"fontname = Helvetica",
"fontcolor = Black"),
edge_attrs = "color = gray20")
# View the graph
render_graph(graph,layout=constant,output="visNetwork")
You could just set the length for the arrows between different nodes:
edges <- create_edges(from = c("Index", "Surveillance","Surveillance","Intervention", "Surveillance", "Index" ),
to = c("Surveillance", "Intervention","Surveillance","Intervention", "Lost", "Lost"),
rel = c(99, 6.7, 99, 99, 27, 22),
arrowhead = rep("normal", 6),
color = c("green", "green", "red", "red", "red", "red"),
length = c(200,200,50,50,200,200))
Or you could define a precise spot for each node:
nodes <- create_nodes(nodes = c("Index", "Surveillance", "Intervention","Lost"),
label = TRUE,
fontsize = 55,
type = "lower",
style = "filled",
color = "aqua",
shape = c("circle", "circle",
"rectangle", "rectangle"),
data = c(30.5, 2.6, 9.4, 2.7),
x = c(-80,80,-80,80),
y = c(-80,80,80,-80))