I'm using Plotly in R to make a scatter plot but currently when I hover over the point in my shiny app, the values corresponding to the x,y values appear. ex (19, 27.73k). However, I would like to change it to display a column corresponding to that data as well. ex (USA, 19, 27.73k) Would this be possible? IDK if relevant but current marker code
mode="markers",
marker = list(
opacity = 0.5,
size = 5
)) %>%
What you're looking for is hovertemplate, possibly in tandem with text. Without data it's hard to generate exactly what you need, but you should be able to figure it out at https://plotly.com/r/hover-text-and-formatting/
Related
I would like to make a Shiny dashboard with e.g. a simple line chart like the one in the top left panel on this website:
https://inflation.ihs.ac.at
I know how to make the plot with ggplot2 and how to make a Shiny dashboard to display the graph (and different versions of it when inputs in the ui are changed).
However, I do not know how to include the vertical line that follows your cursor and how to display labels and values when you hover over certain values.
My question: Is it possible to make a dashboard like this without having knowledge of CSS/Java/some other programming language than R? I looked at the dygraph package which helped me program something similar to the dashboard on the website above but I am not sure if it includes the possibility to make specific adjustments to the hover options provided.
Ideally I would like to be able to specify the detailed hover options (length, width and color of the line and the labels/values, etc.) and still be able to make the original plot with ggplot2 which is obviously not the case if I plot my data using the dygraph package. Is that an option?
Not exactly what you want but close, with rAmCharts4:
library(rAmCharts4)
dat <- data.frame(
x = 1:10,
y = rnorm(10)
)
amLineChart(
data = dat,
xValue = "x",
yValues = "y",
alwaysShowBullets = TRUE,
cursor = list(axes = "x")
)
I am using plotly to zoom my plots, save to svg and show most interesting intervals to customers. The thing I can't reach - I need to save picture with hover showing values.
For example, I zoomed, I pointed value I need to show, I see values using hover.
But when I press "Download plot" button - It's saved without hover information. Here is what I see in svg:
How can I show values on saved plots?
Here is my example code:
library(plotly)
x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)
fig <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')
fig
I really don't know any thing about the software/code you are using but as a general logic you have some different approaches to solve this.
1-create a function to take a screenshot of the area you have on screen with those hovering elements and save it as a bitmap image. This is valid just if you don't require to save/download your plot as an svg file.
2- In case you need to export as svg then you have to create a function to observe and get the svg code which is displaying your plot at the moment of hovering, then save the whole stuff as svg file.
3-If what you require is to save the plot as a svg file then it is also possible that the hovering effect along with the code to produce the highlighted content, to be exported inside the svg. The only means most users have t visualize an svg file is via web browser, so when the parts conteining hovering are hovered they will show the effect and the information.
As I said, these are just general logic to try to find out a solution.
Using rpivottable function in my code. rpivottable provides us readymade graph options which we can choose. eg 1. Heat map 2. Bar chart 3. Tree map 4. Horizontal stacked bar chart etc.
For example, when "Horizontal Bar Chart" is selected, whenever I hover over each bar it is showing appropriate value.
However, I want the value displayed on hover to be statically shown on the chart. So without hovering over, the viewer can see the value for each bar.
Is this possible?
(NOTE: This post has been edited so that the request is more clear)
You asked this a long time ago, but I've just now come across your question. I can help with this! Other than the treemap, all of the other charts are C3 charts.
In this example, I used the data mtcars. I've included more than the static labels here; you may find use for some of these other methods to customize your table.
rp <- rpivotTable(mtcars, rows = "mpg", cols=c("cyl"), width = "80%", height = "40%",
rendererOptions = list(
c3 = list(legend = list(show = FALSE), # hide legend
data = list(labels = TRUE), # label the data
size = list(width = "600", # control the size
height = "500"))))
If you are using the cran package, you'll have to run this line of code, as well.
rp$x$params$rendererOptions <- rp$x$params$rendererOptions[[0]]
I've forked their repo; it doesn't look like they are going to be updating that package anymore. I've fixed this problem in my fork. If you wanted to you could always install my repo instead: devtools::install_github("fraupflaume/rpivotTable")
I'm looking for a versatile bubble chart.
In my shiny app it has to be able to adjust to two things:
It must be clickable. I need to read the selected bubble from the chart and use the selected input for other visualisations inside the app.
I have a categorial x-axis.
I was looking into gvisBubbleChart which seemed to fit very well, but it only responds to a numerical x-axis.
For example:
data <- data.frame(id = c(1,2,3,4,5),xvar = c("a","b","c","d","e"),yvar = c(7,2,5,8,1),sizevar = c(3,66,7,8,5),colorvar = rep(0,5))
plot(gvisBubbleChart(data,idvar = "id", xvar = "xvar", yvar = "yvar", sizevar = "sizevar",colorvar="colorvar"))
Changes the xaxis to 1.5:6.0 instead of the string.
Is there a way to work around this? It seems that the input has to be numerical, I could not find a native option to turn this off.
I'm also very open to any other suggested packages which serve my needs, for ggplot I'm not sure if I can make the clickable part work?
So I checked various sources, editing the googlevisBubble chart is possible with a lot of effort, too much for my case as this would be a very hard workaround.
Google Charts Bubble Charts categorical x and y axes instead of numeric
What I'm going to do is use plotly with ggplot for the dynamic input in my shiny app, the hole package looks very promising.
https://plot.ly/r/shinyapp-plotly-events/
Sorry in advance for the wall of text. I am creating a sort of novel type of choropleth map, in which countries are shaded based on different categorical variables. The way I've set up the app, I assign each country an RGB value based on its levels of each of the underlying variables and I want the map to show that RGB value--seems simple, right?
Unfortunately, most of the map visualizations seem to want to do the color selection for me, rather than letting me choose. The best I've been able to do is to treat the data as categorical and I end up with the same number of categories as countries. This worked fairly well for rworldmap. The problem is, I'm developing this for web use, and I'd really like to have tooltips so that you can hover over a particular country and this doesn't work with rworldmap, as it's just a basic plot. Also, the rworldmap output is not particularly nice looking.
Here's the code I used with that:
mapjoin <- joinCountryData2Map(db, joinCode="ISO3",
nameJoinColumn="iso", mapResolution="high")
mapCountryData(mapjoin, nameColumnToPlot="iso", addLegend=FALSE,
catMethod="categorical", colourPalette=db$rgb, mapTitle=input$year)
I have experimented with googleVis, but I was having a lot of trouble with that--the map would just disappear for no reason and I'd have to reload the page, which I believe was an issue with the Shiny bindings in the googleVis package. I ultimately went with googleCharts (https://github.com/jcheng5/googleCharts), which clears up the problems with the bindings.
However, I'm still having problems.
Here's the reactive function:
output$mapviz <- reactive({
db <- genRgb()
list(
data=googleDataTable(db[c("country", "id")]),
options=list(legend="none", projection="kavrayskiy-vii", colors=db$rgb)
)
)}
and here's the output call:
googleGeoChart("mapviz", width="100%", height="780px")
As you can see, there's not a specific way to clue the JS app that it's categorical data, so as a result, it's making a choropleth with 182 different gradient stop points. Usually this works fine, but occasionally something weird happens and a country mysteriously ends up in an intermediate place between colors. I can always tell that there's a problem because certain countries are supposed to be specific colors (for instance, the U.S. will show as #0000FF, and it's pretty obvious when it's not). I've found that I can go to a different chart type (the app uses other googleCharts types) and then return to the map and usually it's fixed. So it's completely inconsistent.
So with that in mind, can anyone suggest a better mapping tool that I can implement in Shiny that would work well for this purpose?
Thanks!
Check out leaflet:
https://rstudio.github.io/leaflet/
It will allow you to:
have pop-ups for each shapefile with more data
explicitly set the colour of shapefiles in R, based on data in your dbf file.
use an open map background
Some example code (not all may be relevant):
map <- leaflet()%>%
addTiles(urlTemplate = url, attribution = HTML(attrib))%>%
addPolygons(data = sub_shape,
fill = TRUE,
fillColor = colors$color, #set color here
fillOpacity = .8,
stroke = TRUE,
weight = 3,
color = "white",
dashArray = c(5,5),
popup = pops
)