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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
mars00
Regular Visitor

Custom visual R assistance with as.POSIXct

Hi,

I'm a total novice with R and I'm trying to get the below to work, however it seems I'm using the as.POSIXCT incorrect in order to get this line: " work scale_x_datetime(breaks = date_breaks("2 hour"), labels=date_format("%m-%d %Hh")) " to work. 

Would anyone be able to assist me with this? 

 

################### Actual code ####################
as.POSIXct('Start Time',format="%Y-%m-%dT%H:%M:%OS", tz ="GMT")
as.POSIXct('End Time',format="%Y-%m-%dT%H:%M:%OS", tz="GMT")

g =ggplot(Values ,aes(x='Start Time', y=CHEID, color= HNDTYP)) +
geom_segment(aes(x='Start Time',xend='End Time',yend=CHEID),size=15) +
scale_colour_discrete(guide=guide_legend(override.aes=list(size=10))) +
scale_x_datetime(breaks = date_breaks("2 hour"), labels=date_format("%m-%d %Hh"))+
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1)) +


ggtitle("Rescue by Time_Move and end_move") + xlab("Time") + ylab("Crane") + theme_bw();

below is the error i keep getting

mars00_0-1640688289387.png

 

to give a bit more context i was trying to convert this code 

 
# dataset <- data.frame(Time_Move, end_move, CHEID)
# dataset <- unique(dataset)

# Paste or type your script code here:
library(ggplot2)
library(scales)



# Convert timestamp to POSIXct and perform sort.
dataset$`Start Time`<-as.POSIXct(dataset$`Time_Move`,format="%Y-%m-%dT%H:%M:%OS", tz ="GMT")
dataset$`End Time`<-as.POSIXct(dataset$`end_move`,format="%Y-%m-%dT%H:%M:%OS", tz="GMT")



ggplot(dataset,aes(x=dataset$`Start Time`, y=dataset$CHEID, color= dataset$HNDTYP)) +
geom_segment(aes(x=dataset$`Start Time`,xend=dataset$`End Time`,yend=dataset$CHEID),size=15) +
scale_colour_discrete(guide=guide_legend(override.aes=list(size=10))) +
scale_x_datetime(breaks = date_breaks("2 hour"), labels=date_format("%m-%d %Hh"))+
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1)) +
 
 
 

ggtitle("Rescue by Time_Move and end_move") + xlab("Time") + ylab("Crane") + theme_bw();

 
Kind regards, Mars
1 ACCEPTED SOLUTION

But i can't seem to get the code to work when creating a custom visual, i fixed some pieces which i still did wrong, but it states it is unable to find the function while the logic does work in the R script editor.

source('./r_files/flatten_HTML.r')

############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
libraryRequireInstall("htmlwidgets")
####################################################

################### Actual code ####################
dataset <- Values

dataset$`Start Time` <- as.POSIXct(dataset$`Time_Move`,format="%Y-%m-%dT%H:%M:%OS", tz ="GMT")
dataset$`end_move`<- as.POSIXct(dataset$`end_move`,format="%Y-%m-%dT%H:%M:%OS", tz="GMT")

g =ggplot(Values ,aes(x=dataset$`Start Time`, y=dataset$`CHEID`, color= dataset$`HNDTYP`)) +
geom_segment(aes(x=dataset$`Start Time`,xend=dataset$`End Time` ,yend=dataset$`CHEID`),size=15) +
scale_colour_discrete(guide=guide_legend(override.aes=list(size=10))) +
scale_x_datetime(breaks = date_breaks("1 hour"), labels=date_format("%m-%d %Hh"))+
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1)) +


ggtitle("Rescue by Time_Move and end_move") + xlab("Time") + ylab("Crane") + theme_bw();
####################################################

############# Create and save widget ###############
p = ggplotly(g);
internalSaveWidget(p, 'out.html');
####################################################

################ Reduce paddings ###################
ReadFullFileReplaceString('out.html', 'out.html', ',"padding":[0-9]*,', ',"padding":0,')
####################################################



mars00_0-1641284454111.png

 

View solution in original post

3 REPLIES 3
V-lianl-msft
Community Support
Community Support

Hi @mars00 ,

 

Power bi somehow resturns "Microsft.OleDb.Date" as the result. Please refer to R script in Power BI returns date as Microsoft.OleDb.Date - Stack Overflow 

 

1. As mentioned in the solution, first change the date type to Text type. 

2. Run the R script

3. Change the datatype back to date once the script has provided an output.


Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

But i can't seem to get the code to work when creating a custom visual, i fixed some pieces which i still did wrong, but it states it is unable to find the function while the logic does work in the R script editor.

source('./r_files/flatten_HTML.r')

############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
libraryRequireInstall("htmlwidgets")
####################################################

################### Actual code ####################
dataset <- Values

dataset$`Start Time` <- as.POSIXct(dataset$`Time_Move`,format="%Y-%m-%dT%H:%M:%OS", tz ="GMT")
dataset$`end_move`<- as.POSIXct(dataset$`end_move`,format="%Y-%m-%dT%H:%M:%OS", tz="GMT")

g =ggplot(Values ,aes(x=dataset$`Start Time`, y=dataset$`CHEID`, color= dataset$`HNDTYP`)) +
geom_segment(aes(x=dataset$`Start Time`,xend=dataset$`End Time` ,yend=dataset$`CHEID`),size=15) +
scale_colour_discrete(guide=guide_legend(override.aes=list(size=10))) +
scale_x_datetime(breaks = date_breaks("1 hour"), labels=date_format("%m-%d %Hh"))+
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1)) +


ggtitle("Rescue by Time_Move and end_move") + xlab("Time") + ylab("Crane") + theme_bw();
####################################################

############# Create and save widget ###############
p = ggplotly(g);
internalSaveWidget(p, 'out.html');
####################################################

################ Reduce paddings ###################
ReadFullFileReplaceString('out.html', 'out.html', ',"padding":[0-9]*,', ',"padding":0,')
####################################################



mars00_0-1641284454111.png

 

never mind, I forget a package and install in the code, now i just need to figgure out why the end time doesn't seem to plot

mars00_0-1641285632165.png

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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!

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.