I've been trying to generate interactive linked visualizations with by use of the plotly and crosstalk libraries. As shown in the example below, it seems to be possible to pass ggplot2 a 'shared Dataframe'-Object in order to generate linked views with ggplotly(). When i try to run this example i get the following error-message:
Error: ggplot2 doesn't know how to deal with data of class SharedData/R6
I'm using the current versions of ggplot2 (v. 2.2.1) and plotly (4.5.6). What can cause this error?
Thx for help!
The example which i'm trying to replicate can be found here:
https://cpsievert.github.io/plotly_book/linking-animated-views.html
library(gapminder)
library(crosstalk)
library(ggplot2)
library(plotly)
g <- crosstalk::SharedData$new(gapminder, ~continent)
gg <- ggplot(g, aes(gdpPercap, lifeExp, color = continent, frame = year)) +
geom_point(aes(size = pop, ids = country)) +
geom_smooth(se = FALSE, method = "lm") +
scale_x_log10()
ggplotly(gg) %>%
highlight("plotly_hover")
Error: ggplot2 doesn't know how to deal with data of class SharedData/R6
Related
I am trying to hide the plotly toolbar when displaying a reactive ggplot chart within a Shiny app. Currently my code looks like this:
renderPlotly( ggplot(users_activated(), aes(x = month, y = n)) +
geom_bar(stat = "identity"))
I have tried config(displayModeBar = F) to no avail.
You need to convert your ggplot object to a plotly object before you can render it as one, or use plotly functions on it. ggplotly does that. Below, is an example using mpg dataset.
ggplotly(ggplot(mpg, aes(class, displ, color = manufacturer)) +
geom_point()
) %>%
config(displayModeBar = F)
I am trying to plot several wordclouds in a scatterplot and I wonder if one can control the position of a wordcloud in ggplot?
As an example the code below overlays both wordclouds around the origin of the plot.
Say I want to place the second wordcloud at x=4 and y =35. Is that possible?
library(ggplot2)
library(ggwordcloud)
ggplot() +
geom_point(mtcars,mapping=aes(wt,mpg)) +
geom_text_wordcloud(love_words_small,mapping=aes(label=word)) +
geom_text_wordcloud(mtcars,mapping=aes(label=rownames(mtcars))) +
theme_minimal()
I was looking for the exact same thing. Looks like you can simply add the x and y aesthetic arguments. Ie.
ggplot() +
geom_point(mtcars,mapping=aes(wt,mpg)) +
geom_text_wordcloud(love_words_small,mapping=aes(label=word)) +
geom_text_wordcloud(mtcars,mapping=aes(label=rownames(mtcars), x=4,y=35)) +
theme_minimal()
What I did may be more generally helpful for folks, which is to pass x and y vectors:
library(tidyverse)
library(ggwordcloud)
ggplot(data = mtcars %>% mutate(car_names = rownames(mtcars)) %>%
group_by(cyl),
mapping = aes(label=car_names, x=mpg, y=disp)) +
geom_text_wordcloud()
Perhaps you could save the wordclouds as separate plots, and then add them to one plot with cowplot or gridExtra or any of the packages that lets you combine plots?
Assuming generating a scatter plot with ggplot which has the gradient color and shape based on the variables in mtcars built-in database in R .
using the following script adopted from Nathan Day in my previous question:
library(ggplot2)
plot<- ggplot(mtcars, aes(mpg, hp, fill = cyl, size = cyl)) +
geom_point(shape = 21, stroke = 2) +
scale_fill_gradientn(colours = rainbow(7)) +
scale_size(range = c(2,6))
we would get the following plot
Now, when I am trying to convert it to ploty using the following script :
library(plotly)
ggploty (plot)
I would get the following plot which is basically change the color and I loose my gradient colour featur in my ggplot as can be seen in previous plot generated by ggplot.
Of course I would get the interactive image BUT I was not able to upload in this page so I just take a static image of the generated ploty image.
Can you please help me to know how I can keep the original format of ggplot when converting to ploty by ggploty command?
I have a ggplot2 graph that is then driving a plotly chart. I am using the stock diamonds data set and am getting some wonkyness in the legends. I want the legend to ONLY show colorizing for the colors. It appears to be trying to take each clarity from each facet and merge into the legend.
How can I get the legend to only show the colors of the the clarities once? Note: I tried removing size already :)
Below is the code and image of what is produced.
d = diamonds %>% sample_frac(.01)
p = d %>%
ggplot(aes(x = carat, y = price)) +
geom_point(aes(size = depth, color = clarity)) +
facet_wrap(~cut)
ggplotly(p)
This ended up being a bug in the plotly rendering of the graphic, which has been resolved in the latest version of plotly. The solution is to terminate your R session, reinstall your plotly packages and then pull in the updated library.
Is there a way to code the hovermode when using plotly with R and ggplot2?
Currently, my code is:
plot <- ggplot(data, aes(var1, var2, text=var3)) +
geom_point()
py$ggplotly(plot)
And I want the plotly graph to automatically have the hover mode set to "show closest data on hover" rather than "compare data on hover".
The answer from 'mkcor' didn't work when trying to do the same in Shiny. I kept getting an 'unused argument' error. For anyone else with the same problem, this worked for me...
Suppose this is my basic plot:
p <- ggplot(myDf, aes(x=x, y=y )) + geom_point(size = 3, shape = 0)
You can convert the ggplot object to a plotly object:
ggObj <- plotly(p)
Then you can change the hovermode like this:
layout(ggObj, hovermode = 'closest')
Add the following argument when calling ggplotly:
py$ggplotly(plot, kwargs=list(layout=list(hovermode="closest")))