3

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

tjebo
  • 21,977
  • 7
  • 58
  • 94
IanJay
  • 373
  • 4
  • 14
  • Does this answer your question? [How to add a horizontal scrollbar to the X axis?](https://stackoverflow.com/questions/56108996/how-to-add-a-horizontal-scrollbar-to-the-x-axis) – tjebo Jun 22 '20 at 07:26
  • oops, just saw this other question is with python. there is liekly some similar function in R though – tjebo Jun 22 '20 at 07:29
  • This thread should help: https://community.plotly.com/t/how-to-add-horizontal-scroll-bar-for-a-visualization/12499 – tjebo Jun 22 '20 at 07:31
  • if you want to stick to ggplot ... rotating the labels could be a solution. Something like ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90)) – sambold Jun 22 '20 at 08:12
  • Try `ggplotly(p) %>% rangeslider()`. – stefan Jun 22 '20 at 08:24
  • 1
    @stefan tried rangeslider(), but not a very good solution though, at first all of the bars are still crowded together. – IanJay Jun 22 '20 at 23:02

1 Answers1

1

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
mhovd
  • 3,724
  • 2
  • 21
  • 47