Home > OS >  Plotting heatmaps of multiple columns using slider in ggplot R
Plotting heatmaps of multiple columns using slider in ggplot R

Time:06-22

I have the following dataframe

df <- data.frame(
  x = rep(c(2, 5, 7, 9, 12), 4),
  y = rep(c(1, 2), each = 10),
  col1 = rexp(20),
  col2 = rnorm(20), 
  col3 = rexp(20)
)

And this is the plot

ggplot(df, aes(x, y, fill = col2))   geom_tile()

I want to make an interactive chart where I can use a slider change the target column (switching between column 1, 2 and 3).

Thanks in advance.

CodePudding user response:

You can use frame aesthetic in the ggplotly function from plotly to make an interactive slider with your target (I am not sure what your target feature is) like this:

library(plotly)
df <- data.frame(
  x = rep(c(2, 5, 7, 9, 12), 4),
  y = rep(c(1, 2), each = 10),
  col1 = rexp(20),
  col2 = rnorm(20), 
  col3 = rexp(20)
)

df$target <- rep(sample(c(1:3), 2), 10)
plot <- ggplot(df, aes(x, y, fill = col2, frame = target))   geom_tile()
ggplotly(plot)

Output:

enter image description here

  • Related