【问题标题】:Changing the axis on ggplot geom_bar [duplicate]更改ggplot geom_bar上的轴[重复]
【发布时间】:2018-01-15 20:44:43
【问题描述】:

我有以下数据框

View(d.df)
Statements | Score | Time
-----------+-------+------
Need Ex    |4.159  | T1
Need Ex    |3.714  | T2
Interest   |2.937  | T1
Interest   |3.508  | T2

和我的阴谋

ggplot(data=d.df, aes(x=`Time`,y=Score, fill=`Time`)) +
  geom_bar(stat="identity") +
  scale_fill_manual(values=c("red","blue"))+
  facet_wrap(~Statements)

生成正确的图形,但 Y 轴刻度不从零开始。我希望 Y 轴从零变为 4.5。 我尝试添加

+ scale_y_continuous(limits=c(0.0,4.5))

但我收到一条错误消息:

Error: Discrete value supplied to continuous scale

如果我尝试

+ coord_cartesian(ylim=c(0,4.5))

坐标几乎保持不变(在底部添加了一些填充)。 关于如何让我的轴与这些数据成比例地从 0 到 4.5 的任何想法?

【问题讨论】:

    标签: r ggplot2 coordinates


    【解决方案1】:

    我无法重现您的示例,因为 Y 在这里从 0 开始。

    我最好的猜测是您的分数列不是数字而是一个因素(这就是错误状态)。

    在绘图之前尝试使用d.df %>% mutate(Score = as.numeric(Score)) 将此列更改为数字。

    【讨论】:

      猜你喜欢
      • 2018-04-09
      • 2021-02-08
      • 2016-12-02
      • 2019-10-22
      • 1970-01-01
      • 2021-12-23
      • 2023-01-19
      • 2020-08-27
      • 2014-09-24
      相关资源
      最近更新 更多