library(DT)
## Warning: package 'DT' was built under R version 4.1.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.3
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
data <- data.frame(
  ID = 1:5,
  Name = c("Alice", "Bob", "Charlie", "David", "Eva"),
  Age = c(25, 30, 35, 40, 45)
)
#make variable as factor to allow drop down selection from the vriable column 
data$Name<-as.factor(data$Name)

labels <- c(ID = "Identifier", Name = "Full Name", Age = "Age in Years")
names(data) <- sapply(names(data), function(x) {
  htmltools::HTML(paste0('<span title="', labels[x], '">', x, '</span>'))
})
DT::datatable(data,  escape = FALSE,filter = 'top')
library(ggplot2)

# Sample data for the line plot
df <- data.frame(x = 1:10, y = rnorm(10))

#to have customized label text on the plot when hovering on point
df$labeltext<-paste0("x: ", df$x, " y: ", round(df$y,3))
  
# Create the line plot and display
p<-ggplot(df, aes(x, y)) +
     geom_line() +
  #need to have option text here, ignore the warning which is negligible
  geom_point(aes(text=labeltext))+
     geom_text(aes(x = mean(range(x)), y = mean(range(y))), 
               label = "D R A F T", 
               angle = 45, 
               alpha = 0.1, 
               color = "grey", 
               size = 30,
               vjust = 0.5, 
               hjust = 0.5)
## Warning in geom_point(aes(text = labeltext)): Ignoring unknown aesthetics: text
p

ggplotly(p, tooltip="labeltext")
p <- ggplot(df, aes(x, y)) +
     geom_line() 


ggplotly(p)%>%add_annotations(  x = 5,  # Starting x position of the arrow
    y = -1,  # Starting y position of the arrow
    ax = 1,  # Ending x position of the arrow
    ay = 2,  # Ending y position of the arrow
    xref = "x",
    yref = "y",
    axref = "x",
    ayref = "y",
    text="draft",
    showarrow = TRUE,
    arrowhead = 2,  # Arrow style, can be adjusted
    arrowsize = 1,  # Arrow size, can be adjusted
    arrowwidth = 2   # Arrow width, can be adjusted
  )
# Print the plot
ggplotly(p)%>%add_annotations(
        text = "D R A F T",
        x = 0.5, y = 0.5,
        xref = "paper", yref = "paper",
        showarrow = FALSE,
        font = list(color = "grey", size =100),
        opacity = 0.1,
        textfont = list(angle = 45, textangle = 45, orientation = 45, rotate = 45)
    )
library(ggplot2)
library(plotly)

# Sample data for the line plot
df <- data.frame(x = 1:10, y = rnorm(10))

# Create the line plot using ggplot2
p <- ggplot(df, aes(x, y)) +
     geom_line()

# Convert to plotly object
plotly_obj <- ggplotly(p)

# Function to add diagonal watermarks
add_diagonal_watermarks <- function(plotly_obj, text, n = 5, color = "grey", opacity = 0.5, size = 10) {
    for (i in 1:n) {
        plotly_obj <- plotly_obj %>%
            add_annotations(
                text = text,
                x = i/n, y = i/n,
                xref = "paper", yref = "paper",
                axref = "pixel", ayref = "pixel",
                ax = 0, ay = 0,
                showarrow = FALSE,
                font = list(color = color, size = size),
                opacity = opacity
            )
    }
    return(plotly_obj)
}

# Add watermark
plotly_obj <- add_diagonal_watermarks(plotly_obj, "Watermark", n = 10)

# Print the plotly object
plotly_obj
library(ggplot2)
library(plotly)

# Sample data for the line plot
df <- data.frame(x = 1:11, y = rnorm(11))

# Create the line plot using ggplot2
p <- ggplot(df, aes(x, y)) +
     geom_line()

# Convert to plotly object
plotly_obj <- ggplotly(p)

# Function to add diagonal watermarks
add_diagonal_watermarks <- function(plotly_obj, letters, n = 5, color = "grey", opacity = 0.1, size =60) {
    if(length(letters) != n) {
        stop("The number of letters must be equal to n")
    }

    for (i in 1:n) {
        plotly_obj <- plotly_obj %>%
            add_annotations(
                text = letters[i],
                x = i/n, y = i/n,
                xref = "paper", yref = "paper",
                axref = "pixel", ayref = "pixel",
                ax = 0, ay = 0,
                showarrow = FALSE,
                font = list(color = color, size = size),
                opacity = opacity
            )
    }
    return(plotly_obj)
}

# Add watermark
plotly_obj <- add_diagonal_watermarks(plotly_obj, letters = c("D", "R", "A", "F", "T"), n = 5)

# Print the plotly object
plotly_obj
library(ggplot2)
library(plotly)

# Sample data for the line plot
df <- data.frame(x = 1:10, y = rnorm(10))

# Create the line plot using ggplot2
p <- ggplot(df, aes(x, y)) +
     geom_line()

# Convert to plotly object
plotly_obj <- ggplotly(p)

# Function to add diagonal watermarks
add_diagonal_watermarks <- function(plotly_obj, letters, n = 5, color = "grey", opacity = 0.2, size = 60) {
    if(length(letters) != n) {
        stop("The number of letters must be equal to n")
    }

    for (i in 1:n) {
        # Adjusting the x and y coordinates for each letter
        x_position <- (i - 1) / (n - 1)
        y_position <- (i - 1) / (n - 1)

        plotly_obj <- plotly_obj %>%
            add_annotations(
                text = letters[i],
                x = x_position, y = y_position,
                xref = "paper", yref = "paper",
                axref = "pixel", ayref = "pixel",
                ax = 0, ay = 0,
                showarrow = FALSE,
                font = list(color = color, size = size),
                opacity = opacity
            )
    }
    return(plotly_obj)
}

# Add watermark
plotly_obj <- add_diagonal_watermarks(plotly_obj, letters = c("D", "R", "A", "F", "T"), n = 5)

# Print the plotly object
plotly_obj
library(ggplot2)
library(plotly)

# Sample data for the line plot
df <- data.frame(x = 1:10, y = rnorm(10))

# Create the line plot using ggplot2
p <- ggplot(df, aes(x, y)) +
     geom_line()

# Convert to plotly object
plotly_obj <- ggplotly(p)

# Function to add diagonal watermarks
add_diagonal_watermarks <- function(plotly_obj, letters, n = 5, color = "grey", opacity = 0.2, size = 60) {
    if(length(letters) != n) {
        stop("The number of letters must be equal to n")
    }

    # Calculate positions for each letter
    positions <- seq(0, 1, length.out = n)

    for (i in 1:n) {
        plotly_obj <- plotly_obj %>%
            add_annotations(
                text = letters[i],
                x = positions[i], y = positions[i],
                xref = "paper", yref = "paper",
                axref = "pixel", ayref = "pixel",
                ax = 0, ay = 0,
                showarrow = FALSE,
                font = list(color = color, size = size),
                opacity = opacity
            )
    }
    return(plotly_obj)
}

# Add watermark
plotly_obj <- add_diagonal_watermarks(plotly_obj, letters = c("D", "R", "A", "F", "T"), n = 5)

# Print the plotly object
plotly_obj