【问题标题】:How to add scroll bar to ggplot2 bar chart?如何在ggplot2条形图中添加滚动条?
【发布时间】:2020-06-22 06:57:38
【问题描述】:

当情节中有很多条时,x轴过于拥挤,是否可以在情节中添加滚动条?下面的例子很简单,只有 26 个柱,我需要绘制 100 多个柱。谢谢。

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)

【问题讨论】:

  • 这能回答你的问题吗? How to add a horizontal scrollbar to the X axis?
  • 哎呀,刚刚看到另一个问题是关于 python 的。尽管 R 中有一些类似的功能
  • 这个帖子应该有帮助:community.plotly.com/t/…
  • 如果你想坚持使用 ggplot ...旋转标签可能是一个解决方案。像 ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90))
  • @stefan 尝试了 rangeslider(),但不是一个很好的解决方案,起初所有的酒吧仍然挤在一起。

标签: r ggplot2 scrollbar ggplotly


【解决方案1】:

不幸的是,不能在ggplot 绘图中添加滚动条,因为它是用来生成静态图像的。相反,您可以查看how to make interactive plotsplotly

这是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

【讨论】:

  • 感谢解答,不过plotly也有问题,我上面的例子也是用plotly的。
  • 你试过调整width吗?例如。 ggplotly(p, width = 600)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-10
  • 2019-01-17
  • 1970-01-01
相关资源
最近更新 更多