scale_x_date以及如何使其等距?

发布于 2025-02-14 01:47:23 字数 1330 浏览 3 评论 0原文

如何使用ggplot2软件包或其他任何包裹在造成的几个月之间删除“空白”我的X轴缺少某些月?换句话说,使X轴看起来是等距的,而没有那些“空白的镜头”。通过代码非常正常,它是关于绘制某些值与包含日期的其他列(并非所有月份都存在于该日期列中)。

 filtredplot1<-reactive({
    req(res_mod())
    dat<-res_mod() 
    dt<-dat[dat$M_Datum >= input$dateRange[1] & dat$M_Datum <= input$dateRange[2],]
    
    dt[,5]<-as.Date(format(as.Date(dt[,5]), "%Y-%m-01"))
    req(dt$M_Datum,dt$Yield)
    dr<-data.frame("M_Datum"=dt$M_Datum,"Yield"=dt$Yield)
  
    
    
    
    mydf=aggregate(Yield ~ M_Datum, dr, length) 
    
    req(mydf$M_Datum,mydf$Yield)
    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")+
      scale_x_date(labels="%b %Y")
      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("")
    
    
    
    
    
    
    
    
    
  })    
  

enter image description here

How can please using ggplot2 package or whatever else package remove that "blank space " between months caused by absence of certain months on my x axis ? In other words to make the x axis looks equidistant and not having those "blank gapes".By the code is very normal ,it is about plotting certain values vs other column containing dates (not all the months are present in that date column ).

 filtredplot1<-reactive({
    req(res_mod())
    dat<-res_mod() 
    dt<-dat[dat$M_Datum >= input$dateRange[1] & dat$M_Datum <= input$dateRange[2],]
    
    dt[,5]<-as.Date(format(as.Date(dt[,5]), "%Y-%m-01"))
    req(dt$M_Datum,dt$Yield)
    dr<-data.frame("M_Datum"=dt$M_Datum,"Yield"=dt$Yield)
  
    
    
    
    mydf=aggregate(Yield ~ M_Datum, dr, length) 
    
    req(mydf$M_Datum,mydf$Yield)
    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")+
      scale_x_date(labels="%b %Y")
      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-21 01:47:23

下面的代码不会创建数据。FRAMESDRmydf,您的数据准备代码太复杂了。以下要简单得多,并且可以工作。
另外,您在代码中两次具有your的错字yiel。第一个创建kokaaes()中的第二个时。

suppressPackageStartupMessages({
  library(shiny)
  library(ggplot2)
})

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$M_Datum <- as.Date(dt$M_Datum)
req(dt$M_Datum, dt$Yield)
koka <- aggregate(Yield ~ M_Datum, dt, length) 

ggplot(koka, aes(x = M_Datum, y = Yield, group = 1)) + 
  geom_point(size = 7, colour = "#EF783D", shape = 17) +
  geom_line(color = "#EF783D") +
  scale_x_date(date_breaks = "1 month", date_labels = "%b %Y") +
  xlab("") +
  ylab("Wafer Quantity") +
  theme(
    axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
    axis.text.y.left = element_text(color = "#EF783D"),
    axis.title.y.left = element_text(color = "#EF783D")
  )

The code below does not create the data.frames dr and mydf, your data preparation code is too complicated. The following is much simpler and works.
Also, you have the typo Yiel for Yield twice in your code. The first when creating koka and the second in aes().

suppressPackageStartupMessages({
  library(shiny)
  library(ggplot2)
})

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$M_Datum <- as.Date(dt$M_Datum)
req(dt$M_Datum, dt$Yield)
koka <- aggregate(Yield ~ M_Datum, dt, length) 

ggplot(koka, aes(x = M_Datum, y = Yield, group = 1)) + 
  geom_point(size = 7, colour = "#EF783D", shape = 17) +
  geom_line(color = "#EF783D") +
  scale_x_date(date_breaks = "1 month", date_labels = "%b %Y") +
  xlab("") +
  ylab("Wafer Quantity") +
  theme(
    axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
    axis.text.y.left = element_text(color = "#EF783D"),
    axis.title.y.left = element_text(color = "#EF783D")
  )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文