Text and Annotations in ggplot2
Adding annotations in ggplot2 with Plotly.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
Default annotate
Set x and y position of the label.
library(plotly)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p <- p + annotate("text", x = 4, y = 25, label = "Some text")
plotly::ggplotly(p)
Usin range
library(plotly)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p <- p + annotate("text", x = 2:5, y = 25, label = "Some text")
plotly::ggplotly(p)
Creating rectangle
library(plotly)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p <-
p + annotate("rect", xmin = 3, xmax = 4.2, ymin = 12, ymax = 21,
alpha = .2)
plotly::ggplotly(p)
Creating segment
library(plotly)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p <-
p + annotate("segment", x = 2.5, xend = 4, y = 15, yend = 25,
colour = "blue")
plotly::ggplotly(p)
Creating pointrange
library(plotly)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p <-
p + annotate("pointrange", x = 3.5, y = 20, ymin = 12, ymax = 28,
colour = "red", size = 1.5)
plotly::ggplotly(p)
Adding multiple labels manually
library(plotly)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p <- p + annotate("text", x = 2:3, y = 20:21, label = c("my label", "label 2"))
plotly::ggplotly(p)
Stlying label text
library(plotly)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p <-
p + annotate("text", x = 4, y = 25, label = "italic(R) ^ 2 == 0.75",
parse = TRUE)
plotly::ggplotly(p)
library(plotly)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p <-
p + annotate("text", x = 4, y = 25,
label = "paste(italic(R) ^ 2, \" = .75\")", parse = TRUE)
plotly::ggplotly(p)
Default raster plot
Set anootation_raster
at specific position.
library(plotly)
rainbow <- matrix(hcl(seq(0, 360, length.out = 50 * 50), 80, 70), nrow = 50)
p <-
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
annotation_raster(rainbow, 15, 20, 3, 4)
plotly::ggplotly(p)
Fill the whole raster plot
library(plotly)
rainbow <- matrix(hcl(seq(0, 360, length.out = 50 * 50), 80, 70), nrow = 50)
p <-
ggplot(mtcars, aes(mpg, wt)) +
annotation_raster(rainbow, -Inf, Inf, -Inf, Inf) +
geom_point()
plotly::ggplotly(p)
Sequential raster
library(plotly)
rainbow2 <- matrix(hcl(seq(0, 360, length.out = 10), 80, 70), nrow = 1)
p <-
ggplot(mtcars, aes(mpg, wt)) +
annotation_raster(rainbow2, -Inf, Inf, -Inf, Inf) +
geom_point()
plotly::ggplotly(p)
Interploate raster colours
library(plotly)
rainbow2 <- matrix(hcl(seq(0, 360, length.out = 10), 80, 70), nrow = 1)
p <-
ggplot(mtcars, aes(mpg, wt)) +
annotation_raster(rainbow2, -Inf, Inf, -Inf, Inf, interpolate = TRUE) +
geom_point()
plotly::ggplotly(p)
What About Dash?
Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash for R at https://dashr.plot.ly/installation.
Everywhere in this page that you see fig
, you can display the same figure in a Dash for R application by passing it to the figure
argument of the Graph
component from the built-in dashCoreComponents
package like this:
library(plotly)
fig <- plot_ly()
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... )
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
app <- Dash$new()
app$layout(
htmlDiv(
list(
dccGraph(figure=fig)
)
)
)
app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)