微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R:使用 ggplot 在 x 轴上连续缩放

如何解决R:使用 ggplot 在 x 轴上连续缩放

我正在尝试在 x 轴(从 00 到 23)上绘制每小时值。

但是,当尝试下面的代码时,它最终会导致我的 R Studio 崩溃。我想知道我是否遗漏了某个步骤,或者我是否使用了错误代码来实现这一目标。

> glimpse(df_temp$start.Hour)
 int [1:496728] 18 17 9 8 20 20 0 21 13 16 ...
# Trip distance by time of the day
p5 <- ggplot(df_temp,aes(x=Start.Hour,y=Trip.distance)) +
  geom_col(fill="salmon") +
  labs(title="Trip distance by Time of the Day",x="Hour of the Day",y="distance (km)") +
      theme_bw() +
  theme(plot.title=element_text(size=14,face="bold")) +
  theme(axis.title.x=element_text(size = 12,face = "bold")) +
  theme(axis.text.x=element_text(size=10,face = "bold")) + 
  theme(axis.title.y=element_text(size=12,face = "bold")) +
  theme(axis.text.y=element_text(size=10,face = "bold")) +
  scale_x_continuous(labels = as.character(df_temp$start.Hour),breaks = df_temp$start.Hour)
  
p5

这是我在没有 scale_x_continuous() 的情况下得到的

enter image description here

这就是我想要实现的目标:

enter image description here

注意:两个图中使用了不同的数据集。

解决方法

在 scale_x_continuous 函数中,标签和中断不应引用整个值向量。 相反,您应该尝试:labels = 0:23 和breaks = 0:23

data = {
    'attribute_1': true,'attribute_2': false,'attribute_1_speed': 25.3,'attribute_2_speed': null
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?