I am trying to plot a scatter plot that when the mouse hovers over one of the points, an image, corresponding to a URL in the data appears.
Is it possible in R? it seems it is possible in python...
thanks,
Here is a solution using the Highchater package:
library(highcharter)
df <- data.frame(x = c(1, 2, 3, 4),
y = rep(0, 4),
package = c("dplyr", "shiny", "purrr", "stringr"),
urlimage = c("https://github.com/rstudio/hex-stickers/raw/master/PNG/dplyr.png",
"https://github.com/rstudio/hex-stickers/raw/master/PNG/shiny.png",
"https://github.com/rstudio/hex-stickers/raw/master/PNG/purrr.png",
"https://github.com/rstudio/hex-stickers/raw/master/PNG/stringr.png"))
hover_info <- tags$tr(
tags$th("Package"),
tags$td(paste0("{point.package}")),
tags$img(src = "{point.urlimage}", width = "125px", height = "125px")) %>%
as.character()
highchart() %>%
hc_add_series(data = df,
mapping = hcaes(x = x, y = y),
type = "scatter",
marker = list(radius = 5, symbol = "circle")) %>%
hc_tooltip(
useHTML = TRUE,
headerFormat = "<table>",
pointFormat = hover_info,
footerFormat = "</table>"
)
Output:
Related
I'm trying to add horizontal lines to an R plotly heatmap that will be located between several of the heatmap's rows.
Here's an example data.frame and a heatmap:
library(plotly)
set.seed(1)
df <- matrix(rnorm(18), nrow = 6, ncol = 3, dimnames = list(paste0("r",1:6),1:3)) %>%
reshape2::melt() %>%
dplyr::rename(row=Var1,col=Var2)
plot_ly(x = df$col, y = df$row,z = df$value,type = "heatmap")
Which gives:
Now suppose I want to add a horizontal line between "r2" and "r3" that runs across the entire heatmap, and a similar one between "r4" and "r5".
I don't know what should be the y location the corresponds to that.
I am able to get this done if my df$rows are integer/numeric rather than character:
library(plotly)
set.seed(1)
df <- matrix(rnorm(18), nrow = 6, ncol = 3, dimnames = list(1:6,1:3)) %>%
reshape2::melt() %>%
dplyr::rename(row=Var1,col=Var2)
plot_ly(x = df$col, y = df$row,z = df$value,type = "heatmap") %>%
add_lines(y = 2.5, x = c(min(df$col)-0.5,max(df$col)+0.5), line = list(color = "black",dash = "dot",size = 5),inherit = FALSE,showlegend = FALSE) %>%
add_lines(y = 4.5, x = c(min(df$col)-0.5,max(df$col)+0.5), line = list(color = "black",dash = "dot",size = 5),inherit = FALSE,showlegend = FALSE)
So my questions are:
Is there a way to place the horizontal lines between rows if the rows of the heatmap are character?
Is there a more compact way of adding multiple horizontal lines rather than explicitly having to code each one, as in my code above?
I am not sure if it would be possible to draw lines between two levels of a factor class.
As for your second question, we can use add_segments:
library(plotly)
set.seed(1)
df <- matrix(rnorm(18), nrow = 6, ncol = 3, dimnames = list(1:6,1:3)) %>%
reshape2::melt() %>%
dplyr::rename(row=Var1,col=Var2)
hdf <- data.frame(y1 = c(2.5, 4.5),
x1 = rep(min(df$col)-0.5, 2), x2 = rep(max(df$col)+0.5, 2))
plot_ly(x = df$col, y = df$row,z = df$value,type = "heatmap") %>%
add_segments(data =hdf , y=~y1, yend =~y1, x=~x1, xend =~x2,
line = list(color = "black",dash = "dot",size = 5),
inherit = FALSE,showlegend = FALSE)
I'm trying to plot some (interactive) highcharts plots for some quantmod works. Plots (in R-studio viewer tab) works fine when I "run" them. or polts then interactively on the console window, but shows nothing when I source the whole program.
How can I source the whole script and get all the plots in the viewer window?
How can I save the plots (from the script) to a pdf file?
thanks.
code example:
library(quantmod)
library(highcharter)
SPY <- getSymbols("SPY", from = Sys.Date() - lubridate::years(3), auto.assign = FALSE)
SPY <- adjustOHLC(SPY)
SPY.EMA.20 <- EMA(Cl(SPY), n = 20)
SPY.EMA.50 <- EMA(Cl(SPY), n = 50)
SPY.RSI.14 <- RSI(Cl(SPY))
SPY.RSI.SellLevel <- xts(rep(70, NROW(SPY)), index(SPY))
SPY.RSI.BuyLevel <- xts(rep(30, NROW(SPY)), index(SPY))
AAPL <- getSymbols("AAPL", from = Sys.Date() - lubridate::years(3), auto.assign = FALSE)
highchart(type = "stock") %>%
hc_add_series(AAPL, name = "AAPL", color = hex_to_rgba("red", 0.7))
hchart(AAPL)
highchart(type = "stock") %>%
hc_yAxis_multiples(
create_yaxis(3, height = c(3, 1, 1), turnopposite = TRUE)
) %>%
hc_add_series(SPY, yAxis = 0, name = "SPY", color = hex_to_rgba("red", 0.7)) %>%
hc_add_series(SPY.EMA.20, yAxis = 0, name = "EMA 20") %>%
hc_add_series(SPY.EMA.50, yAxis = 0, name = "EMA 50") %>%
hc_add_series(SPY$SPY.Volume, yAxis = 1, color = "gray", name = "Volume", type = "column", title = 'volume') %>%
hc_subtitle(yAxis = 1, text = "Volume", align = "left", style = list(color = "#2b908f", fontWeight = "bold")) %>%
hc_add_series(SPY.RSI.14, yAxis = 2, name = "Osciallator", color = hex_to_rgba("green", 0.7)) %>%
hc_add_series(SPY.RSI.SellLevel, yAxis = 2, color = hex_to_rgba("red", 0.7), name = "Sell level") %>%
hc_add_series(SPY.RSI.BuyLevel, yAxis = 2, color = hex_to_rgba("blue", 0.7), name = "Buy level")
I'm trying to create a scatter plot in highcharts shiny R but I need to give a different color to points, individually. Consider for instance the following example:
library("MASS")
dscars <- round(mvrnorm(n = 20, mu = c(1, 1), Sigma = matrix(c(1,0,0,1),2)), 2)
highchart() %>%
hc_chart(type = "scatter", zoomType = "xy") %>%
hc_tooltip(
useHTML = TRUE,
pointFormat = paste0("<span style=\"color:{series.color};\">{series.options.icon}</span>",
"{series.name}: <b>[{point.x}, {point.y}]</b><br/>")
) %>%
hc_add_series(data = list.parse2(as.data.frame(dscars)),
marker = list(symbol = fa_icon_mark("car")),
icon = fa_icon("car"), name = "car")
My objective is to give to this 20 points, an unique color.
I tried to set the "fillColor" inside marker list as also as to define the color of the series, both with a vector of 20 colors but I had no success.
Can any one give me a hint?
Thank you
In highcharts (the highcharter) the point can be given as other parameter, same as x and y. So first
library("MASS")
dscars <- round(mvrnorm(n = 20, mu = c(1, 1), Sigma = matrix(c(1,0,0,1),2)), 2)
dscars <- as.data.frame(dscars)
names(dscars) <- c("x", "y") # it's better give a named list IMHO
dscars$color <- colorize(1:nrow(dscars))
colorizeis a function to create a color vector given other vector. In this case the input vector is a sequence (no repeated) so the output will be differents colors. But if you want yo can use your own colors.
highchart() %>%
hc_chart(type = "scatter", zoomType = "xy") %>%
hc_tooltip(
useHTML = TRUE,
pointFormat = paste0("<span style=\"color:{point.color};\">{series.options.icon}</span>",
"{series.name}: <b>[{point.x}, {point.y}]</b><br/>")
) %>%
hc_add_series(data = list_parse(dscars),
marker = list(symbol = fa_icon_mark("car")),
icon = fa_icon("car"), name = "car")
Note we used:
color:{point.color}; in the poinFormat, beacuse every point has its own color in the color accesor.
I used list_parse which parse the data frame in a named list instead of unnamed list so highcharts understand how to use the data. list_parse is the same list.parse3 for old version of highcharts.
Hope it helps.
Is this what you want?
rm(list = ls())
library(highcharter)
library(MASS)
dscars <- data.frame(round(mvrnorm(n = 20, mu = c(1, 1), Sigma = matrix(c(1,0,0,1),2)), 2))
highchart() %>%
hc_chart(type = "scatter", zoomType = "xy") %>%
hc_tooltip(
useHTML = TRUE,
pointFormat = paste0("<span style=\"color:{colorByPoint:true};\">{series.options.icon}</span>",
"{series.name}: <b>[{point.x}, {point.y}]</b><br/>")
) %>%
hc_add_series(data = list.parse2(as.data.frame(dscars)),colorByPoint = TRUE,
marker = list(symbol = fa_icon_mark("car")),
icon = fa_icon("car"), name = "car")
I'm trying to add horizontal and vertical lines in a highchart (rcharts) in a revealjs presentation.
I tried to modify the code of this post in this way:
require(xlsx)
library(rCharts)
Perhplot.df <-read.xlsx("C:\\RDirectory\\AREALAVORO\\JOB\\RISULTATI2.xlsx", sheetName="completo2")
lDf <- split(Perhplot.df, Perhplot.df$variable)
h16 <- hPlot(protection ~ days, data = lDf$Exposure,
type = "bubble",
group = "label",
title = "By Days of Exposure",
subtitle = "Move the mouse pointer on the bubbles to view the data",
size = "cluster_size",
group = "label")
h16$set(width = 1000, height = 600)
ord <- c("Less than 1 week"=0,
"1-2 weeks"=1,
"3-4 weeks"=2,
"More than 4 weeks"=3,
"Mean"=4
)
h16$params$series <- lapply(h16$params$series, function(d){
temp = ord[d$name]
names(temp) = NULL
d$legendIndex = temp
return(d)
})
h16$yAxis(min = 35, max = 70, title = list(text = "Level of Protection"))
h16$xAxis(min = 0, max = 45, title = list(text = "Days of Exposure"))
dfy<-data.frame(y=c(35,58,70), x=c(18.8,18.8,18.8))
h16$layer(y~x,data=dfy,type="line",color=list(const = 'darkblue'))
h16$show('inline', include_assets = TRUE)
the bubble plot is ok but then I try to add the vertical line I have this error:
Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : ‘layer’ is not a valid field or method name for reference class “Highcharts”
So the solution works for Dimple Charts but not for Highcharts...
As same as rcharts highcharts plotLines.
You need to use plotLines argument:
library("rCharts")
# Some data
x <- abs(rnorm(10))
# A graph with column
h <- Highcharts$new()
h$chart(type = "column")
h$series(data = x)
h$xAxis(categories = letters[1:10])
# the horizontal line
h$yAxis(title = list(text = "rnorm()"),
plotLines = list(list(
value = mean(x),
color = '#ff0000',
width = 3,
zIndex = 4,
label = list(text = "mean",
style = list( color = '#ff0000', fontWeight = 'bold' )
))))
h
Yo add vertical you change yAxis by xAxis.
Or if you use highcharter (It's a new wrapper of highcharts for R):
h2 <- highchart() %>%
hc_chart(type = "column") %>%
hc_add_serie(data = x) %>%
hc_xAxis(categories = letters[1:10]) %>%
hc_yAxis(title = list(text = "rnorm()"),
plotLines = list(list(
value = mean(x),
color = '#ff0000',
width = 3,
zIndex = 4,
label = list(text = "mean",
style = list( color = '#ff0000', fontWeight = 'bold' )
))))
h2
Source: http://jkunst.com/highcharter/highcharts-api.html#hc_xaxis-and-hc_yaxis
I am trying to create a time-series plot using the plotting interface of rCharts to the Highcharts library.
I am trying to figure out how I can set the color of an individual point depending on its y-value. I found a way to have different colors for the line and the points, but only as a group, not for the data points individually.
Here's the test code:
library(rCharts)
library(rjson)
TransformDate <- function(x){
as.numeric(as.POSIXct(x, origin="1970-01-01")) * 1000
}
x <- TransformDate(c('2013-01-01 11:05:35', '2013-03-03 04:50:35', '2013-05-05 21:09:37', '2013-07-07 12:49:05'))
y <- c(1,56,123,1000)
w<-TransformDate(c('2013-01-10 11:05:35', '2013-03-13 04:50:35', '2013-05-15 21:09:37', '2013-07-17 12:49:05'))
z<-c(10, 100, 70, 500)
df1 <- data.frame(x = x,y = y)
df2 <- data.frame(x = w, y = z)
combo <- rCharts:::Highcharts$new()
combo$series(list(list(data = rCharts::toJSONArray2(df1, json = F, names = F), name = "Temp1", marker = list(fillColor = c('#999'), lineWidth=6, lineColor=c('#999'))),
list(data = rCharts::toJSONArray2(df2, json = F, names = F), name = "Temp2")))
combo$xAxis(type='datetime')
combo$chart(type = "scatter")
combo$chart(zoomType="x")
combo
I believe that this can be done in Polycharts but the reason why I am using highcharts is that it plots time-series data nicely and it has also cool zoom-in functionality.
Thanks in advance for your help & suggestions.
Jan
Here's one way to control color/size for lines/markers separately:
h <- rCharts:::Highcharts$new()
h$series(list(
list(data = rCharts::toJSONArray2(df1, json = FALSE, names = FALSE),
name = "Big Reds",
color = '#FF0000',
lineWidth = 4,
marker = list(
fillColor = '#FFA500',
radius = 10)
),
list(data = rCharts::toJSONArray2(df2, json = FALSE, names = FALSE),
name = "Small Blues",
color = '#0000FF',
lineWidth = 2,
marker = list(
fillColor = '#ADD8E6',
radius = 6)
)))
h$xAxis(type = 'datetime')
h$chart(type = "scatter")
h$chart(zoomType = "x")
h