简体   繁体   中英

How to add scroll bar to ggplot2 bar chart?

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)

在此处输入图像描述

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

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