2

My data (final.df) looks like the following:

A B C Y 1     
0 0 0 0 0.05 
0 0 1 1 0.03 
....

Based on the comment below, here is a ASCII text representation of the dataframe.

structure(list(A = c(502, 541, 542, 543, 544, 545, 4304, 4370, 
4371, 4372, 4373, 4442), B = c(4.4, 4.2, 4.4, 4.6, 4.8, 5, 5.2, 
4.6, 4.8, 5, 5.2, 5.2), C = c(2.6, 2.8, 2.8, 2.8, 2.8, 2.8, 12.6, 
12.8, 12.8, 12.8, 12.8, 13), Y = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1), `1` = c(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1), `NA` = c(0, 
0, 0, 0, 0, 0, 0, 0, 0.000281600479875937, 0, 0, 0)), .Names = c("A", 
"B", "C", "Y", "1", NA), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L), class = "data.frame")

To summarize, there are four columns that identify each data point. I am interested in creating two boxplots according to their values in column with name 1. I want to compare the values for points labeled 0 in column 'Y' and labeled 1 in column 'Y'. Finally, I want to be able to hover over the points to retrieve the meta-data, meaning the 'A', 'B', 'C', and '1' value.

p <- ggplot(final.df, aes(x = factor(Y), y = 
Y, fill = factor(Y))) 
p <- p + geom_boxplot() + geom_point() + xlab("Y") + guides(fill = 
guide_legend("Y")) + theme(legend.position="top") 
final.p <- ggplotly(p)  

The current plot shows me factor(Y) value and the corresponding value in 1. How can I include the meta-data in columns 'A', 'B', 'C'?

1
  • Using dput(), can you add final.df to your post. Commented Aug 24, 2018 at 17:59

2 Answers 2

4

We can build a text using paste0 and HTML tag <br><\br> and instructe toolttip to use text.

p <- ggplot(df, aes(x = factor(Y), y = Y, 
                    fill = factor(Y), text=paste('</br>A: ',A,'</br>B: ',B, '</br>1: ',1)))

ggplotly(p,tooltip = c("text"))
Sign up to request clarification or add additional context in comments.

Comments

1

Use the tooltip feature of ggplotly. Read about it by typing in help(ggplotly). See Below:

library(tidyverse)
library(plotly)

set.seed(55)
df <- data.frame(
  A = c(rep(0, 8), rep(1, 8)),
  B = rep(c(rep(0, 4), rep(1, 4)), 2),
  C = rep(c(rep(0, 2), rep(1, 2)), 4),
  Y = rep(c(0, 1), 8),
  X1 = runif(16)
)

p <- ggplot(df, aes(x = factor(Y), y = X1, fill = factor(Y), A = A, B = B, C = C))
p <- p + geom_boxplot() +
  geom_point() +
  xlab("Y") +
  guides(fill = guide_legend("Y")) +
  theme(legend.position = "top")
final.p <- ggplotly(p, tooltip = c("A", "B", "C"))
final.p

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.