gvisBubbleChart with categories for shiny app - r

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/

Related

Show vertical line on hover in R Shiny dashboard

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")
)

How to Change X-Axis Label in ChromoMap?

I am new to R and I have been using chromoMap library in R to visualize annotation plots and visualizing the feature-associated data. It's a great library and produces great charts. However, I was not able to find any option for changing the x-axis label from length (bp) to anything else. Because of this, I am not able to use any of the produced charts. It sounds like a small issue, but it totally affects the usability of this great package. I followed this tutorial link and produced the below chart. In my chart and all the samples, the x-axis label is fixed and this is my problem and I am looking for a way to just change it.
library(chromoMap)
chr_file_1 = "chr_file_without_centromere.txt"
anno_file_1 = "annotation_pos.txt"
chromoMap(chr_file_1,anno_file_1)
I am wondering if anybody has the same experience ?? This package produces output as a htmlwidget object and therefore I could not change the x-axis lable. Is there any way to modify a htmlwidget object? or Any way to change this bp to something else??

Shiny. Plot axis labels as custom names, not variable names (from data)

I am working in Shiny and making an interactive plot with ggplot2. Using pull down menus on the UI the user can change what variables are plotted on the x and y axes. I would like the axes labels on the plot to represent the labels within the pull down menu, not the variable names from the actual dataset. The labels in the pull down menu are easier to interpret.
I can get this to work on my machine by specifying two lists (one for each axis) in the global workspace prior to running my Shiny app.
In the global workspace for one axis
predChoices <- c("Cabezon Standard Length (mm)" = "predlength",
"Cabezon Mass (g)" = "predmass")
and then in the server.R when I specify the axis label in ggplot
scale_x_continuous(paste("\n",names(predChoices[predChoices == input$predVariable])))
where predVariable is the name of the pull-down widget in the UI.R. This code labels the axes nicely with the much more descriptive labels.
The problem is that using this workaround not all the requirements for the Shiny App are included within the app, making it hard to post to the web or share with others, because of the need for the initial code to setup the naming lists within the global workspace.
Is there a means within Shiny of making the axis labels reactive, but allow them to have custom names rather than the actual variable names (from the datafile), without having to make the specification in the global workspace prior to running the app?
Thanks,
Nate

Can anyone suggest a good world map visualization for use in Shiny?

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
)

accessing shape attribute for points when making NVD3 scatterChart with nplot/rCharts

How do you set the shape attribute for points when building a scatterChart with nplot from rCharts? Point size can be set by providing a column in the input dataframe named "size" but if there's a corresponding "shape" column consisting of strings such as "square" or "cross" the resulting graph still has the default circle points. New to R and NVD3 so I apologize for my lack of vocabulary.
It appears the newest version of nvd3 no longer works the same way as the old version. See for example. The screenshot shows shapes, and the data has shape:, but only circles are rendered in the actual chart. Also, the tests do not produce anything other than circles. I glanced at the source, and I could not find where or how to set shape. If you know how to do with nvd3, I could easily translate into a rCharts example.
I don't have a reputation of 50, but I'd like to comment.
Line 18 in this NVD3 example(Novus.github) shows how it's currently done. Likewise, all you need to do with the live code(nvd3.org) is uncomment the 'size' line in the data tab.
I attempted making a column in my df named 'shape', and using n1 <- nPlot(x~y, data=df, shape='shape', type='scatterChart'); n1$chart(onlyCircles=FALSE); and a number of other combinations. I've only spent the last two days working with rCharts but have made some exciting progress. I'm giving up on this but found it curious that these two examples weren't mentioned here, so I thought I'd mention them.
I know this question is a bit "ancient" but I faced the same problem and it took me a while to find out how to change the shapes.
I followed the approach in this example for changing the size:
nvd3 scatterPlot with rCharts in R: Vary size of points?
Here my solution:
library(rCharts)
df=data.frame(x=rep(0:2,3),y=c(rep(1,3),rep(2,3),rep(3,3)),
group=c(rep("a",3),rep("b",3),rep("c",3)),shape=rep("square",9))
p <- nPlot(y~x , group = 'group',data = df, type = 'scatterChart')
#In order to make it more pleasant to look at
p$chart(xDomain=c(-0.5,2.5))
p$chart(yDomain=c(0,4))
p$chart(sizeRange = c(200,200))
#here the magic
p$chart(scatter.onlyCircles = FALSE)
p$chart(shape = '#! function(d){return d.shape} !#')
p

Resources