r - 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() 的情况下得到的
注意:两个图中都使用了不同的数据集。
解决方案
在 scale_x_continuous 函数中,标签和中断不应引用整个值向量。相反,您应该尝试:标签 = 0:23 和休息 = 0:23
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 = 0:23, breaks = 0:23) # changes only here !
推荐阅读
- c# - 从字符串中提取多个整数
- javascript - jQuery - 未捕获的类型错误:无法读取未定义的属性“默认值”
- miragejs - 如何使用 MirageJS 中的工厂返回字符串 id 数组?
- scala - 将列动态传递到 Spark SQL 中的其他函数时
- arrays - 查找总和大于给定数字的月份
- ios - 有时 Date() 返回 1 月 1 日
- ffmpeg - rust 中的 FFMPEG 输入选项
- ios - 在 Swift 如何识别音频文件中的多个声音?
- asp.net-core - 通过比较原始图和修改图 EF Core 来更新实体状态的通用解决方案
- r - 安装 R 时出错