Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Kolumam
Post Prodigy
Post Prodigy

R code Error

Hi All,

 

I am using R code to generate a scatter plot with moving average but when I run the code I am getting the below error.

library(ggplot2)
library(zoo, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
library(lubridate, warn.conflicts = FALSE)


dataset$Date_of_Cleaning__c = as.factor(dataset$Date_of_Cleaning__c)
dataset$Date_of_Cleaning__c = as.Date(dataset$Date_of_Cleaning__c,format="%d/%m/%Y")


# find moving average
zoo.avg <- zoo(dataset$Specific_Water_Usage__c)
ma1 <- rollapplyr(zoo.avg, 2, mean, fill = NA, na.rm = T)
#result$avgwu=coredata(ma1) 

#plot graph
p <- ggplot(data=dataset, aes(x= Date_of_Cleaning__c, y= Specific_Water_Usage__c, color= Name))+
  ylab("Water Usage") + geom_point()+
  ggtitle(paste0("Water Management")) +
  scale_x_date(date_breaks = "1 month",date_labels = "%b/%y")+
  theme(axis.title.y = element_text(size = 11, face = "bold",margin = margin(0,2,0,0)))+
  theme(axis.title.x = element_text(size = 11, face = "bold"))+
  theme(axis.text.x = element_text(size=11))+
  theme(axis.text.y = element_text(size=11))+
  theme(plot.title = element_text(face = "bold",size= 11,lineheight = 0.7,hjust = 0.5), plot.subtitle = element_text(face = "bold",size= 12,lineheight = 0.9,hjust = 0.5))+
  theme(plot.title=element_text(margin=margin(0,0,7,0)))+
  #geom_smooth(data=result, aes(x=Date, y=avgwu),color="red", size=1.5,se=FALSE)

Capture.JPG

 

2 REPLIES 2
v-chuncz-msft
Community Support
Community Support

@Kolumam

 

It seems that you may remove the last plus sign (+).

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

HI @v-chuncz-msft,

 

I had removed the (+), another issue is I was using ggplot to plot geom_point(), however, the date is overlapped each other.

So try to use scale_x_date(date_breaks = "1 month",date_labels = "%b/%y"), however is not working due to the date format. 

 

#date format

dataset$Date_of_Cleaning__c = as.factor(dataset$Date_of_Cleaning__c)
dataset$Date_of_Cleaning__c = as.Date(dataset$Date_of_Cleaning__c,format="%d/%m/%Y")

 

I did change the date format using the code above, it shows error in seq.int(0, to0 - from, by)...

 

Anyone can help me to solve this issue? 

is there any way to overcome the date overlapping each other?2.png

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors