如果时间数据为字符形式(R),如何在x轴上缩放时间

问题描述 投票:0回答:1
ggplot(R3L12, aes(x=Time, y=HeartRate)) +
  stat_summary(fun.y=mean, geom="point") +
  geom_smooth(aes(x=as.numeric(factor(Time)), y=HeartRate)) +
  labs(title = "Turtle R3L12 Average Heart Rate", ylab = "Heart Rate") +
   theme(axis.text.x = element_text(angle = 90, hjust=1, size = 3, color = "black"), plot.title = element_text(face = "bold", hjust = 0.5, size = 15)) 

这是我必须用平滑线绘制散点图的代码。我的时间间隔是从00:00到23:55:00。我只希望时间每30分钟显示一次,因此x轴不会很拥挤,但是我还不知道该怎么做。我对R非常陌生,因此请保持简单的解释。

时间变量当前是一个像HH:MM:SS这样的字符对象。

r ggplot2 scale
1个回答
0
投票

没有可重现的数据集示例,很难确定该解决方案是否适用于您,但是您可以使用scale_x__datetime设置x轴格式的分隔符(假设x轴有效地位于时间格式):

ggplot(R3L12, aes(x=Time, y=HeartRate)) +
  stat_summary(fun.y=mean, geom="point") +
  geom_smooth(aes(x=as.numeric(factor(Time)), y=HeartRate)) +
  labs(title = "Turtle R3L12 Average Heart Rate", ylab = "Heart Rate") +
  theme(axis.text.x = element_text(angle = 90, hjust=1, size = 3, color = "black"), plot.title = element_text(face = "bold", hjust = 0.5, size = 15)) +
  scale_x_datetime(breaks = date_breaks("30 min"),labels=date_format("%H:%M:%S"))

它回答了您的问题吗?

如果没有,请提供可复制的示例数据集,以便人们进行故障排除。 (请参阅:How to make a great R reproducible example

© www.soinside.com 2019 - 2024. All rights reserved.