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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
palvarez
New Member

R Script ggplot Line Plot

Hello,

 

I'm trying for the first time ever R Scripting with ggplot. However I've encountered a small roadblock. While attempting to do a line chart, why does my data plunges to 0 but lines back to the number it should be? My data doesn't behave in such way, so what am I missing? I'm truly an beginner in this topic, so it might be something dumb, however I haven't found any answers online yet.

 

image.png

 

 

library(ggplot2)
library(gtable)
library(grid)
#library(extrafont)

data <- dataset
mon <- data$Monto
trans <- data$Transacciones
fec <- format(as.Date(data$Fecha), "%Y/%m")

options(scipen=999)
p1 <- ggplot(data, aes(fec, mon, group = 1)) + geom_line()

p1

 

* Note: I've been attempting to create a visual of two y axes. That's why I'm using ggplot. I'm halfway there, but I need to get this fixed first.

1 ACCEPTED SOLUTION
v-sihou-msft
Microsoft Employee
Microsoft Employee

@palvarez

 

Based on your code, it seems you put date field into your dataset. I assume you have data on day level. As you format your Date into "YearMonth", which means you have multiple data point on same X axis category. This the reason why your line goes crazy. 

 

In your scenario, you should use stat_summary() function to have those data points aggregate on each "YearMonth". Please refer to my sample below: 

 

library(ggplot2)
library(gtable)

df <- dataset
trans <- df$Amount
fec <- format(as.Date(df$Date), "%Y/%m")
options(scipen=999)
p1 <- ggplot() +stat_summary(aes(x=fec, y=trans,group=1), fun.y = sum, geom = "line")
p1

123.PNG

 

Regards,

View solution in original post

1 REPLY 1
v-sihou-msft
Microsoft Employee
Microsoft Employee

@palvarez

 

Based on your code, it seems you put date field into your dataset. I assume you have data on day level. As you format your Date into "YearMonth", which means you have multiple data point on same X axis category. This the reason why your line goes crazy. 

 

In your scenario, you should use stat_summary() function to have those data points aggregate on each "YearMonth". Please refer to my sample below: 

 

library(ggplot2)
library(gtable)

df <- dataset
trans <- df$Amount
fec <- format(as.Date(df$Date), "%Y/%m")
options(scipen=999)
p1 <- ggplot() +stat_summary(aes(x=fec, y=trans,group=1), fun.y = sum, geom = "line")
p1

123.PNG

 

Regards,

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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