#R shiny non-numeric argument to binary operator - r

I am trying to make a problem more reactive to new inputs here is where the error is given
Pwh_Design=reactive((input$Pwh+0.2*(input$Pso-input$Pwh)))
Gtd=Pwh_Design-P_operating_point_formation/(0-D_operating_point)
this is in the server.
the entire app:
UI:
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("OTIS Gas Lift"),
fluidRow(column(width = 6,
numericInput(inputId = "Dmax",label = "Depth to mid perforation",value = 7500),
numericInput("Pwh","Pwh",100),
numericInput("Tres","Tres",182),
numericInput("Pso","P Casing at Surface",870),
numericInput("Psc","P Casing at Depth D1",1050),
numericInput("D1","D1",7000),
numericInput("P_across_valve","P_across_valve",100),
numericInput("GL","Load Grad",0.5),
numericInput("Pwf","Pwf",1760),
numericInput("RGOR","Required GOR",400)
),
column(width = 6,
numericInput("FGOR","Formation GOR",200),
numericInput("Twh","Twh",100),
numericInput("Pko","kill pressure at Surface",920),
numericInput("Pk","Kill Pressure at D2",1100),
numericInput("D2","D2",7000),
numericInput("T_inj","T_inj",100),
numericInput("q","Desired Production Rate STB/d",600),
numericInput("R","R",0.1534),
numericInput("Gf","Flowing Grad before inj",0.4)
)),
fluidRow(submitButton(text = "Apply Changes",width = "100%")),
splitLayout(tableOutput(outputId = "table"),plotOutput(outputId = "plot"))))
'''
This is the server
library(shiny)
shinyServer(function(input, output) {
Dmax=reactiveValues() #to mid perforation ft
#one of those two next must be given
FGOR=reactiveValues() #scf/STB Formation GOR
Pwh=reactiveValues() #psig
Twh=reactiveValues() #degree fahrenheit
Tres=reactiveValues() #degree fahrenheit
#casing and kill gradients
Pso=reactiveValues() #psig
Pcs=reactiveValues() #psig at 7000ft
D1=reactiveValues() #ft
Pko=reactiveValues() #psig
Pk=reactiveValues() #psig at 7000ft
D2=reactiveValues() #ft
P_across_valve=reactiveValues() #psig
T_inj=reactiveValues() #degree fahrenheit
GL=reactiveValues() #load gradient (kill fluid grad)
q=reactiveValues()
Pwf=reactiveValues() #psi or BHP
RGOR=reactiveValues() #scf/STB required GOR
R=reactiveValues()
Gf=reactiveValues() #flowing gradient before injection default value
library(ggplot2)
library(dplyr)
#plot basics
plot<-reactive(ggplot+
coord_cartesian(xlim = c(0,input$Pwf+500))+
scale_y_continuous(trans = "reverse")+
scale_x_continuous(position = "top"))
#Flowing Gradient
#slope Gf
#point (pwf,Dmax)
#get p1 at depth 4000ft
p1=reactive(input$Pwf-input$Gf*(input$Dmax-4000))
flowing_gradient<-reactive(data.frame(D=c(4000,input$Dmax),P=c(p1,input$Pwf)))
plot0<-plot+
geom_point(data = flowing_gradient,aes(x=P,y=D))+
geom_line(data = flowing_gradient,aes(x=P,y=D))
#casing gradient
casing_gradient<-reactive(data.frame(D=c(0,input$D1),P=c(input$Pso,input$Pcs)))
plot1<-plot0+geom_point(data=casing_gradient,aes(x=P,y=D))+
geom_line(data=casing_gradient,aes(x=P,y=D))
Gc=reactive((input$Pso-input$Pcs)/(0-input$D1))
#killing gradient
killing_gradient<-reactive(data.frame(D=c(0,input$D2),P=c(input$Pko,input$Pk)))
plot2<-plot1+geom_point(data=killing_gradient,aes(x=P,y=D))+
geom_line(data=killing_gradient,aes(x=P,y=D))
Gk=reactive((input$Pko-input$Pk)/(0-input$D2))
#balance point
d1=reactive((input$Pwf-input$Pso-input$Gf*input$Dmax)/(Gc-input$Gf))
d_balance_point=d1
#operating_point
d1=reactive((input$Pso-input$Pwf+input$Gf*input$Dmax-input$P_across_valve)/(input$Gf-Gc))
D_operating_point=d1
P_operating_point_casing=reactive(input$Pso+D_operating_point*Gc)
P_operating_point_formation=reactive(input$Pwf-input$Gf*(input$Dmax-D_operating_point))
plot3<-plot2+geom_point(aes(x=P_operating_point_formation,y=D_operating_point))
#Tubing grad
tubing_grad<-reactive(data.frame(D=c(0,D_operating_point),P=c(input$Pwh,P_operating_point_formation)))
plot4<-plot3+geom_point(data = tubing_grad,aes(x=P,y=D))+
geom_line(data = tubing_grad,aes(x=P,y=D))
#Tubing_design_grad
Pwh_Design=reactive((input$Pwh+0.2*(input$Pso-input$Pwh)))
Gtd=Pwh_Design-P_operating_point_formation/(0-D_operating_point) #Tubing design grad
tubing_design_grad<-data.frame(D=c(0,D_operating_point),P=c(Pwh_Design,P_operating_point_formation))
plot5<-plot4+geom_point(data = tubing_design_grad,aes(x=P,y=D))+
geom_line(data = tubing_design_grad,aes(x=P,y=D))
#first valve
D_valve1=reactive((input$Pwh-input$Pko)/(Gk-input$GL))
PK_valve1=reactive(D_valve1*Gk+input$Pko)
PTD_valve1=Gtd*D_valve1+Pwh_Design
df<-reactive(data.frame(D=c(0,D_valve1),P=c(input$Pwh,PK_valve1)))
#ploting
plot6<-plot5+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D))
df<-data.frame(D=c(D_valve1,D_valve1),P=c(PTD_valve1,PK_valve1))
plot7<-plot6+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D))
plot<-plot7
Final_table<-data.frame(depth=D_valve1,
Pc=PK_valve1,
Pt=PTD_valve1)
dvalve=D_valve1
PTDvalve=PTD_valve1
Pcvalve=PK_valve1
D_valve=D_valve1
PTD_valve=PTD_valve1
while (dvalve<D_operating_point) {
dvalve=reactive((PTDvalve-input$Pso-dvalve*input$GL)/(Gc-input$GL))
Pcvalve=reactive(input$Pso+Gc*dvalve)
if(dvalve<D_operating_point){
df<-data.frame(D=c(D_valve,dvalve),P=c(PTD_valve,Pcvalve))
plot<-plot+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D))
PTDvalve=Gtd*dvalve+Pwh_Design
df<-data.frame(D=c(dvalve,dvalve),P=c(PTDvalve,Pcvalve))
plot<-plot+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D))
valve<-c(dvalve,Pcvalve,PTDvalve)
Final_table<-rbind(Final_table,valve)
D_valve=dvalve
PTD_valve=PTDvalve
}}
output$plot<-renderPlot(plot)
valve<-c(D_operating_point,P_operating_point_casing,P_operating_point_formation)
Final_table<-rbind(Final_table,valve)
GTemp=reactive((input$Tres-input$Twh)/input$Dmax)
CT_data<-data.frame(Temp=c(61:300),Ct=c(0.998,0.996,0.994,0.991,0.989,0.987,0.985,0.983,0.981,0.979,0.977,0.975,0.973,0.971,0.969,0.967,0.965,0.963,0.961,0.959,0.957,0.955,0.953,0.951,0.949,0.947,0.945,0.943,0.941,0.939,0.938,0.936,0.934,0.932,0.93,0.928,0.926,0.924,0.923,0.921,0.919,0.917,0.915,0.914,0.912,0.91,0.908,0.906,0.905,0.903,0.901,0.899,0.898,0.896,0.894,0.893,0.891,0.889,0.887,0.886,0.884,0.882,0.881,0.879,0.877,0.876,0.874,0.872,0.871,0.869,0.686,0.866,0.864,0.863,0.861,0.86,0.858,0.856,0.855,0.853,0.852,0.85,0.849,0.847,0.845,0.844,0.842,0.841,0.839,0.838,0.836,0.835,0.833,0.832,0.83,0.829,0.827,0.826,0.825,0.823,0.822,0.82,0.819,0.817,0.816,0.814,0.813,0.812,0.81,0.809,0.807,0.806,0.805,0.803,0.802,0.8,0.799,0.798,0.796,0.795,0.794,0.792,0.791,0.79,0.788,0.787,0.786,0.784,0.783,0.782,0.78,0.779,0.778,0.776,0.775,0.774,0.772,0.771,0.77,0.769,0.767,0.766,0.765,0.764,0.762,0.761,0.76,0.759,0.757,0.756,0.755,0.754,0.752,0.751,0.75,0.749,0.748,0.746,0.745,0.744,0.743,0.742,0.74,0.739,0.738,0.737,0.736,0.735,0.733,0.732,0.731,0.73,0.729,0.728,0.727,0.725,0.724,0.723,0.722,0.721,0.72,0.719,0.718,0.717,0.715,0.714,0.713,0.712,0.711,0.71,0.709,0.708,0.707,0.706,0.705,0.704,0.702,0.701,0.7,0.699,0.698,0.697,0.696,0.695,0.694,0.693,0.692,0.691,0.69,0.689,0.688,0.687,0.686,0.685,0.684,0.683,0.682,0.681,0.68,0.679,0.678,0.677,0.676,0.675,0.674,0.673,0.672,0.671,0.67,0.699,0.668,0.667,0.666,0.665,0.664,0.663,0.662,0.662,0.661,0.66))
Final_table<-Final_table %>% mutate(reactive(Pbt=input$Pc*(1-input$R)+Pt*input$R),
reactive(TEMP=input$Twh+GTemp*depth),
Temp=ceiling(TEMP))
Final_table<-merge(x=Final_table,y=CT_data,by = "Temp",all.x = T)
Final_table<-Final_table%>%select(depth,Pc,Pt,Pbt,TEMP,Ct)
Final_table<-Final_table %>% mutate(Pb=Pbt*Ct,
Pvo=reactive(Pb/(1-input$R)))
output$table<-tableOutput(Final_table)})
I tried making any variable with reactive assignment included in () but it gives this error4
"Warning: Error in : Operation not allowed without an active reactive context.
You tried to do something that can only be done from inside a reactive consumer.
58:
Error : Operation not allowed without an active reactive context.
You tried to do something that can only be done from inside a reactive consumer."
I am pretty sure i did it to only the reactive assigned variables when recalling them
EDIT
After I tried to solve the problem it gives:
Warning: Error in data.frame: arguments imply differing number of rows: 1, 0
1: runApp
whenever I press the action button
the new server
library(shiny)
library(ggplot2)
library(dplyr)
shinyServer(function(input, output) {
plot<-reactiveVal(NULL)
Final_table<-reactiveVal(data.frame())
observeEvent(input$submit,{
plot(ggplot()+
coord_cartesian(xlim = c(0,input$Pwf+500))+
scale_y_continuous(trans = "reverse")+
scale_x_continuous(position = "top"))
#Flowing Gradient
#slope Gf
#point (pwf,Dmax)
#get p1 at depth 4000ft
p1=input$Pwf-input$Gf*(input$Dmax-4000)
flowing_gradient<-data.frame(D=c(4000,input$Dmax),P=c(p1,input$Pwf))
plot(plot()+
geom_point(data = flowing_gradient,aes(x=P,y=D))+
geom_line(data = flowing_gradient,aes(x=P,y=D)))
#casing gradient
casing_gradient<-data.frame(D=c(0,input$D1),P=c(input$Pso,input$Pcs))
plot(plot()+geom_point(data=casing_gradient,aes(x=P,y=D))+
geom_line(data=casing_gradient,aes(x=P,y=D)))
Gc=(input$Pso-input$Pcs)/(0-input$D1)
#killing gradient
killing_gradient<-data.frame(D=c(0,input$D2),P=c(input$Pko,input$Pk))
plot(plot()+geom_point(data=killing_gradient,aes(x=P,y=D))+
geom_line(data=killing_gradient,aes(x=P,y=D)))
Gk=(input$Pko-input$Pk)/(0-input$D2)
#balance point
d1=(input$Pwf-input$Pso-input$Gf*input$Dmax)/(Gc-input$Gf)
d_balance_point=d1
#operating_point
d1=(input$Pso-input$Pwf+input$Gf*input$Dmax-input$P_across_valve)/(input$Gf-Gc)
D_operating_point=d1
P_operating_point_casing=input$Pso+D_operating_point*Gc
P_operating_point_formation=input$Pwf-input$Gf*(input$Dmax-D_operating_point)
plot(plot()+geom_point(aes(x=P_operating_point_formation,y=D_operating_point)))
#Tubing grad
tubing_grad<-data.frame(D=c(0,D_operating_point),P=c(input$Pwh,P_operating_point_formation))
plot(plot()+geom_point(data = tubing_grad,aes(x=P,y=D))+
geom_line(data = tubing_grad,aes(x=P,y=D)))
#Tubing_design_grad
Pwh_design=input$Pwh+0.2*(input$Pso-input$Pwh)
Gtd=(Pwh_design-P_operating_point_formation)/(0-D_operating_point) #Tubing design grad
tubing_design_grad<-data.frame(D=c(0,D_operating_point),P=c(Pwh_design,P_operating_point_formation))
plot(plot()+geom_point(data = tubing_design_grad,aes(x=P,y=D))+
geom_line(data = tubing_design_grad,aes(x=P,y=D)))
#first valve
D_valve1=(input$Pwh-input$Pko)/(Gk-input$GL)
PK_valve1=D_valve1*Gk+input$Pko
PTD_valve1=Gtd*D_valve1+Pwh_design
df<-data.frame(D=c(0,D_valve1),P=c(input$Pwh,PK_valve1))
#ploting
plot(plot()+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D)))
df<-data.frame(D=c(D_valve1,D_valve1),P=c(PTD_valve1,PK_valve1))
plot(plot()+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D)))
Final_table(data.frame(depth=D_valve1,
Pc=PK_valve1,
Pt=PTD_valve1))
dvalve=D_valve1
PTDvalve=PTD_valve1
Pcvalve=PK_valve1
D_valve=D_valve1
PTD_valve=PTD_valve1
while (dvalve<D_operating_point) {
dvalve=(PTDvalve-input$Pso-dvalve*input$GL)/(Gc-input$GL)
Pcvalve=input$Pso+Gc*dvalve
if(dvalve<D_operating_point){
df<-data.frame(D=c(D_valve,dvalve),P=c(PTD_valve,Pcvalve))
plot(plot()+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D)))
PTDvalve=Gtd*dvalve+Pwh_design
df<-data.frame(D=c(dvalve,dvalve),P=c(PTDvalve,Pcvalve))
plot(plot()+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D)))
valve<-c(dvalve,Pcvalve,PTDvalve)
Final_table(rbind(Final_table(),valve))
D_valve=dvalve
PTD_valve=PTDvalve
}}
valve<-c(D_operating_point,P_operating_point_casing,P_operating_point_formation)
Final_table(rbind(Final_table(),valve))
GTemp=(input$Tres-input$Twh)/input$Dmax
CT_data<-data.frame(Temp=c(61:300),Ct=c(0.998,0.996,0.994,0.991,0.989,0.987,0.985,0.983,0.981,0.979,0.977,0.975,0.973,0.971,0.969,0.967,0.965,0.963,0.961,0.959,0.957,0.955,0.953,0.951,0.949,0.947,0.945,0.943,0.941,0.939,0.938,0.936,0.934,0.932,0.93,0.928,0.926,0.924,0.923,0.921,0.919,0.917,0.915,0.914,0.912,0.91,0.908,0.906,0.905,0.903,0.901,0.899,0.898,0.896,0.894,0.893,0.891,0.889,0.887,0.886,0.884,0.882,0.881,0.879,0.877,0.876,0.874,0.872,0.871,0.869,0.686,0.866,0.864,0.863,0.861,0.86,0.858,0.856,0.855,0.853,0.852,0.85,0.849,0.847,0.845,0.844,0.842,0.841,0.839,0.838,0.836,0.835,0.833,0.832,0.83,0.829,0.827,0.826,0.825,0.823,0.822,0.82,0.819,0.817,0.816,0.814,0.813,0.812,0.81,0.809,0.807,0.806,0.805,0.803,0.802,0.8,0.799,0.798,0.796,0.795,0.794,0.792,0.791,0.79,0.788,0.787,0.786,0.784,0.783,0.782,0.78,0.779,0.778,0.776,0.775,0.774,0.772,0.771,0.77,0.769,0.767,0.766,0.765,0.764,0.762,0.761,0.76,0.759,0.757,0.756,0.755,0.754,0.752,0.751,0.75,0.749,0.748,0.746,0.745,0.744,0.743,0.742,0.74,0.739,0.738,0.737,0.736,0.735,0.733,0.732,0.731,0.73,0.729,0.728,0.727,0.725,0.724,0.723,0.722,0.721,0.72,0.719,0.718,0.717,0.715,0.714,0.713,0.712,0.711,0.71,0.709,0.708,0.707,0.706,0.705,0.704,0.702,0.701,0.7,0.699,0.698,0.697,0.696,0.695,0.694,0.693,0.692,0.691,0.69,0.689,0.688,0.687,0.686,0.685,0.684,0.683,0.682,0.681,0.68,0.679,0.678,0.677,0.676,0.675,0.674,0.673,0.672,0.671,0.67,0.699,0.668,0.667,0.666,0.665,0.664,0.663,0.662,0.662,0.661,0.66))
Final_table(Final_table() %>% mutate(Pbt=Pc*(1-input$R)+Pt*input$R,
TEMP=input$Twh+GTemp*depth,
Temp=ceiling(TEMP)))
Final_table(merge(x=Final_table(),y=CT_data,by = "Temp",all.x = T))
Final_table(Final_table()%>%select(depth,Pc,Pt,Pbt,TEMP,Ct))
Final_table(Final_table() %>% mutate(Pb=Pbt*Ct,
Pvo=Pb/(1-input$R)))
})
output$plot<-renderPlot({
isolate(plot())
})
output$table<-renderTable({
isolate(Final_table())
})
})

Here's a MRE up to the first plot to ilustrate how to deal with reactive objects. Notice the use of () when accessing a reactive value.
library(shiny)
library(tidyverse)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("OTIS Gas Lift"),
fluidRow(column(width = 6,
numericInput(inputId = "Dmax",label = "Depth to mid perforation",value = 7500),
numericInput("Pwh","Pwh",100),
numericInput("Tres","Tres",182),
numericInput("Pso","P Casing at Surface",870),
numericInput("Psc","P Casing at Depth D1",1050),
numericInput("D1","D1",7000),
numericInput("P_across_valve","P_across_valve",100),
numericInput("GL","Load Grad",0.5),
numericInput("Pwf","Pwf",1760),
numericInput("RGOR","Required GOR",400)
),
column(width = 6,
numericInput("FGOR","Formation GOR",200),
numericInput("Twh","Twh",100),
numericInput("Pko","kill pressure at Surface",920),
numericInput("Pk","Kill Pressure at D2",1100),
numericInput("D2","D2",7000),
numericInput("T_inj","T_inj",100),
numericInput("q","Desired Production Rate STB/d",600),
numericInput("R","R",0.1534),
numericInput("Gf","Flowing Grad before inj",0.4)
)),
fluidRow(actionButton('submit' ,label = "Apply Changes",width = "100%")),
splitLayout(tableOutput(outputId = "table"),plotOutput(outputId = "plot"))
)
library(shiny)
server <- function(input, output) {
#define plot object that can be accessed inside of reactive() or observe()
plot_rv <- reactiveVal(NULL)
flowing_gradient_rv <- reactiveVal(NULL)
#inside this observer we can create regular local objects (only accesible inside the observer) like when creating functions.
#notice the code wrapped in curly braces {} to be able to pass multiple lines of code into `observeEvent()` second argument.
#input$submit corresponds to the action button created in the ui.
observeEvent(input$submit, {
plot_rv(ggplot() +
coord_cartesian(xlim = c(0, input$Pwf + 500)) +
scale_y_continuous(trans = "reverse") +
scale_x_continuous(position = "top"))
#Flowing Gradient
#slope Gf
#point (pwf,Dmax)
#get p1 at depth 4000ft
#input values are now available without calling reactive since we already are "inside" the observer
p1 <- input$Pwf - input$Gf * (input$Dmax - 4000)
flowing_gradient <- data.frame(D = c(4000, input$Dmax), P = c(p1, input$Pwf))
#to use flowing_gradient outside this observer we use flowing_gradient reactiveVal
flowing_gradient_rv(flowing_gradient)
plot_rv(plot_rv() +
geom_point(data = flowing_gradient,aes(x=P,y=D))+
geom_line(data = flowing_gradient,aes(x=P,y=D))
)
})
#to show the plots
output$plot <- renderPlot({
plot_rv()
})
output$table <- renderTable(
flowing_gradient_rv()
)
}
shinyApp(ui, server)
Edit: The edited app currently working.
library(shiny)
library(tidyverse)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("OTIS Gas Lift"),
fluidRow(
column(
width = 6,
numericInput(inputId = "Dmax", label = "Depth to mid perforation", value = 7500),
numericInput("Pwh", "Pwh", 100),
numericInput("Tres", "Tres", 182),
numericInput("Pso", "P Casing at Surface", 870),
numericInput("Psc", "P Casing at Depth D1", 1050),
numericInput("D1", "D1", 7000),
numericInput("P_across_valve", "P_across_valve", 100),
numericInput("GL", "Load Grad", 0.5),
numericInput("Pwf", "Pwf", 1760),
numericInput("RGOR", "Required GOR", 400)
),
column(
width = 6,
numericInput("FGOR", "Formation GOR", 200),
numericInput("Twh", "Twh", 100),
numericInput("Pko", "kill pressure at Surface", 920),
numericInput("Pk", "Kill Pressure at D2", 1100),
numericInput("D2", "D2", 7000),
numericInput("T_inj", "T_inj", 100),
numericInput("q", "Desired Production Rate STB/d", 600),
numericInput("R", "R", 0.1534),
numericInput("Gf", "Flowing Grad before inj", 0.4)
)
),
fluidRow(actionButton("submit", label = "Apply Changes", width = "100%")),
splitLayout(tableOutput(outputId = "table"), plotOutput(outputId = "plot"))
)
library(shiny)
library(shiny)
library(ggplot2)
library(dplyr)
server <- function(input, output) {
plot <- reactiveVal(NULL)
Final_table <- reactiveVal(data.frame())
observeEvent(input$submit, {
plot(ggplot() +
coord_cartesian(xlim = c(0, input$Pwf + 500)) +
scale_y_continuous(trans = "reverse") +
scale_x_continuous(position = "top"))
# Flowing Gradient
# slope Gf
# point (pwf,Dmax)
# get p1 at depth 4000ft
p1 <- input$Pwf - input$Gf * (input$Dmax - 4000)
flowing_gradient <- data.frame(D = c(4000, input$Dmax), P = c(p1, input$Pwf))
plot(plot() +
geom_point(data = flowing_gradient, aes(x = P, y = D)) +
geom_line(data = flowing_gradient, aes(x = P, y = D)))
# casing gradient
casing_gradient <- data.frame(D = c(0, input$D1), P = c(input$Pso, input$Pcs))
plot(plot() + geom_point(data = casing_gradient, aes(x = P, y = D)) +
geom_line(data = casing_gradient, aes(x = P, y = D)))
Gc <- (input$Pso - input$Psc) / (0 - input$D1)
# killing gradient
killing_gradient <- data.frame(D = c(0, input$D2), P = c(input$Pko, input$Pk))
plot(plot() + geom_point(data = killing_gradient, aes(x = P, y = D)) +
geom_line(data = killing_gradient, aes(x = P, y = D)))
Gk <- (input$Pko - input$Pk) / (0 - input$D2)
# balance point
d1 <- (input$Pwf - input$Pso - input$Gf * input$Dmax) / (Gc - input$Gf)
d_balance_point <- d1
# operating_point
d1 <- (input$Pso - input$Pwf + input$Gf * input$Dmax - input$P_across_valve) / (input$Gf - Gc)
D_operating_point <- d1
P_operating_point_casing <- input$Pso + D_operating_point * Gc
P_operating_point_formation <- input$Pwf - input$Gf * (input$Dmax - D_operating_point)
plot(plot() + geom_point(aes(x = P_operating_point_formation, y = D_operating_point)))
# Tubing grad
tubing_grad <- data.frame(D = c(0, D_operating_point), P = c(input$Pwh, P_operating_point_formation))
plot(plot() + geom_point(data = tubing_grad, aes(x = P, y = D)) +
geom_line(data = tubing_grad, aes(x = P, y = D)))
# Tubing_design_grad
Pwh_design <- input$Pwh + 0.2 * (input$Pso - input$Pwh)
Gtd <- (Pwh_design - P_operating_point_formation) / (0 - D_operating_point)# Tubing design grad
tubing_design_grad <- data.frame(D = c(0, D_operating_point), P = c(Pwh_design, P_operating_point_formation))
plot(plot() + geom_point(data = tubing_design_grad, aes(x = P, y = D)) +
geom_line(data = tubing_design_grad, aes(x = P, y = D)))
# first valve
D_valve1 <- (input$Pwh - input$Pko) / (Gk - input$GL)
PK_valve1 <- D_valve1 * Gk + input$Pko
PTD_valve1 <- Gtd * D_valve1 + Pwh_design
df <- data.frame(D = c(0, D_valve1), P = c(input$Pwh, PK_valve1))
# ploting
plot(plot() + geom_point(data = df, aes(x = P, y = D)) +
geom_line(data = df, aes(x = P, y = D)))
df <- data.frame(D = c(D_valve1, D_valve1), P = c(PTD_valve1, PK_valve1))
plot(plot() + geom_point(data = df, aes(x = P, y = D)) +
geom_line(data = df, aes(x = P, y = D)))
Final_table(data.frame(
depth = D_valve1,
Pc = PK_valve1,
Pt = PTD_valve1
))
dvalve <- D_valve1
PTDvalve <- PTD_valve1
Pcvalve <- PK_valve1
D_valve <- D_valve1
PTD_valve <- PTD_valve1
while (dvalve < D_operating_point) {
dvalve <- (PTDvalve - input$Pso - dvalve * input$GL) / (Gc - input$GL)
Pcvalve <- input$Pso + Gc * dvalve
if (dvalve < D_operating_point) {
df <- data.frame(D = c(D_valve, dvalve), P = c(PTD_valve, Pcvalve))
plot(plot() + geom_point(data = df, aes(x = P, y = D)) +
geom_line(data = df, aes(x = P, y = D)))
PTDvalve <- Gtd * dvalve + Pwh_design
df <- data.frame(D = c(dvalve, dvalve), P = c(PTDvalve, Pcvalve))
plot(plot() + geom_point(data = df, aes(x = P, y = D)) +
geom_line(data = df, aes(x = P, y = D)))
valve <- c(dvalve, Pcvalve, PTDvalve)
Final_table(rbind(Final_table(), valve))
D_valve <- dvalve
PTD_valve <- PTDvalve
}
}
valve <- c(D_operating_point, P_operating_point_casing, P_operating_point_formation)
Final_table(rbind(Final_table(), valve))
GTemp <- (input$Tres - input$Twh) / input$Dmax
CT_data <- data.frame(Temp = c(61:300), Ct = c(0.998, 0.996, 0.994, 0.991, 0.989, 0.987, 0.985, 0.983, 0.981, 0.979, 0.977, 0.975, 0.973, 0.971, 0.969, 0.967, 0.965, 0.963, 0.961, 0.959, 0.957, 0.955, 0.953, 0.951, 0.949, 0.947, 0.945, 0.943, 0.941, 0.939, 0.938, 0.936, 0.934, 0.932, 0.93, 0.928, 0.926, 0.924, 0.923, 0.921, 0.919, 0.917, 0.915, 0.914, 0.912, 0.91, 0.908, 0.906, 0.905, 0.903, 0.901, 0.899, 0.898, 0.896, 0.894, 0.893, 0.891, 0.889, 0.887, 0.886, 0.884, 0.882, 0.881, 0.879, 0.877, 0.876, 0.874, 0.872, 0.871, 0.869, 0.686, 0.866, 0.864, 0.863, 0.861, 0.86, 0.858, 0.856, 0.855, 0.853, 0.852, 0.85, 0.849, 0.847, 0.845, 0.844, 0.842, 0.841, 0.839, 0.838, 0.836, 0.835, 0.833, 0.832, 0.83, 0.829, 0.827, 0.826, 0.825, 0.823, 0.822, 0.82, 0.819, 0.817, 0.816, 0.814, 0.813, 0.812, 0.81, 0.809, 0.807, 0.806, 0.805, 0.803, 0.802, 0.8, 0.799, 0.798, 0.796, 0.795, 0.794, 0.792, 0.791, 0.79, 0.788, 0.787, 0.786, 0.784, 0.783, 0.782, 0.78, 0.779, 0.778, 0.776, 0.775, 0.774, 0.772, 0.771, 0.77, 0.769, 0.767, 0.766, 0.765, 0.764, 0.762, 0.761, 0.76, 0.759, 0.757, 0.756, 0.755, 0.754, 0.752, 0.751, 0.75, 0.749, 0.748, 0.746, 0.745, 0.744, 0.743, 0.742, 0.74, 0.739, 0.738, 0.737, 0.736, 0.735, 0.733, 0.732, 0.731, 0.73, 0.729, 0.728, 0.727, 0.725, 0.724, 0.723, 0.722, 0.721, 0.72, 0.719, 0.718, 0.717, 0.715, 0.714, 0.713, 0.712, 0.711, 0.71, 0.709, 0.708, 0.707, 0.706, 0.705, 0.704, 0.702, 0.701, 0.7, 0.699, 0.698, 0.697, 0.696, 0.695, 0.694, 0.693, 0.692, 0.691, 0.69, 0.689, 0.688, 0.687, 0.686, 0.685, 0.684, 0.683, 0.682, 0.681, 0.68, 0.679, 0.678, 0.677, 0.676, 0.675, 0.674, 0.673, 0.672, 0.671, 0.67, 0.699, 0.668, 0.667, 0.666, 0.665, 0.664, 0.663, 0.662, 0.662, 0.661, 0.66))
Final_table(Final_table() %>% mutate(
Pbt = Pc * (1 - input$R) + Pt * input$R,
TEMP = input$Twh + GTemp * depth,
Temp = ceiling(TEMP)
))
Final_table(merge(x = Final_table(), y = CT_data, by = "Temp", all.x = T))
Final_table(Final_table() %>% select(depth, Pc, Pt, Pbt, TEMP, Ct))
Final_table(Final_table() %>% mutate(
Pb = Pbt * Ct,
Pvo = Pb / (1 - input$R)
))
})
output$plot <- renderPlot({
plot()
})
output$table <- renderTable({
Final_table()
})
}
shinyApp(ui, server)

Related

Add legend based on line types and colors in ggplot

I have created the following plot using plot() function and I would like to convert it to ggplot() and add colors in the line types like:
and also a legend for 'predicted' (normal line) and 'observed' values (dashed line) like:
Here is my code:
# Creating some data first
scoregroepen <- seq(from = 1, to = 8, by = 1)
s_toets_observed <- c(0.18, 0.31, 0.42, 0.53, 0.64,0.75,0.84,0.95)
s_toets_predicted <- c(0.20, 0.29, 0.40, 0.55, 0.66, 0.75, 0.85, 0.94)
s_toets_conf_low <- s_toets_observed-0.03
s_toets_conf_high <- s_toets_observed+0.045
plot(scoregroepen,s_toets_predicted, type="b", ylab = "proporties", ylim = c(0,1))
lines(scoregroepen, s_toets_observed, type="b", lty = 2 )
lines(scoregroepen, s_toets_conf_low, lty = 2 )
lines(scoregroepen, s_toets_conf_high, lty = 2 )
Try this which is close to what you expect. I have re arranged your variables in a dataframe to reshape them and then sketch the plot. Here the code:
library(ggplot2)
library(dplyr)
library(tidyr)
# Creating some data first
scoregroepen <- seq(from = 1, to = 8, by = 1)
s_toets_observed <- c(0.18, 0.31, 0.42, 0.53, 0.64,0.75,0.84,0.95)
s_toets_predicted <- c(0.20, 0.29, 0.40, 0.55, 0.66, 0.75, 0.85, 0.94)
s_toets_conf_low <- s_toets_observed-0.03
s_toets_conf_high <- s_toets_observed+0.045
df <- data.frame(scoregroepen,s_toets_observed,s_toets_predicted,
s_toets_conf_low,s_toets_conf_high)
#Plot
df %>% pivot_longer(-scoregroepen) %>%
ggplot(aes(x=scoregroepen,y=value,color=name,linetype=name))+
geom_line()+
geom_point(aes(shape=name))+
scale_color_manual(values=c('blue','blue','tomato','cyan3'),
breaks=c('s_toets_observed','s_toets_predicted'),
labels=c('Observed','Predicted'))+
scale_shape_manual(values=c(NA,NA,1,4),
breaks=c('s_toets_observed','s_toets_predicted'),
labels=c('Observed','Predicted'))+
scale_linetype_manual(values=c('dotted','dotted','dashed','solid'),
breaks=c('s_toets_observed','s_toets_predicted'),
labels=c('Observed','Predicted'))+
labs(color='var',shape='var',linetype='var')
Output:

The error "Breaks and labels are different lengths" appears when using grid.arrange

In the following script:
dataset_prices2017 <- read.csv("/home/nasser/Desktop/prices2017.csv")
dataset_prices2018 <- read.csv("/home/nasser/Desktop/prices2018.csv")
dataset_prices2019 <- read.csv("/home/nasser/Desktop/prices2019.csv")
selectedDF <- dataset_prices2017 %>% select(Price, Volume)
plot_17 <- ggplot(selectedDF, aes(Price, Volume)) + geom_hex(bins = 30) + theme_bw() +
geom_smooth(method="lm") + labs(fill = "Count")
plot_17 <- plot_17 + scale_fill_gradientn(colors = brewer.pal(3,"Dark2"), labels = percent(0.25*0:4))
selectedDF <- dataset_prices2018 %>% select(Price, Volume)
plot_18 <- ggplot(selectedDF, aes(Price, Volume)) + geom_hex(bins = 30) + theme_bw() +
geom_smooth(method="lm") + labs(fill = "Count")
plot_18 <- plot_18 + scale_fill_gradientn(colors = brewer.pal(3,"Dark2"), labels = percent(0.25*0:4))
selectedDF <- dataset_prices2019 %>% select(Price, Volume)
plot_19 <- ggplot(selectedDF, aes(Price, Volume)) + geom_hex(bins = 30) + theme_bw() +
geom_smooth(method="lm") + labs(fill = "Count")
plot_19 <- plot_19 + scale_fill_gradientn(colors = brewer.pal(3,"Dark2"), labels = percent(0.25*0:4))
fullPlot <- grid.arrange(plot_17, plot_18, plot_19, ncol=3, nrow = 1)
Running this script generates the following error:
Error in f(..., self = self) : Breaks and labels are different lengths
Note that I got this error after adding the last line in the script.
Do you have any idea how to solve this error?
Just figured out that the problem is caused by the data of plot_18. The data for that plot:
structure(list(Price = c(0.5219551282, 0, 0, 0.194543297766667,
0.46875, 0.8108108108, 0.2962962963, 0.436213991763333, 0, 0.1904761905,
0.0208333333, 0, 0, 0.630769230766667, 0.0074626866, 0, 0.7037037037,
0.70736842106, 0.597250859103333, 0.88, 0.6666666667, 0, 0.13523391815,
0.035490830643333, 0.02705627708, 0.0666666667, 0.29674796745,
0.41944444446, 0.1621621622, 0.09127725854, 0.9234567901, 0.582170542623333,
0.28962962964, 0.79312169317, 0.793380614656667, 0.875187969906667,
0.03595113442, 0.113242009126667, 0.0293040293, 0.381194511696667,
0.0344827586, 0.09544715448, 0.161734693876667, 0.123095238113333,
0.7075949367, 0, 0.8125, 0.8571428571, 0.9, 0.9333333333, 0.79920634921,
0.0085106383, 0.713333333323333, 0.62150537635, 0.232905982933333,
0.868, 0.851580459763333, 0.743589743583333, 0.744298245626667,
0, 0.434379671133333, 0.797916666666667, 0.2222222222, 0.612962962953333,
0.591333333333333, 0.516666666666667, 0.53809523808, 0.611515151526667,
0.64246575339, 0.7101449275, 0.122445561153333, 0.65848765432,
0.2558526441, 0.641025641, 0.347892720293333, 0.0769230769, 0.353939393936667,
0.978333333333333, 0.755238095256667, 0.863333333333333, 0.6751243781,
0.67297297298, 0.5344017094, 0.0057142857, 0.921568627463333,
0.52644927536, 0.128333333333333, 0, 0.1904761905, 0.471929824536667,
0.15972222225, 0, 0, 0, 0.446258503403333, 0.44210526318, 0.82189054726,
0.628169014093333, 0, 0.2307692308, 0.191304347823333, 0.4004385965,
0.83441734419, 0.20449172577, 0.746598639443333, 0.211494252846667,
0.8695652174, 0.519999999993333, 0.113333333333333, 0.637142857133333,
0.699420289853333, 0.7391304348, 0.457575757543333, 0.461904761866667,
0.788888888883333, 0.503065134103333, 0.499616858236667, 0.793888888883333,
0.70901542112, 0.591358024693333, 0.47769028871, 0.456692913383333,
0.40625, 0, 0.7678571429, 0.796969696963333, 0.914886731406667,
0.102424242446667, 0.487804878, 0.1923076923, 0, 0.3658536585,
0.634782608673333, 0.385028248613333, 0.69864864864, 0.60404040406,
0.814880952386667, 0.533582089543333, 0.8695652174, 0, 0.715942029016667,
0.74987277353, 0.864455782326667, 0.624999999986667, 0.3333333333,
0.4642857143, 0.10434782606, 0.078095238096667, 0.0119047619,
0.427472527466667, 0.0909090909, 0.6071428571, 0.6071428571,
0.064516129, 0.500266666666667, 0.5263157895, 0, 0.75256410254,
0.691836734676667, 0.2083333333, 0.5, 0.85625, 0.6666666667,
0.60065359476, 0.5, 0.279012345676667, 0.1176470588, 0.5185185185,
0.8085106383, 0.5365853659, 0.9090909091, 0.82589928058, 0, 0,
0.03175438598, 0.511764705856667, 0.0196078431, 0.693650793643333,
0.18378378382, 0.10596205961, 0.022916666666667, 0.288888888903333,
0.306666666673333, 0, 0.68078703703, 0.744217687073333, 0.6428571429,
0.828462709286667, 0.887265917566667, 0.713710691823333, 0.76077643908,
0.72816091955, 0.648370927326667, 0.76565656568, 0.315458937203333,
0.29583333335, 0.714765100693333, 0.812159329126667, 0.05, 0.713681592026667,
0.10976645433, 0.75625, 0, 0.42777777777, 0.6842105263, 0.79678362574,
0.71025641027, 0.88205128204, 0.84959349595, 0.650104821806667,
0.9032258065, 0.895652173936667, 0.374390243886667, 0.531182795713333,
0.8429378531, 0.08, 0.030075188, 0, 0.0134228188, 0.93787878788,
0.7804878049, 0.556666666676667, 0.506630824373333, 0, 0.368527918786667,
0.7439716312, 0.21818181816, 0.7647058824, 0.90188034188, 0.792857142846667,
0.157142857183333, 0.19814814816, 0.799047619056667, 0.8947368421,
0, 0.318562091513333, 0.40884353743, 0.085815602833333, 0.14280701756,
0.804624277473333, 0.5714285714, 0.867759562826667, 0.624074074063333,
0.24139784949, 0.42, 0.716269841283333, 0.692539109493333, 0.398290598286667,
0.474193548386667, 0.5384615385, 0.225925925923333, 0.1666666667,
0.156540084396667, 0.6666666667, 0.82459546925, 0.683681592026667,
0.70945945945, 0.677494692146667, 0.65738396624, 0.83459119496,
0.794179894193333, 0.533630952366667, 0.639999999996667, 0.620648967546667,
0.301428571416667, 0.10307017545, 0.72935323382, 0.43850129199,
0.43074712644, 0.432835820883333, 0.1666666667, 0.668965517236667,
0.062745098026667, 0.625, 0.0425531915, 0.617567567546667, 0.069005847956667,
0.081836327346667, 0.6956521739, 0.9818181818, 0.5, 0.5, 0.4324324324,
0.2666666667, 0.2857142857, 0, 0.4027777778, 0.71195402298, 0.64342105263,
0.7741935484, 0.186419753086667, 0.822710622716667, 0, 0, 0.021875,
0.091484184913333, 0, 0, 0, 0, 0.6666666667, 0.737435897473333,
0.759420289843333, 0.6666666667, 0.295928753186667, 0.75420054203,
0.039602925803333, 0.6666666667, 0.183333333326667, 0.74521072797,
0.43983739835, 0.653645833333333, 0, 0.1935483871, 0.22883895131,
0.72955465589, 0.380459770086667, 0.844061302676667, 0.952873563223333,
0.3325), Volume = c(0.002414102564097, 0.001174398625467,
0.0010256410256, 0.001051838671393, 0.002878125, 0.004077477477493,
0.00177037037037, 0.003053909465023, 0.00206551724138, 0.002257142857167,
0.002033333333333, 0.001, 0.001072380952367, 0.0029717948718,
0.002232587064677, 0.00201036414567, 0.0021, 0.00292771929824,
0.003005841924393, 0.008772666666667, 0.002482539682533, 0.0016028328612,
0.001884795321637, 0.001899568500543, 0.001675757575777, 0.0012,
0.002775880758787, 0.002668333333327, 0.00151171171169, 0.001388785046723,
0.00607530864197, 0.00415348837209, 0.002834814814827, 0.008986243386227,
0.0020964539007, 0.005417543859647, 0.00207905759162, 0.002877168949767,
0.001929792429827, 0.003026553672313, 0.00150344827588, 0.001880108401087,
0.001844047619057, 0.002190952380963, 0.00348396624472, 0.0010344827586,
0.006220833333333, 0.00308730158726, 0.002998333333333, 0.00227777777778,
0.002014484126987, 0.00126765957446, 0.003718888888883, 0.005132616487453,
0.002628205128203, 0.004248, 0.007026293103447, 0.002628205128207,
0.003443421052633, 0.001, 0.002645739910313, 0.008983333333333,
0.0017777777778, 0.002870370370367, 0.003363333333333, 0.00210256410256,
0.003542857142857, 0.002534848484847, 0.00231506849313, 0.002258454106293,
0.001163484087093, 0.005761111111113, 0.00211039809863, 0.002683760683757,
0.016383908045983, 0.002228205128207, 0.00215212121213, 0.004805,
0.010981904761907, 0.00828875, 0.00274427860695, 0.002558108108107,
0.002019444444443, 0.0015914285714, 0.004362745098053, 0.002043840579703,
0.002366666666667, 0.001103703703693, 0.001742857142873, 0.004219298245603,
0.00166712962963, 0.001, 0.0013430107527, 0.0015434782609, 0.002538775510207,
0.003559899749367, 0.0025592039801, 0.00343615023474, 0.001239955849933,
0.002176923076903, 0.001110144927567, 0.00341403508773, 0.003459349593493,
0.002460283687937, 0.00319863945578, 0.00226666666666, 0.018426086956527,
0.003018888888873, 0.002331666666667, 0.003414285714283, 0.002526956521737,
0.00519710144927, 0.002732034632027, 0.002861904761897, 0.003106666666673,
0.002896934865907, 0.00257969348659, 0.003066111111117, 0.00349881376038,
0.00244814814815, 0.00230262467191, 0.002416272965877, 0.00248125,
0.001801666666667, 0.007639880952377, 0.002253030303033, 0.009329126213587,
0.00166909090911, 0.00293821138211, 0.004464102564097, 0.002315942029033,
0.003039024390237, 0.015501449275363, 0.008321468926553, 0.009915315315317,
0.004964646464643, 0.00526488095237, 0.004199004975113, 0.00949130434782,
0.001162745098033, 0.00265942028985, 0.002461323155213, 0.013436054421777,
0.003893253968247, 0.001716666666667, 0.00195595238097, 0.0019130434783,
0.001076190476233, 0.001866269841277, 0.003128937728933, 0.0021,
0.00230238095237, 0.00248333333332, 0.00281397849465, 0.002320533333333,
0.002299122807017, 0.002, 0.00275, 0.002816326530617, 0.002254166666667,
0.00369761904762, 0.00381875, 0.003125925925927, 0.005600000000007,
0.00369861111111, 0.002507407407373, 0.0014117647059, 0.003214814814817,
0.002590070921973, 0.00297479674797, 0.01804242424242, 0.00463980815348,
0.001, 0.0010222222222, 0.001853947368427, 0.0017058823529, 0.0011568627451,
0.003668253968263, 0.00215675675675, 0.001922222222237, 0.002383333333333,
0.00296495726495, 0.04700512820514, 0.001080952380933, 0.007685648148143,
0.0026112244898, 0.014720238095237, 0.00248949771689, 0.00493520599251,
0.00640553459119, 0.004878313253013, 0.00365545977011, 0.004275689223057,
0.01131245791246, 0.003004106280193, 0.00678472222223, 0.005784004474273,
0.004531446540867, 0.001835, 0.00332611940299, 0.001566454352443,
0.01443125, 0.001049275362333, 0.007142424242427, 0.0240552631579,
0.007115204678353, 0.003953846153853, 0.00365705128204, 0.005986720867213,
0.002818448637317, 0.00557311827956, 0.00247934782609, 0.002582926829263,
0.003732258064523, 0.00645706214689, 0.004855999999993, 0.001249122807027,
0.0010241545894, 0.00103154362418, 0.013604545454543, 0.005505691056917,
0.002934444444437, 0.002545340501793, 0.0010238095238, 0.00221370558375,
0.00335000000001, 0.001788552188527, 0.00236274509804, 0.013679487179487,
0.005909523809527, 0.003596190476187, 0.002676851851847, 0.00611942857143,
0.005988596491227, 0.002, 0.002481045751637, 0.00392789115644,
0.00167568389058, 0.002203684210527, 0.003684778420043, 0.0097238095238,
0.00281311475409, 0.003364074074077, 0.002609677419357, 0.002558,
0.002856746031737, 0.00735330926595, 0.001780128205137, 0.00261075268818,
0.002848717948717, 0.00189058641975, 0.001945555555547, 0.001983966244727,
0.005273333333327, 0.004203883495147, 0.003079104477617, 0.003092342342337,
0.003043312101907, 0.00304556962024, 0.003135220125783, 0.004403703703707,
0.002158333333327, 0.004803902439023, 0.00234247787612, 0.0019919047619,
0.001954385964913, 0.003713432835827, 0.002959173126613, 0.00378965517242,
0.00259552238807, 0.002186111111113, 0.001156704980857, 0.002354901960793,
0.01525625, 0.0015957446809, 0.00489594594594, 0.001830409356733,
0.00169753825682, 0.00253671497585, 0.01430363636364, 0.0025,
0.016829166666667, 0.002627027027023, 0.002300000000033, 0.0022857142857,
0.002, 0.001732870370397, 0.002788850574713, 0.00302105263158,
0.008280645161293, 0.002154938271587, 0.003345421245423, 0.001345662100463,
0.001486666666667, 0.00210078125, 0.002824574209243, 0.001466666666667,
0.002, 0.002, 0.001147619047633, 0.00367654320988, 0.00385538461539,
0.003284541062797, 0.00693765432099, 0.001989058524173, 0.004204607046073,
0.002009299895507, 0.004667777777773, 0.00320292397661, 0.003397318007663,
0.00231788617884, 0.005576388888893, 0.001764102564133, 0.002895698924767,
0.001935205992517, 0.002502699055333, 0.002919540229867, 0.00350996168583,
0.003030459770113, 0.002411666666667)), .Names = c("Price",
"Volume"), class = "data.frame", row.names = c(NA, -320L
))
When I remove the labels = percent(0.25*0:4), the error is gone. However, I added this in order to show the legend labels as 25%, 50%, 75%, 100%. What should I do to solve this issue? It's important for me to show the legend labels as I mentioned.
I am guessing what your labels indicate are the quantiles of counts. So first let's get back what your original plot is without percentage labels:
library(scales)
library(ggplot2)
library(hexbin)
library(RColorBrewer)
plot_17 <- ggplot(selectedDF, aes(Price, Volume)) +
geom_hex(bins = 30) + theme_bw() +
geom_smooth(method="lm") + labs(fill = "Count")
plot_17_count <- plot_17 +
scale_fill_gradientn(colors = brewer.pal(3,"Dark2"))
The problem with defining a 0,0.25.. labels for every plot is, you might have different number of breaks depending on the range. You can see for the plot above (plot_17_count), it has only 3 breaks. For you to place the percentiles correctly, you need to access the hexbin count inside geom_hex.. which might not be so easy. One workaround is to transform the scale.
We define the quantiles you need, and a transformation
BR = percent(0.25*0:4)
# unfortunately no inverse, I just put in identity so the function works
min_max_trans = trans_new("min_max",function(i)i/(max(i)-min(i)),inverse=identity)
plot_17_perc <- plot_17 +
scale_fill_gradientn(colors = brewer.pal(3,"Dark2"),
trans=min_max_trans,labels=percent(0.25*0:4))
Put them together:
library(gridExtra)
grid.arrange(plot_17_count,plot_17_perc)

Remove white space between two plots resulting from grid.arrange function in R

I'd like to eliminate the white space between my two forest plots that I plotted side-by-side using grid.arrange().
Before you vote down or redirect - Before asking this question, I have spent hours attempting every solution posed in each of the responses I've seen here for similar questions without achieving my desired result.
First, here is my dataset and code:
library(meta)
library(grid)
library(gridExtra)
df <- structure(list(study = 1:7,
sens = c(0.88, 0.86, 0.75, 0.9, 0.91, 0.93, 0.98),
sens.se = c(0.13, 0.08, 0.2, 0.06, 0.13, 0.15, 0.66),
sens2 = c(0.76, 0.68, 0.9, 0.82, 0.76, 0.85, 0.76),
sens.se2 = c(0.14, 0.08, 0.2, 0.06, 0.14, 0.15, 0.66)),
class = "data.frame",
row.names = c(NA, -7L))
## setting up meta-analysis model using library(meta)
res1 <- metagen(TE=sens, seTE=sens.se, data=df, studlab=study)
res2 <- metagen(TE=sens2, seTE=sens.se2, data=df, studlab=study)
## changing plots to grid graphical objects to use grid.arrange
fp1 <- grid.grabExpr(forest(res1, data=df, method.tau="REML",
comb.random=TRUE, leftcols="studlab",
rightcols=c("effect", "ci")))
fp2 <- grid.grabExpr(forest(res2, data=df, method.tau="REML",
comb.random=TRUE, leftcols="studlab",
rightcols=c("effect", "ci")))
## arranging plots side by side:
grid.arrange(fp1, fp2, ncol = 2)
When I have attempted to use code suggested in responses to similar questions, I get the "only grobs allowed in gList" error code, even though R recognizes the plots as "gTrees" because I used the grid.grabExpr function. I've tried coercing the gTrees into grobs via:
p1 <- as.grob(fp1)
p2 <- as.grob(fp2)
, which only creates null values in the global environment.
I would greatly appreciate some help with this!
Perhaps this does what you are looking for:
grid.grabExpr(
forest(
res1, data=df, method.tau="REML",
comb.random=TRUE, leftcols="studlab",
rightcols=c("effect", "ci")
),
height = 1, width = 2
) -> fp1
grid.grabExpr(
forest(
res2, data=df, method.tau="REML",
comb.random=TRUE, leftcols="studlab",
rightcols=c("effect", "ci")
),
height = 1, width = 2
) -> fp2
grid.arrange(fp1, fp2, ncol = 2, vp=viewport(width=1, height=1, clip = TRUE))

Create A Stat_Density_2D Plot By Binning Fill Variable

I have been trying to create a density plot in R that looks similar to the picture below.
In my code below, I have created a stat_density_2D plot that successfully plots my data, however, it fails to recognize my fill variable (in this case exitspeed) and only plots one color.
Upon further research, I believe the reason for this is because stat_density_2d bins the fill into levels. The problem I am having is that my fill variable has multiple values for the points within a particular level ultimately resulting in a density plot that only displays one color. Does anyone know how to bin my data so that my density plot can recognize the fill variable (exitspeed)? Please see below for the dataset and R code. Thanks in advance!
Data:
structure(list(platelocheight = c(2.594, 3.803, 3.254, 3.599,
3.617, 3.297, 2.093, 3.611, 2.842, 3.316, 2.872, 3.228, 3.633,
4.28, 3.309, 2.8, 2.632, 3.754, 2.207, 3.604, 3.443, 2.188, 3.452,
2.553, 3.382, 3.067, 2.986, 2.785, 2.567, 3.804), platelocside = c(0.059,
-1.596, -0.65, -0.782, -0.301, -0.104, 0.057, -0.807, 0.003,
1.661, 0.088, -0.32, -1.115, -0.146, -0.364, -0.952, 0.254, 0.109,
-0.671, -0.803, -0.212, -0.069, -0.09, -0.472, 0.434, 0.337,
0.723, 0.508, -0.197, -0.635), exitspeed = c(69.891, 73.352,
83.942, 85.67, 79.454, 85.277, 81.078, 73.573, 77.272, 59.263,
97.343, 91.436, 76.264, 83.479, 47.576, 84.13, 60.475, 61.093,
84.54, 69.959, 88.729, 88.019, 82.18, 83.684, 86.296, 90.605,
79.945, 59.899, 62.522, 77.75)), .Names = c("platelocheight",
"platelocside", "exitspeed"), row.names = c(NA, 30L), class = "data.frame")
R-Code:
library(RODBC)
library(ggplot2)
con=odbcConnect('username',uid='userid', pwd = 'password')
df=sqlQuery(con,"select platelocheight, platelocside, exitspeed from tm_sample where pitchcall='InPlay'
and exitspeed is not null")
topKzone <- 3.5
botKzone <- 1.6
inKzone <- -0.95
outKzone <- 0.95
kZone <- data.frame(
x=c(inKzone, inKzone, outKzone, outKzone, inKzone),
y=c(botKzone, topKzone, topKzone, botKzone, botKzone)
)
df$h <- round(df$platelocheight)
df$s <- round(df$platelocside)
df$es<- round(df$exitspeed)
ggplot(kZone, aes(x,y)) +
stat_density_2d(data=df, aes(x=s, y=h, fill=es),geom="polygon") +
scale_fill_distiller(palette = "Spectral") +
geom_path(lwd=1.5, col="black") +
coord_fixed()

Using log2 transfer with stat_summary

I am new to R and trying to draw a boxplot on two factors. I am able to draw a graph according to my requirement. Here is the code:
library(ggplot2)
Systems <- c(rep("A", 23), rep("B", 328), rep("C", 85), rep("D", 25))
treatment <- rep(c("MEAN\n0.035 0.252 0.005 0.032", "MEAN\n0.030 0.213 0.008 0.033"), each = 461)
Performance <- c(
0.041817315, 0.012366105, 0.008223291, 0.101194094, 0.032095414, 0.022424856, 0.004272651, 0.02568757, 0.012011461, 0.032519093, 0.015862062, 0.027747871, 0.024599762, 0.003786862, 0.28017758, 0.017898477, 0.00646475, 0.004093185, 0.000740573, 0.039312335, 0.002415459, 0.051774672, 0.029394015, 0.303119908, 0.247602112, 0.234497662, 0.240247293, 0.256006375, 0.241818886, 0.241818886, 0.23355839, 0.23355401, 0.24791564, 0.232738515, 0.247145881, 0.234726096, 0.233802269, 0.296144867, 0.318978078, 0.309485102, 0.24498581, 0.361880846, 0.233798292, 0.233802269, 0.23446443, 0.23446443, 0.245941147, 0.251454016, 0.256166292, 0.269703778, 0.25459034, 0.255547875, 0.293828554, 0.269443031, 0.245104749, 0.278098811, 0.304266148, 0.238588328, 0.304754431, 0.251716996, 0.260886932, 0.260633422, 0.247319476, 0.24162866, 0.268908914, 0.23960451, 0.255093289, 0.268908914, 0.245971624, 0.26665091, 0.279822691, 0.268784457, 0.298624598, 0.241807867, 0.232050361, 0.241527075, 0.27750843, 0.26665091, 0.2317304, 0.234567279, 0.232487121, 0.233076363, 0.242322263, 0.241818886, 0.241818886, 0.235521394, 0.261765062, 0.270101105, 0.242477399, 0.232661245, 0.283295967, 0.247602112, 0.263427876, 0.239485768, 0.237005999, 0.243690789, 0.247371437, 0.237005999, 0.247231137, 0.249111033, 0.26665091, 0.254097987, 0.273745634, 0.290167407, 0.245623389, 0.241930972, 0.231602752, 0.234514088, 0.256039044, 0.265463913, 0.256166292, 0.247743609, 0.232694788, 0.237961675, 0.281536336, 0.24977717, 0.262282857, 0.24543148, 0.247155404, 0.247155404, 0.26129785, 0.234514088, 0.26494978, 0.258628847, 0.257580217, 0.253061784, 0.232630126, 0.247155404, 0.244319159, 0.262394668, 0.26665091, 0.3165875, 0.268706383, 0.250278577, 0.253011248, 0.249419117, 0.245104749, 0.235595377, 0.238554589, 0.273267375, 0.248984622, 0.24520136, 0.258955737, 0.2317304, 0.272465904, 0.23870662, 0.248026932, 0.263427876, 0.278497558, 0.265828268, 0.234826045, 0.2317304, 0.232172596, 0.235692813, 0.232737079, 0.297579932, 0.286422023, 0.259325175, 0.250639446, 0.231587836, 0.300662192, 0.248210353, 0.294150342, 0.251203475, 0.251417668, 0.295233578, 0.256006375, 0.256006375, 0.245015073, 0.249356914, 0.293528269, 0.281821725, 0.248799266, 0.301499956, 0.247721985, 0.290792643, 0.328679821, 0.330321343, 0.30640335, 0.276763556, 0.247496304, 0.237458207, 0.237458207, 0.237516949, 0.233802269, 0.234468703, 0.239264575, 0.234887381, 0.23446443, 0.234887381, 0.234225205, 0.237004685, 0.23455164, 0.23446443, 0.234870008, 0.232694788, 0.234870008, 0.234860348, 0.270044052, 0.232694788, 0.269666687, 0.236614177, 0.280480722, 0.243978724, 0.249111033, 0.279925672, 0.232172596, 0.239249363, 0.272197191, 0.246987548, 0.244882001, 0.269280706, 0.249566945, 0.241863369, 0.239677242, 0.242426616, 0.253140755, 0.239869221, 0.239677242, 0.23171238, 0.275635746, 0.231602752, 0.286266232, 0.259265885, 0.239869221, 0.236614177, 0.255256967, 0.257580217, 0.290128013, 0.274832429, 0.243149974, 0.232123048, 0.235250227, 0.276830805, 0.231188689, 0.247743609, 0.245963204, 0.259455641, 0.32522266, 0.252770086, 0.236461595, 0.244470967, 0.244024628, 0.24498581, 0.2317304, 0.233464424, 0.266062106, 0.233118162, 0.243588113, 0.232694788, 0.243435021, 0.249775037, 0.240018899, 0.26665091, 0.26665091, 0.268784457, 0.237497376,
0.237483089, 0.237145874, 0.237145874, 0.2317304, 0.243435021, 0.269419291, 0.243767405, 0.274193607, 0.243435021, 0.237281364, 0.237205977, 0.237145874, 0.237145874, 0.245029635, 0.242611375, 0.237961675, 0.266369762, 0.266369762, 0.245760558, 0.266369762, 0.249121027, 0.291800661, 0.240954111, 0.238391917, 0.235264993, 0.233355127, 0.234898357, 0.235244609, 0.238499482, 0.234215035, 0.236608826, 0.235265733, 0.25019165, 0.283415889, 0.237145874, 0.267352731, 0.237422834, 0.273147585, 0.225775832, 0.238321926, 0.235125859, 0.232694788, 0.249626478, 0.234218629, 0.235269307, 0.26533449, 0.242207321, 0.24183494, 0.241848474, 0.256006375, 0.24183494, 0.241848474, 0.2734133, 0.241848474, 0.241848474, 0.24183494, 0.23422781, 0.248608779, 0.235125859, 0.234871225, 0.235125859, 0.233774363, 0.235278439, 0.245684423, 0.234705022, 0.245684423, 0.235186554, 0.245684423, 0.23470016, 0.236614177, 0.245953538, 0.245953538, 0.245922432, 0.249522459, 0.276680994, 0.251743401, 0.235125859, 0.222236695, 0.251487861, 0.286975357, 0.240545386, 0.245488295, 0.233464424, 0.270731686, 0.241818886, 0.285677834, 0.24543148, 0.237961675, 0.262911409, 0.258234963, 0.24326508, 0.236608826, 0.237516949, 0.26533449, 0.003208185, 0.003075313, 0.003075313, 0.051965266, 0.086809801, 0.002074263, 0.003814715, 0.00241056, 0.004519913, 0.002354925, 0.003668154, 0.002411354, 0.001046015, 0.00641338, 0.002344457, 0.00090009, 0.010972388, 0.001492537, 0.00084317, 0.005872887, 0.003912383, 0.001643901, 0.001492537, 0.001844077, 0.00084317, 0.005379174, 0.001821256, 0.002505201, 0.001051525, 0.00084317, 0.002505201, 0.004837974, 0.00084317, 0.002488134, 0.005078271, 0.00084317, 0.002488134, 0.00250647, 0.00250647, 0.002480833, 0.002480833, 0.001101322, 0.010244482, 0.001321974, 0.012283591, 0.001733435, 0.001512552, 0.001671509, 0.001428863, 0.002618663, 0.005121464, 0.002808259, 0.002503417, 0.002558745, 0.00216939, 0.002021331, 0.001588465, 0.001690088, 0.003279291, 0.002295707, 0.003248547, 0.021351276, 0.002499903, 0.002503417, 0.001101322, 0.00354847, 0.002499903, 0.003075313, 0.00567605, 0.01320935, 0.001052632, 0.004384033, 0.001918676, 0.001597092, 0.015212658, 0.001046015, 0.006676165, 0.006883556, 0.002696254, 0.003247508, 0.002709837, 0.002234992, 0.011494253, 0.001311413, 0.014849595, 0.012022537, 0.006497906, 0.025349246, 0.020579744, 0.049220779, 0.018911298, 0.005464481, 0.023748064, 0.045756258, 0.010638298, 0.019893403, 0.023962608, 0.05465644, 0.028966333, 0.004459272, 0.105586592, 0.075615135, 0.022154774, 0.017105029, 0.005208333, 0.011363636, 0.008951424, 0.117416075, 0.007916667, 0.078388988, 0.030861112, 0.003519782, 0.005422482, 0.125796873, 0.005031371, 0.03362749, 0.000778961, 0.016681028, 0.004599127, 0.000454307, 0.001505359, 0.031611448, 0.011308386, 0.002134792, 0.286294212, 0.018161072, 0.006453087, 0.003571249, 0.000447235, 0.042566651, 7.77303E-05, 0.042401148, 0.023784287, 0.245226355, 0.207228787, 0.204169525, 0.211077481, 0.213258479, 0.207484024, 0.207484024, 0.201602486, 0.202638497, 0.216700318, 0.201214614, 0.220748161, 0.202433079, 0.201738383, 0.248574743, 0.262874666,
0.252606723, 0.200504039, 0.312541501, 0.202773831, 0.201738383, 0.2030333, 0.2030333, 0.203064156, 0.209310089, 0.217450641, 0.231772732, 0.209757897, 0.217977319, 0.247090075, 0.21844954, 0.209559111, 0.225575484, 0.254032564, 0.201524647, 0.24778379, 0.217517211, 0.222981128, 0.227145657, 0.210758647, 0.208297414, 0.216762142, 0.203586766, 0.216700206, 0.216762142, 0.207592071, 0.213666768, 0.227338434, 0.215879786, 0.246166402, 0.20561442, 0.205026788, 0.207675824, 0.227655498, 0.213666768, 0.197325754, 0.210254948, 0.206766076, 0.207358384, 0.218678788, 0.207484024, 0.207484024, 0.204014221, 0.218887554, 0.225205822, 0.204384584, 0.198406675, 0.227971421, 0.207228787, 0.214348845, 0.201777676, 0.205916468, 0.213450757, 0.209587709, 0.205916468, 0.209362242, 0.20856388, 0.213666768, 0.216186368, 0.234453313, 0.248382745, 0.21701993, 0.213600036, 0.201106069, 0.203802471, 0.216655494, 0.225560499, 0.217450641, 0.208587224, 0.197681539, 0.199276425, 0.232171602, 0.210470295, 0.22175041, 0.204355918, 0.204475181, 0.204475181, 0.231548299, 0.203802471, 0.223041945, 0.217862851, 0.230785952, 0.213283262, 0.200257172, 0.204475181, 0.202897929, 0.211315524, 0.213666768, 0.263361407, 0.225185308, 0.214271007, 0.210497889, 0.212978687, 0.209559111, 0.199014939, 0.20945405, 0.224553068, 0.217749634, 0.216496692, 0.212296213, 0.197325754, 0.221826528, 0.208655636, 0.20192612, 0.214348845, 0.223101747, 0.23194135, 0.198683705, 0.197325754, 0.198089791, 0.20427291, 0.199290161, 0.24070988, 0.249032587, 0.218488173, 0.211126674, 0.197780282, 0.244029201, 0.208767987, 0.240834568, 0.209095817, 0.20977662, 0.239262653, 0.213258479, 0.213258479, 0.209840866, 0.210998294, 0.251844378, 0.22782243, 0.209390413, 0.242168553, 0.207556718, 0.230220324, 0.277619451, 0.271500416, 0.2463793, 0.236268228, 0.204252778, 0.205975522, 0.205975522, 0.20638888, 0.201738383, 0.202307262, 0.203126957, 0.203547468, 0.2030333, 0.203547468, 0.202251975, 0.206942541, 0.202009011, 0.2030333, 0.199306658, 0.197681539, 0.199306658, 0.200035605, 0.219731274, 0.197681539, 0.220112984, 0.201326042, 0.222447694, 0.204767129, 0.20856388, 0.238296852, 0.198089791, 0.20783083, 0.228566801, 0.213406582, 0.202128774, 0.223103043, 0.21174389, 0.209480203, 0.203141599, 0.213522888, 0.218265827, 0.203332504, 0.203141599, 0.200857909, 0.221504103, 0.201106069, 0.234932916, 0.224156158, 0.203332504, 0.201326042, 0.212033061, 0.230785952, 0.232169974, 0.22103837, 0.208472924, 0.201662359,
0.197770095, 0.227186459, 0.197226949, 0.208587224, 0.203044091, 0.21799408, 0.27616292, 0.206754881, 0.205386431, 0.20518108, 0.206706255, 0.200504039, 0.197325754, 0.201725618, 0.216929898, 0.198194632, 0.210965099, 0.197681539, 0.210099248, 0.205271974, 0.200638561, 0.213666768, 0.213666768, 0.215879786, 0.209006585, 0.208696114, 0.207817635, 0.207817635, 0.197325754, 0.210099248, 0.216982956, 0.212178741, 0.217721009, 0.210099248, 0.20711904, 0.208521315, 0.207817635, 0.207817635, 0.205501252, 0.202278423, 0.199276425, 0.213447242, 0.213447242, 0.205132583, 0.213447242, 0.214546613, 0.233837102, 0.20461375, 0.200041017, 0.203376022, 0.201666622, 0.203273295, 0.206239566, 0.206005147, 0.202367361, 0.205336165, 0.206159599, 0.212043546, 0.23652672, 0.207817635, 0.222323542, 0.207992955, 0.230179094, 0.191639063, 0.203652472, 0.205707127, 0.197681539, 0.210851357, 0.20206244, 0.205851609, 0.230072742, 0.203700261, 0.204112552, 0.203387156, 0.213258479, 0.204112552, 0.203387156, 0.226895739, 0.203387156, 0.203387156, 0.204112552, 0.202062597, 0.207156386, 0.205707127, 0.204103383, 0.205707127, 0.20231351, 0.205851767, 0.202278126, 0.203456946, 0.202278126, 0.206408028, 0.202278126, 0.202940706, 0.201326042, 0.205811807, 0.205811807, 0.202945571, 0.209642676, 0.236595043, 0.211389397, 0.205707127, 0.187165901, 0.221709686, 0.235580538, 0.205130035, 0.208843627, 0.201725618, 0.228112266, 0.207484024, 0.233405455, 0.204355918, 0.199276425, 0.2214728, 0.214289366, 0.207335809, 0.205336165, 0.20638888, 0.230072742, 0.011415222, 0.003524363, 0.003524363, 0.065250462, 0.099337796, 0.002467596, 0.005194761, 0.002749848, 0.00431973, 0.002470309, 0.00429052, 0.002528805, 0.000964973, 0.007359328, 0.002605198, 0.010989011, 0.012864128, 0.001187648, 0.003546099, 0.024859586, 0.004188306, 0.002222601, 0.001187648, 0.002754248, 0.003546099, 0.001364, 0.001148428, 0.002092947, 0.000649351, 0.003546099, 0.002092947, 0.002955342, 0.003546099, 0.012749845, 0.002594875, 0.003546099, 0.012749845, 0.002994309, 0.002994309, 0.021827371, 0.021827371, 0.001730104, 0.011329362, 0.001260936, 0.01263916, 0.001845543, 0.002035106, 0.003043491, 0.002160411, 0.001939528, 0.010275639, 0.003682458, 0.002439817, 0.012294413, 0.003980996, 0.028844157, 0.002571735, 0.005293216, 0.003654834, 0.002891218, 0.002046854, 0.037579121, 0.002432344, 0.002439817, 0.001730104, 0.006082265, 0.002432344, 0.003524363, 0.006134648, 0.009195465, 0.000657895, 0.005597993, 0.002345224, 0.001933761, 0.0150357, 0.000964973, 0.004387928, 0.00773195, 0.002727137, 0.006166549, 0.002631304, 0.002269814, 0.000711744, 0.001519004, 0.013924241, 0.016998883, 0.004487617, 0.009501123, 0.06314105, 0.00686984, 0.016491278, 0.005154639, 0.006493982, 0.028453674, 0.005319149, 0.165847332, 0.015483992, 0.151746052, 0.058287127, 0.004457018, 0.005368773, 0.037517336, 0.012809372, 0.012127533, 0.002079002, 0.003448276, 0.017034111, 0.117145251, 0.016396803, 0.092959905
)
data <- data.frame(Systems, treatment, Performance)
data$Systems <- factor(data$Systems, levels = c("A", "B", "C", "D"))
colors <- c(rep("#222222", 23), rep("#777777", 328), rep("#AAAAAA", 85), rep("#CCCCCC", 25), rep("#222222", 23), rep("#777777", 328), rep("#AAAAAA", 85), rep("#CCCCCC", 25))
scaleFUN <- function(x) sprintf("%.3f", x)
ggplot(data, aes(x = Systems, y = Performance, fill = treatment)) +
geom_boxplot(alpha = 1, aes(fill = factor(colors))) +
facet_wrap(~ treatment) +
stat_summary(
fun.y = mean, geom = "point", shape = 21, size = 5, color = "#FFFFFF", fill = "#222222"
) +
ylab("Average Precision") +
ggtitle(" TEC-Gensim TEC-Matlab") +
theme(
legend.position = "none",
panel.grid.major = element_line(colour = "#d3d3d3"),
panel.border = element_blank(),
panel.background = element_blank(),
plot.title = element_text(size = 14, face = "bold", hjust = 0.5)
) +
scale_fill_brewer(palette = "Greys") +
coord_cartesian(ylim = c(0.001, 0.3)) +
scale_y_continuous(breaks = c(0, 0.002, 0.004, 0.008, 0.016, 0.032, 0.064, 0.128, 0.256))
Such transformation making the display better but disturbing the mean value manifested through geom-point using stat_summary.
Maybe you are looking for the geom = "label" option inside stat_summary?
library(ggplot2)
Systems <- c(rep("A", 23), rep("B", 328), rep("C", 85), rep("D", 25))
treatment <- rep(c("TEC-Gensim", "TEC-Matlab"), each = 461)
Performance <- c(rnorm(n = 230, mean = 0.035, sd = 0.02),
rnorm(n = 231, mean = 0.252, sd = 0.01),
rnorm(n = 230, mean = 0.005, sd = 0.03),
rnorm(n = 231, mean = 0.031, sd = 0.01))
Performance <- c(runif(n = 230, max = 0.3),
runif(n = 231, max = 0.3),
runif(n = 230, max = 0.3),
runif(n = 231, max = 0.3))
data <- data.frame(Systems, treatment, Performance)
data$Systems <- factor(data$Systems, levels = c("A", "B", "C", "D"))
colors <- c(rep("#222222", 23), rep("#777777", 328), rep("#AAAAAA", 85), rep("#CCCCCC", 25), rep("#222222", 23), rep("#777777", 328), rep("#AAAAAA", 85), rep("#CCCCCC", 25))
scaleFUN <- function(x) sprintf("%.3f", x)
ggplot(data, aes(x = Systems, y = Performance, fill = treatment)) +
geom_boxplot(alpha = 1, aes(fill = factor(colors))) +
facet_wrap(~ treatment) +
stat_summary(fun.y = mean, geom = "point", shape = 21, size = 5,
color = "#FFFFFF", fill = "#222222") +
stat_summary(fun.y = mean, geom = "label", aes(label = round(..y.., 3)),
hjust = -0.3, fill = 'white') +
ylab("Average Precision") +
theme(
legend.position = "none",
panel.grid.major = element_line(colour = "#d3d3d3"),
panel.border = element_blank(),
panel.background = element_blank(),
plot.title = element_text(size = 14, face = "bold", hjust = 0.5)
) +
scale_fill_brewer(palette = "Greys") +
coord_cartesian(ylim = c(0.001, 0.3)) +
scale_y_continuous(breaks = c(0, 0.002, 0.004, 0.008, 0.016, 0.032, 0.064, 0.128, 0.256))
```

Resources