plot a data in rshiny

Hello,
I would like to plot in the same graph two dataset below with ggplot 2.
I can draw independent graph with ggplot but, how can I put them on the same graph with ggplot?
Do you have an idea?

The unit of the variable p is percent.

You will find below what I have done...
Thanks in advance to your help!

rm(list = ls())
library(dplyr)
#> 
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyverse)

df<-data.frame(
  Time=as.Date(c("2020/01/01", "2020/02/01", "2020/03/01")),
  Qte_total=as.numeric(c("780","803","819")),
  Qte_total_prio=as.numeric(c("10","23","30")),
  Qte_ouverture = as.numeric(c("20","40","5")),
  Qte_sortie=as.numeric(c("2","4","10"))
 )

p<-data.frame(Time=as.Date(c("2020/01/01", "2020/02/01", "2020/03/01")),
              OTD=as.numeric(c("0.1","0.3","0.2")))

df.l<-df %>%gather("types", "qte", -Time)

ggplot(df.l, aes(x=Time , y=qte,fill=types)) + 
       geom_bar(position="dodge", stat="identity")

image

ggplot(p,aes(x=Time,y=OTD))+
      geom_line()

image

library(tidyverse)

df<-data.frame(
  Time=as.Date(c("2020/01/01", "2020/02/01", "2020/03/01")),
  Qte_total=as.numeric(c("780","803","819")),
  Qte_total_prio=as.numeric(c("10","23","30")),
  Qte_ouverture = as.numeric(c("20","40","5")),
  Qte_sortie=as.numeric(c("2","4","10"))
)

p<-data.frame(Time=as.Date(c("2020/01/01", "2020/02/01", "2020/03/01")),
              OTD=as.numeric(c("0.1","0.3","0.2")))

df.l<-df %>%gather("types", "qte", -Time)

ggplot(df.l, aes(x=Time , y=qte,fill=types)) + 
  geom_bar(position="dodge", stat="identity") +
  geom_line(data=p,aes(x=Time,y=OTD,fill=NULL))

but how do you want to handle the fact that the y axis of both charts are different orders of magnitude?
Do the quantities relate ?

p.s this question doesnt appear to involve shiny at all. perhaps edit your thread title ?

2 Likes

Thanks!
p is a percent value and is calculated with Qte_sortie/Qte_total_prio
I re calculated the variable p with the good definition
How can I have a good scale for the second y axis?

I can't set the right scale for the second values of my y-axis.
I think I need to modify the OTD variable. I tried to put the p$OTD in factor . It is not good!!

var<-factor(p$OTD, levels = (unique(paste0(round((p$OTD)*100,0),"%"))), ordered=FALSE)

thanks in advance to your help!

rm(list = ls())
library(tidyverse)
library(lubridate)
#> 
#> Attachement du package : 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

df<-data.frame(
    Time=as.Date(c("2020/01/01", "2020/02/01", "2020/03/01")),
    Qte_total=as.numeric(c("780","803","819")),
    Qte_total_prio=as.numeric(c("10","23","30")),
    Qte_ouverture = as.numeric(c("20","40","5")),
    Qte_sortie=as.numeric(c("2","4","10"))
   )
df<-df%>%
      select(-.data$Qte_total)
  
p<-data.frame(Time=as.Date(c("2020/01/01", "2020/02/01", "2020/03/01")),
                OTD=as.numeric(c("0.1","0.17","0.33")))
df.l<-df %>%gather("types", "qte", -Time)  

ggplot(df.l, aes(x=Time , y=qte,fill=types)) + 
       geom_bar(position="dodge", stat="identity") +
       geom_line(data=p,aes(x=Time,y=OTD,fill=NULL)) +
       scale_y_continuous(name = "Qte", 
                          sec.axis = sec_axis(~./max(df.l$qte), name = "Qte_sortie/Qte_Total_prio", 
                          labels = function(b){paste0(round(b * 100, 0), "%")}),
                          limits = c(0,max(df.l$qte)))


Created on 2020-11-07 by the reprex package (v0.3.0)

image

The good code is :

rm(list = ls())
library(tidyverse)
library(lubridate)
#> 
#> Attachement du package : 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

dt<-data.frame(when=as.Date(c("2018-03-20 ","2018-03-21 ","2018-03-23","2018-03-24 ","2018-03-25")),
               numinter=c(1,5,4,3,4),
               prod=c(0.95,0.5,0.7,0.75,0.60)
                )

df<-data.frame(
        Time=as.Date(c("2020/01/01", "2020/02/01", "2020/03/01")),
        Qte_total=as.numeric(c("780","803","819")),
        Qte_total_prio=as.numeric(c("10","23","30")),
        Qte_ouverture = as.numeric(c("20","100","5")),
        Qte_sortie=as.numeric(c("2","4","10"))
)
df<-df%>%
  select(-.data$Qte_total)

p<-data.frame(Time=as.Date(c("2020/01/01", "2020/02/01", "2020/03/01")),
              OTD=c(0.1,0.4,0.5))
df.l<-df %>%gather("types", "qte", -Time)  

ggplot() + 
  geom_bar(df.l,mapping = aes(x = Time, y = qte,fill=types),position = "dodge", stat = "identity") +
  geom_line(p,mapping = aes(x = Time, y = OTD*max(df.l$qte)), size = 2, color = "blue") + 
  scale_x_date(name = "Day", labels = NULL) +
  scale_y_continuous(name = "Quantité", 
                     sec.axis = sec_axis(~./max(df.l$qte), name = "OTD", 
                                         labels = function(b) { paste0(round(b * 100, 0), "%")})) + 
  theme(
    axis.title.y = element_text(color = "grey"),
    axis.title.y.right = element_text(color = "blue"))

image
source :
more explanation here

Thanks to your help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.