Adding custom tooltip to plotly using R and ggplot2 syntax
I've been looking at the same problem and I think what you need to do is something like this (via https://stackoverflow.com/a/27007513/829256 and h/t to @plotlygraphs on Twitter)
# first use your Plotly connection and retrieve data for the ggiris plot you uploaded
irisplot <- py$get_figure('username', n) # where n = the number of this plot on your account
# inspect the irisplot object
str(irisplot) # a list of 2
# inspect irisplot$data
str(irisplot$data) # a list of 3, one list for each Species
# overwrite 'text' for each Species list
irisplot$data[[1]]$text <- labels[1:50]
irisplot$data[[2]]$text <- labels[51:100]
irisplot$data[[3]]$text <- labels[101:150]
# re-upload to Plotly
resp <- py$plotly(irisplot$data, kwargs = list(layout = irisplot$layout))
# check out your new plot
resp$url
So the plot should now have a value from 'labels' for each data point, displayed as a tooltip with mouseover.
You will presumably want to do something smarter in how you assign the labels to the points, but hopefully this gets you started.
And thanks, I think working through this question will help me solve my own task too :-)
ggplotly - R, labeling trace names
You can also edit any of the plotly figure properties after the ggplot2 conversion but before you send it to plotly. Here is an example that changes the legend entry names manually. I'll repeat it here:
df <- data.frame(x=c(1, 2, 3, 4), y=c(1, 5, 3, 5), group=c('A', 'A', 'B', 'B'))
g <- ggplot(data=df, aes(x=x, y=y, colour=group)) + geom_point()
# an intermediate step that `ggplotly` calls
p <- plotly_build(g)
# manually change the legend entry names, which are "trace0", "trace1" in your case
p$data[[1]]$name <- 'Group A'
p$data[[2]]$name <- 'Group B'
# send this up to your plotly account
p$filename <- 'ggplot2-user-guide/custom-ggplot2'
plotly_POST(p)
The extended example here explains in more detail how and why this works.
Note that in general the legend item names, e.g. "trace0", are going to be the labels that you grouped by in the dataframe (as in ggplot2).
ggplot2 to plotly- Only shows traces and not data on the tooltip
So below is the regular scatter plot example.
library(ggplot2)
library(plotly)
lol <- ggplot(diamonds,aes(x=carat,y = price)) + geom_point()
ggplotly(lol)
This is how you can customize the tooltip.Remember also to add ,tooltip=c("text") like below.
lol2 <- ggplot(diamonds,aes(x=carat,y = price)) + geom_point(aes(text=paste('<br>cut:',cut)))
ggplotly(lol2,tooltip = c("text"))
Label every nth datapoint in ggplot
You can pass the data to geom_label
and filter for row numbers that match your condition.
Assuming that by "every seventh" you mean 7, 14, 21... use %% 7 == 0
. Otherwise use %% 7 == 1
for 8, 15, 22...
library(dplyr)
library(ggplot2)
mydata <- data.frame(x = 2 * (1:49),
y = 3 * (1:49))
mydata %>%
ggplot(aes(x, y)) +
geom_point() +
geom_label(aes(label = x),
data = . %>%
filter(row_number() %% 7 == 0 | row_number() == 1))
Result:
Using 2+ legends from R to plotly / plot.ly
You ran into a bug, which we now fixed it.
Please reinstall and reload the "plotly" package, re-instantiate your py
object and it should work now: https://plot.ly/~marianne2/38/or-illn-vs-or-edu/
Plotly edit name tag on hover bar chart
Via empet from community plotly -> Update each trace with hoverlabel
hoverlabel=dict(bgcolor='set_your_preferred_background_color',
font=dict(family='Balto', #or other font family
size=11, #or other size
color='font_color')
)
See also: https://plot.ly/python/reference/#bar-hoverlabel .
Related Topics
Removing/Replacing Brackets from R String Using Gsub
Spread with Duplicate Identifiers for Rows
How to Set The Maximum Recursion Depth In
Use Different Font Sizes for Different Portions of Text in Ggplot2 Title
Change Position of Tick Marks of a Single Graph, Using Ggplot2
Shiny Datatable in Landscape Orientation
Using Mutate Rowwise Over a Subset of Columns
How to Do Histograms of This Row-Column Table in R Ggplot
Generating Dropdown Menu for Plotly Charts
How to Specify Certificate, Key and Root Certificate with Httr for Certificate Based Authentication
R - Error When Using Geturl from Curl After Site Was Changed
R Leaflet Offline Tiles Within Shiny
Pipe in Magrittr Package Is Not Working for Function Load()
An Error in R: When I Try to Apply Outer Function:
Joining Two Data.Tables in R Based on Multiple Keys and Duplicate Entries
How to Remove Trailing Zeros in R Dataframe
Small Ggplot Object (1 Mb) Turns into 7 Gigabyte .Rdata Object When Saved