简体   繁体   中英

Plotting heatmaps of multiple columns using slider in ggplot R

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.

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM