如何在ggplot2中保留原始顺序

时间:2018-12-09 22:37:37

标签: r ggplot2

我有以下代码:

df <- data.frame(Days = days,Temperature = temp)
pl <- ggplot(df,aes(x=Days,y=Temperature)) + geom_point()
print(pl)

当我尝试运行此代码时,它以字母顺序而不是索引顺序(星期一,星期二,星期三,星期四,星期五)显示日期。如何将其更改为正确的顺序?

2 个答案:

答案 0 :(得分:3)

稍作搜索后,我通过安装class YourCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource { @IBOutlet weak var collectionView: UICollectionView! override func awakeFromNib() { super.awakeFromNib() registerCell() self.collectionView.delegate = self self.collectionView.dataSource = self } func registerCell() { collectionView.register(TimeCell.self, forCellWithReuseIdentifier: "cell") } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 10 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TimeCell cell.time.text = "1:00" return cell } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } 软件包找到了一个内置的英语-工作日缩写对象(正确顺序)。使用

DescTools

似乎是执行此操作的最原则(我想不出使用其他语言环境的工作日名称缩写的简单方法...)

答案 1 :(得分:2)

以下代码有效:

library(ggplot2)

days <- c("Mon","Tue","Wed","Thu","Fri")
temp <- c(21, 24, 34, 23, 23)

df2 <- data.frame(Days2=factor(days,levels=unique(days)), Temperature2 = temp)

pl2 <- ggplot(df2,aes(x=Days2,y=Temperature2)) + geom_point()
print(pl2)

它产生以下图形:enter image description here