How to add scroll bar to ggplot2 bar chart?

Viewed 2179

When there are many bars in the plot, the x-axis is too crowded, is it possible to add scroll bar to the plot? The example below is simple, only 26 bars, I need to plot more than 100 bars. Thanks.

library(ggplot2)
library(plotly)
tdf <- data.frame(c = letters[1:26], v = 1:26)
p <- ggplot(tdf, aes(x = c, y = v)) +
  geom_bar(stat = "identity", width = 1)
ggplotly(p, width = 200)

enter image description here

1 Answers

Unfortunately, it is not possible to add scrollbars to ggplot plots, as it is meant to produce static images. Instead, you can take a look at at how to make interactive plots with plotly.

Here is an example

# Libraries
library(ggplot2)
library(dplyr)
library(plotly)
library(hrbrthemes)

# Load dataset from github
data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered.csv", header=T)
data$date <- as.Date(data$date)

# Usual area chart
p <- data %>%
  ggplot( aes(x=date, y=value)) +
    geom_area(fill="#69b3a2", alpha=0.5) +
    geom_line(color="#69b3a2") +
    ylab("bitcoin price ($)") +
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
Related