You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让R的DataFrame保持输入顺序,在ggplot2绘图时不打乱X轴?

问题:ggplot2绘图时X轴日期顺序被打乱,如何保持输入顺序?

我创建了两个基础DataFrame:

CollaroyPrices<-data.frame(years=c("Jan-2023", "Feb-2023", "Mar-2023", "Apr-2023", "May-2023", "Jun-2023", "Jul-2023", 
                                   "Aug-2023", "Sep-2023", "Oct-2023", "Nov-2023", "Dec-2023"),
                          medianprice=c(2700000, 2740000, 2920000, 2990000, 3100000, 3210000, 3220000, 3180000, 3200000, 3200000, 3130000, 3120000))
                             

NSWPrices<-data.frame(years=c("Jan-2023", "Feb-2023", "Mar-2023", "Apr-2023", "May-2023", "Jun-2023", "Jul-2023", 
                              "Aug-2023", "Sep-2023", "Oct-2023", "Nov-2023", "Dec-2023"),
                      medianprice=c(949000, 955000, 958000, 969000, 980000, 993000, 1000000, 1000100, 1000200, 1000200, 1000300, 1000300))

运行后DataFrame的顺序保持正确(从1月到12月):

> CollaroyPrices
      years medianprice
1  Jan-2023     2700000
2  Feb-2023     2740000
3  Mar-2023     2920000
4  Apr-2023     2990000
5  May-2023     3100000
6  Jun-2023     3210000
7  Jul-2023     3220000
8  Aug-2023     3180000
9  Sep-2023     3200000
10 Oct-2023     3200000
11 Nov-2023     3130000
12 Dec-2023     3120000

但使用ggplot2绘制基础折线图时,X轴顺序被打乱:

ggplot()+
  geom_line(data=CollaroyPrices, aes(x=years, y=medianprice, color="CollaroyPrices", group=1), size=1) +
  geom_line(data=NSWPrices, aes(x=years, y=medianprice, color="NSWPrices", group=1), size=1) 

解决方案

方法1:将years转为有序因子

ggplot会对字符型变量按字母顺序排序,导致X轴顺序混乱。将years转为有序因子,强制指定排序规则即可:

# 为CollaroyPrices设置有序因子
CollaroyPrices$years <- factor(CollaroyPrices$years,
                               levels = c("Jan-2023", "Feb-2023", "Mar-2023", "Apr-2023", "May-2023", "Jun-2023",
                                          "Jul-2023", "Aug-2023", "Sep-2023", "Oct-2023", "Nov-2023", "Dec-2023"),
                               ordered = TRUE)

# 为NSWPrices设置相同的有序因子
NSWPrices$years <- factor(NSWPrices$years,
                          levels = c("Jan-2023", "Feb-2023", "Mar-2023", "Apr-2023", "May-2023", "Jun-2023",
                                     "Jul-2023", "Aug-2023", "Sep-2023", "Oct-2023", "Nov-2023", "Dec-2023"),
                          ordered = TRUE)

# 重新绘图,X轴会按指定顺序显示
ggplot()+
  geom_line(data=CollaroyPrices, aes(x=years, y=medianprice, color="CollaroyPrices", group=1), size=1) +
  geom_line(data=NSWPrices, aes(x=years, y=medianprice, color="NSWPrices", group=1), size=1)

方法2:将years转为日期类型(推荐)

如果后续需要进行时间序列分析,将years转为日期类型更规范,ggplot会自动按时间顺序排序:

# 转换为日期格式(添加"-01"补全日,匹配格式"%b-%Y-%d")
CollaroyPrices$years <- as.Date(paste0(CollaroyPrices$years, "-01"), format = "%b-%Y-%d")
NSWPrices$years <- as.Date(paste0(NSWPrices$years, "-01"), format = "%b-%Y-%d")

# 绘图时可自定义X轴标签格式
ggplot()+
  geom_line(data=CollaroyPrices, aes(x=years, y=medianprice, color="CollaroyPrices", group=1), size=1) +
  geom_line(data=NSWPrices, aes(x=years, y=medianprice, color="NSWPrices", group=1), size=1) +
  scale_x_date(date_labels = "%b-%Y")  # 设置标签显示为"Jan-2023"格式

内容的提问来源于stack exchange,提问作者PhobixPhobix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 02:45:25