在X轴是日期的因素的命令或对X轴上排序?

发布于 2025-02-13 20:10:07 字数 1622 浏览 2 评论 0 原文

在这个问题中我试图找到解决方案X轴的等距是由于YM数据中几个月不存在的X轴。那里的一个人建议我使用因子()。享受我在第一篇文章中上传的第一个情节。我为该图中的scale_x-tate介绍了,但这对因子()技巧都不起作用。

 dt<-data.frame(M_Datum=c("2018-02-05","2018-02-15","2018-02-10","2018-02-13","2017-02-05","2017-02-15","2017-02-10","2017-02-23","2020-02-25","2012-02-15","2020-02-10","2020-02-13"),Yield=c(4,47,18,10,22,50,70,120,150,400,60,78))
        
        dt[,1]<-as.Date(format(as.Date(dt[,1]), "%Y-%m-01"))
       
        dr<-data.frame("M_Datum"=dt$M_Datum,"Yield"=dt$Yield)
      
        
        
        
        mydf=aggregate(Yield ~ M_Datum, dr, length) 
        
        
        koka<-data.frame("M_Datum"=mydf$M_Datum,"Yiel"=mydf$Yield)
        
        
        
        ggplot(koka, aes(x=factor(format(M_Datum, "%b %Y")), y=Yiel,group = 1)) + 
          geom_point(size=7,colour="#EF783D",shape=17) +
          geom_line(color="#EF783D")+
          
          theme(axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1))+
          theme(axis.text.y.left = element_text(color = "#EF783D"),
                axis.title.y.left = element_text(color = "#EF783D"))+
          ylab("Wafer Quantity")+
          xlab("")
        
        
        
        
        
        
        
        
        
      
      

in this question scale_x_date and how to make it equidistant? i tried to find a solution for the problem of the equidistance in the x-axis caused by the non existence of some months in ym data .a guy there suggested to me use factor() .if fact that wa s ok except that i can not anymore order my x-axis chronologically as i was enjoying in my first plot which i uploaded in my first post. iused scale_x-date in for that plot but this does not work for the factor() trick.

enter image description here

 dt<-data.frame(M_Datum=c("2018-02-05","2018-02-15","2018-02-10","2018-02-13","2017-02-05","2017-02-15","2017-02-10","2017-02-23","2020-02-25","2012-02-15","2020-02-10","2020-02-13"),Yield=c(4,47,18,10,22,50,70,120,150,400,60,78))
        
        dt[,1]<-as.Date(format(as.Date(dt[,1]), "%Y-%m-01"))
       
        dr<-data.frame("M_Datum"=dt$M_Datum,"Yield"=dt$Yield)
      
        
        
        
        mydf=aggregate(Yield ~ M_Datum, dr, length) 
        
        
        koka<-data.frame("M_Datum"=mydf$M_Datum,"Yiel"=mydf$Yield)
        
        
        
        ggplot(koka, aes(x=factor(format(M_Datum, "%b %Y")), y=Yiel,group = 1)) + 
          geom_point(size=7,colour="#EF783D",shape=17) +
          geom_line(color="#EF783D")+
          
          theme(axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1))+
          theme(axis.text.y.left = element_text(color = "#EF783D"),
                axis.title.y.left = element_text(color = "#EF783D"))+
          ylab("Wafer Quantity")+
          xlab("")
        
        
        
        
        
        
        
        
        
      
      

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ低調成傷 2025-02-20 20:10:07

您将需要按正确的顺序将因子的水平放置。您可以对此

ggplot(koka, aes(
  x=factor(format(M_Datum, "%b %Y"), levels=format(sort(unique(M_Datum)),"%b %Y")), 
  y=Yiel,
  group = 1)) + ...

有点混乱,因此您可以编写一个辅助功能来清理,

date_factor <- function(dates, format_string="%b %Y") {
  factor(format(dates, format_string), levels=format(sort(unique(dates)), format_string))
}
ggplot(koka, aes(x=date_factor(M_Datum), y=Yiel,group = 1)) + ...

从而导致以下图

“在此处输入图像说明”

You will need to put the levels of the factor in the correct order. You can do that with

ggplot(koka, aes(
  x=factor(format(M_Datum, "%b %Y"), levels=format(sort(unique(M_Datum)),"%b %Y")), 
  y=Yiel,
  group = 1)) + ...

Which is a bit messy so you could write a helper function to clean up up

date_factor <- function(dates, format_string="%b %Y") {
  factor(format(dates, format_string), levels=format(sort(unique(dates)), format_string))
}
ggplot(koka, aes(x=date_factor(M_Datum), y=Yiel,group = 1)) + ...

That results in the following plot

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文