How can I plot this data in R? - r

I have 4 columns: date & time, stage_duration, various_stages, Vehicle_ID. I want to plot date and time in mins on X-axis and id, stage_duration on Y-axis and fill by various stages on line or bar chart.
Something like this would be good:
Here is my data:
var_events time_date event_duration veh_id
LD 17-06-2018 13:25 6.52 B33
WL 17-06-2018 13:25 14.52 B31
TL 17-06-2018 13:26 0.32 B32
TE 17-06-2018 13:26 4.58 B13
UL 17-06-2018 13:26 3.45 B12
WT 17-06-2018 13:26 5.46 B25
UL 17-06-2018 13:26 1.56 B17
TL 17-06-2018 13:26 13.6 B33
SL 17-06-2018 13:26 0.05 B32
Here is a minimal example that creates the plot
# load data
data(presidential)
data(economics)
# events of interest
events <- presidential[-(1:3),]
# strip year from economics and events data frames
economics$year = as.numeric(format(economics$date, format = "%Y"))
# use dplyr to summarise data by year
#install.packages("dplyr")
library(dplyr)
econonomics_mean <- economics %>%
group_by(year) %>%
summarise(mean_unemployment = mean(unemploy))
# add president terms to summarized data frame as a factor
president <- c(rep(NA,14), rep("Reagan", 8), rep("Bush", 4), rep("Clinton", 8), rep("Bush", 8), rep("Obama", 7))
econonomics_mean$president <- president
# create ggplot
p <- ggplot(data = econonomics_mean, aes(x = year, y = mean_unemployment)) +
geom_point(aes(color = president)) +
geom_line(alpha = 1/3)
Update
This is the output:
structure(list(Event_stage = c("SE", "MN", "MN", "TE", "TE",
"TE", "TE", "TE", "TE", "TE", "TE", "WL", "TE", "TE", "SE", "TE",
"TE", "WL", "WT", "MN", "WL", "TE", "WL", "WL", "WT", "WL", "LD",
"WT", "WL", "WT", "WT", "TE", "WL", "LD", "WT", "LD", "MN", "TL",
"TE", "WL", "TL", "TL", "WT", "TE", "TE", "LD", "WT", "TL", "LD"),
event_date = structure(c(1529573704, 1529573710, 1529573713,
1529573724, 1529573855, 1529573874, 1529573880, 1529573895, 1529573906,
1529573918, 1529573925, 1529573931, 1529573931, 1529573941, 1529573947,
1529573969, 1529574006, 1529574054, 1529574088, 1529574114, 1529574120,
1529574123, 1529574134, 1529574137, 1529574148, 1529574163, 1529574164,
1529574148, 1529574169, 1529574170, 1529574178, 1529574188, 1529574189,
1529574196, 1529574178, 1529574188, 1529574203, 1529574213, 1529574214,
1529574214, 1529574215, 1529574227, 1529574231, 1529574242, 1529574244,
1529574245, 1529574248, 1529574260, 1529574262), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), stage_duration = c(3.78, 3.47, 2.78,
3.45, 3.32, 4.93, 4.23, 4.22, 3.85, 3.37, 5.88, 5.92, 3.97, 3.7,
NA, 4.08, 3.05, 0.57, 11.18, 12.08, 2.6, 3.3, 0.23, 0.85, 0.27,
0.25, 0.82, 10.42, 0.15, 0.43, 1.4, 0.25, 0.7, 0.52, 1.12, 0.45,
12.87, 12.18, 2.92, 0.57, 14.07, 12.72, 17.12, 4.13, 3.13, 0.25,
0.33, 18.98, 1.05), veh_id = c("B35", "B05", "B04", "B08", "B14",
"B13", "B04", "B17", "B41", "B05", "B26", "B08", "B35", "B19a",
"B10a", "B01a", "B28", "B14", "B14", "B18", "B05", "B37", "B04",
"B41", "B04", "B19a", "B04", "B17", "B35", "B13", "B35", "B02b",
"B28", "B13", "B19a", "B41", "B02b", "B04", "B15", "B01a", "B41",
"B13", "B28", "B27", "B33", "B19a", "B01a", "B19a", "B35")),
.Names = c("Event_stage", "event_date", "stage_duration", "veh_id"),
row.names = c(NA, -49L), class = c("tbl_df", "tbl", "data.frame"))

require(ggplot2)
require(dplyr)
df = structure(list(Event_stage = c("SE", "MN", "MN", "TE", "TE", "TE", "TE", "TE", "TE", "TE", "TE", "WL", "TE", "TE", "SE", "TE", "TE", "WL", "WT", "MN", "WL", "TE", "WL", "WL", "WT", "WL", "LD", "WT", "WL", "WT", "WT", "TE", "WL", "LD", "WT", "LD", "MN", "TL", "TE", "WL", "TL", "TL", "WT", "TE", "TE", "LD", "WT", "TL", "LD" ), event_date = structure(c(1529573704, 1529573710, 1529573713, 1529573724, 1529573855, 1529573874, 1529573880, 1529573895, 1529573906, 1529573918, 1529573925, 1529573931, 1529573931, 1529573941, 1529573947, 1529573969, 1529574006, 1529574054, 1529574088, 1529574114, 1529574120, 1529574123, 1529574134, 1529574137, 1529574148, 1529574163, 1529574164, 1529574148, 1529574169, 1529574170, 1529574178, 1529574188, 1529574189, 1529574196, 1529574178, 1529574188, 1529574203, 1529574213, 1529574214, 1529574214, 1529574215, 1529574227, 1529574231, 1529574242, 1529574244, 1529574245, 1529574248, 1529574260, 1529574262), class = c("POSIXct", "POSIXt"), tzone = "UTC"), stage_duration = c(3.78, 3.47, 2.78, 3.45, 3.32, 4.93, 4.23, 4.22, 3.85, 3.37, 5.88, 5.92, 3.97, 3.7, NA, 4.08, 3.05, 0.57, 11.18, 12.08, 2.6, 3.3, 0.23, 0.85, 0.27, 0.25, 0.82, 10.42, 0.15, 0.43, 1.4, 0.25, 0.7, 0.52, 1.12, 0.45, 12.87, 12.18, 2.92, 0.57, 14.07, 12.72, 17.12, 4.13, 3.13, 0.25, 0.33, 18.98, 1.05), veh_id = c("B35", "B05", "B04", "B08", "B14", "B13", "B04", "B17", "B41", "B05", "B26", "B08", "B35", "B19a", "B10a", "B01a", "B28", "B14", "B14", "B18", "B05", "B37", "B04", "B41", "B04", "B19a", "B04", "B17", "B35", "B13", "B35", "B02b", "B28", "B13", "B19a", "B41", "B02b", "B04", "B15", "B01a", "B41", "B13", "B28", "B27", "B33", "B19a", "B01a", "B19a", "B35")), .Names = c("Event_stage", "event_date", "stage_duration", "veh_id"), row.names = c(NA, -49L), class = c("tbl_df", "tbl", "data.frame"))
# create ggplot
ggplot(data = df, aes(x = event_date,
y = stage_duration)) +
geom_point(aes(color = Event_stage), size= 3) +
geom_line(alpha = 1/2)+
facet_wrap(~veh_id, nrow = 4) +
labs(x = "Event date", y = "Stage duration")

Related

Problem Publishing RShiny App Warning: Computation failed in `stat_summary_hex()`:

I am having a problem publishing my RShiny App. Whenever I publish it, everything works besides my stat_summary_hex() function within my ggplot. I am not really sure why it is not working, whenever I run it through RStudio it works perfectly fine, but not when I publish it. If I could get help, that would be much appreciated.
I am getting this message: Computation failed in stat_summary_hex():
Here's my code
library(shiny)
library(dplyr)
library(tidyverse)
library(ggplot2)
allStuff2 = read.csv("appData.csv")
allStuff2$player_name = iconv(allStuff2$player_name,from="UTF-8",to="ASCII//TRANSLIT")
allStuff2 = allStuff2 %>%
mutate(pitch_type = ifelse(pitch_type == "FF", "Fourseam Fastball", pitch_type),
pitch_type = ifelse(pitch_type == "SI", "Sinker", pitch_type),
pitch_type = ifelse(pitch_type == "FC", "Cutter", pitch_type),
pitch_type = ifelse(pitch_type == "SL", "Slider", pitch_type),
pitch_type = ifelse(pitch_type == "CU", "Curveball", pitch_type),
pitch_type = ifelse(pitch_type == "CH", "Changeup", pitch_type)) %>%
arrange(player_name)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("2022 Stuff+ Dashboard"),
theme = shinythemes::shinytheme("journal"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput('name',
'Select Pitcher:',
choices = unique(allStuff2$player_name)),
selectInput('pitch_type',
'Select Pitch Type:',
choices = c('Fourseam Fastball', 'Sinker', 'Cutter', 'Slider', 'Curveball', 'Changeup'),
selected = 'Fourseam Fastball')),
# Show a plot of the generated distribution
mainPanel(
plotOutput("stuffPlot"),
tableOutput("table")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
pitcherAvg = reactive({
allStuff2 %>%
filter(player_name == input$name,
pitch_type == input$pitch_type) %>%
group_by(pitch_type) %>%
summarise(velo = mean(release_speed, na.rm = T),
pfx_x = -mean(pfx_x, na.rm = T),
pfx_z = mean(pfx_z, na.rm = T),
rp_x = mean(release_pos_x, na.rm = T),
rp_z = mean(release_pos_z, na.rm = T),
p_throws = p_throws[1],
Stuff. = mean(Stuff., na.rm = T))
})
output$stuffPlot = renderPlot({
ggplot(filter(allStuff2, pitch_type == input$pitch_type,
p_throws == pitcherAvg()$p_throws,
release_pos_x >= pitcherAvg()$rp_x - 0.5,
release_pos_x <= pitcherAvg()$rp_x + 0.5,
release_pos_z >= pitcherAvg()$rp_z - 0.5,
release_pos_z <= pitcherAvg()$rp_z + 0.5,
release_speed >= pitcherAvg()$velo - 1,
release_speed <= pitcherAvg()$velo + 1),
aes(x = -pfx_x, y = pfx_z, z = Stuff.)) +
stat_summary_hex() +
scale_fill_gradient2(midpoint = 100, low = "blue", mid = "white", high = "red") +
geom_point(aes(x=pitcherAvg()$pfx_x, y=pitcherAvg()$pfx_z), colour="black", size = 5) +
theme_bw() +
labs(x = 'Horizontal Movement', y = 'Vertical Movement', fill = 'Stuff+',
title = paste(strsplit(input$name, ", ")[[1]][1], "'s Stuff+ by Location for ", input$pitch_type, sep = ""),
subtitle = "Based off of Pitchers with Similar Release Point and Velocity (Pitcher's Point of View)",
caption = 'Email: fran1412#umn.edu') +
coord_fixed() +
geom_vline(xintercept = 0) +
geom_hline(yintercept = 0) +
xlim(-2.5,2.5) +
ylim(-2.5,2.5)
})
output$table = renderTable({
allStuff2 %>%
filter(player_name == input$name,
pitch_type == input$pitch_type) %>%
group_by(pitch_type) %>%
summarise(Velocity = mean(release_speed, na.rm = T),
`Horiz. Break` = -mean(pfx_x, na.rm = T),
`Vert. Break` = mean(pfx_z, na.rm = T),
`Horiz. Release Point` = -mean(release_pos_x, na.rm = T),
`Vert. Release Point` = mean(release_pos_z, na.rm = T),
`Stuff+` = as.integer(round(mean(Stuff., na.rm = T), 0))) %>%
select(-pitch_type)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Here is my dput file:
structure(list(release_speed = c(85.9, 97.6, 100.5, 98.9, 87.3,
100.7, 100.6, 87.3, 95, 100.7, 101.2, 87.4, 102, 87.2, 103.2,
95.8, 94.3, 93.1, 89, 101, 87.2, 95.8, 101.5, 96.1, 96.2, 85.5,
88.7, 88.4, 87.7, 90.5, 88.2, 87.2, 101.2, 100.6, 96.1, 95, 85,
85.7, 102.3, 96.2, 89.3, 88.3, 91.4, 86.1, 96.5, 101.5, 96.4,
100.7, 84.5, 89.9, 101.1, 94.3, 98.8, 95.7, 95.4, 89.3, 94.7,
98.1, 94.7, 97, 95.7, 98.5, 98.9, 87.6, 90.4, 86.5, 88.4, 88.3,
101.9, 98.9, 100.7, 89, 95.4, 99.6, 102.4, 87.5, 87.2, 88, 87,
95.9, 99.1, 96.5, 91, 85.9, 87.2, 84.8, 87.1, 88, 98.3, 102.1,
86.3, 99.2, 101.9, 100, 84.7, 86.1, 98.2, 102.4, 99.8, 98.8),
pfx_x = c(0.5, -1.12, -0.79, -0.95, 0.52, -0.89, -1.44, 0.26,
-1.12, -1, -0.86, 0.2, -0.84, 0.32, -1.2, -1.09, -1.37, -0.54,
0.25, -1.12, 0.15, -0.77, -0.84, -0.99, -1.38, 0.26, 0.05,
0.32, 0.2, 0.36, 0.29, 0.01, -1.08, -1, -0.75, -0.67, 0,
0.27, -0.9, -1.48, 0.33, 0.11, 0.33, 0.22, -0.75, -1.23,
-1.25, -1.02, 0.27, -0.13, -1.1, -0.51, -0.91, -0.7, -0.84,
0.23, -1.64, -0.96, -0.76, -1.03, -1.53, -1.24, -1.41, 0.48,
0.39, 0.09, 0.56, 0.18, -1.08, -0.97, -1.21, 0.5, -1.21,
-0.89, -1.12, 0.13, 0.16, 0.39, 0.46, -1.1, -0.79, -1.19,
-0.65, 0.28, 0.23, 0.35, 0.1, 0.34, -0.35, -0.91, 0.02, -0.86,
-1.02, -0.75, 0.38, 0.08, -1.38, -1.08, -1.02, -1.15), pfx_z = c(-0.63,
0.06, 1.22, 0.13, -0.45, 1.3, 1.15, -1.16, 0.64, 1.18, 1.08,
-0.75, 1, -0.59, 1.04, 0.29, 0.25, 0.53, -0.62, 0.97, -0.61,
0.13, 1.16, 0.32, 0.05, -0.48, -0.4, -0.39, -0.9, -0.42,
-0.64, -0.59, 1.24, 1.01, 0.1, 0.25, -1.05, -0.51, 1.12,
0.35, -0.63, -0.52, -0.56, -0.69, 0.22, 0.8, 0.48, 1.18,
-0.43, -0.34, 1.17, -0.21, 1.07, 0.21, 0.32, -0.49, 0.16,
1.35, 0.12, 0.43, 0.57, 1.04, 0.42, -0.56, -0.58, -0.82,
-0.37, -0.48, 0.8, 1, 1.33, -0.7, 0.45, 0.95, 1.37, -0.32,
-0.57, -0.72, -0.85, 0.3, 1.32, 0.33, 0.51, -0.55, -0.68,
-0.58, -0.57, -0.49, 0.17, 1.3, -0.5, 1.21, 1.14, 1.28, -0.72,
-0.42, 0.33, 1.37, 1.24, 0.3), release_pos_x = c(-2.18, -1.97,
-2.11, -1.72, -1.4, -1.66, -2.19, -1.93, -2.09, -1.89, -1.85,
-1.95, -1.89, -2.16, -1.73, -2.2, -2.22, -2.45, -2.07, -2.11,
-2.18, -2.24, -1.85, -2.29, -2.23, -2.16, -2.06, -2.07, -1.77,
-1.81, -2, -1.85, -1.75, -2.13, -2.14, -2.49, -2.14, -2.05,
-1.89, -1.79, -1.71, -1.73, -1.82, -2.08, -2.32, -1.76, -2.03,
-1.93, -2.04, -2.13, -1.97, -2.27, -2.2, -2.1, -2.27, -1.94,
-1.95, -1.86, -2.27, -2.28, -1.84, -2.21, -2, -1.72, -1.94,
-1.73, -1.74, -2, -1.82, -1.89, -1.59, -1.83, -2.12, -2.12,
-1.84, -1.92, -2.13, -1.69, -1.9, -2.12, -2.15, -1.98, -2.63,
-2.24, -2.02, -1.83, -2.32, -1.96, -1.88, -2.02, -2.29, -2.2,
-2.15, -2.43, -1.68, -1.92, -2.05, -1.65, -2.12, -1.91),
release_pos_z = c(6.03, 6.29, 6.13, 6.04, 6.32, 6.24, 6.07,
6.18, 6.21, 6.07, 6.02, 6.14, 6.17, 6.16, 6.06, 5.95, 6.06,
5.8, 6, 6.02, 6.09, 6.14, 6.08, 5.93, 6.08, 6.26, 6.08, 6.12,
6.14, 6.04, 6.41, 6.23, 6.08, 6.11, 5.88, 5.95, 6.28, 6.05,
6.06, 6.29, 6.27, 6.2, 5.99, 6.23, 6.08, 6.07, 6.09, 6.18,
6.1, 6.19, 5.97, 5.95, 6.11, 5.99, 6.25, 5.97, 6.02, 6.15,
6.01, 6.04, 6.18, 6.06, 6.03, 6.2, 5.85, 6.25, 6.07, 6.25,
6.06, 5.99, 6.28, 6.18, 5.87, 6.15, 6.01, 6.12, 6.2, 6.09,
6.09, 5.94, 5.99, 6, 5.74, 6.36, 6.04, 6.16, 5.94, 6.2, 6,
5.96, 6.31, 5.99, 5.96, 6.13, 6.17, 6.21, 6.14, 6.04, 6.05,
6.21), player_name = c("Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan", "Duran, Jhoan",
"Duran, Jhoan"), p_throws = c("R", "R", "R", "R", "R", "R",
"R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R",
"R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R",
"R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R",
"R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R",
"R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R",
"R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R",
"R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R", "R",
"R", "R", "R", "R", "R", "R", "R", "R", "R", "R"), pitch_type = c("CU",
"CH", "FF", "CH", "CU", "FF", "FF", "CU", "CH", "FF", "FF",
"CU", "FF", "CU", "FF", "CH", "CH", "SL", "CU", "FF", "CU",
"CH", "FF", "CH", "CH", "CU", "CU", "CU", "CU", "CU", "CU",
"CU", "FF", "FF", "CH", "CH", "CU", "CU", "FF", "CH", "CU",
"CU", "CU", "CU", "CH", "FF", "CH", "FF", "CU", "CU", "FF",
"CH", "FF", "CH", "CH", "CU", "CH", "FF", "CH", "CH", "CH",
"FF", "CH", "CU", "CU", "CU", "CU", "CU", "FF", "FF", "FF",
"CU", "CH", "FF", "FF", "CU", "CU", "CU", "CU", "CH", "FF",
"CH", "SL", "CU", "CU", "CU", "CU", "CU", "CH", "FF", "CU",
"FF", "FF", "FF", "CU", "CU", "CH", "FF", "FF", "CH"), Stuff. = c(110.783365414633,
83.9695389876577, 126.982883382714, 100.773492772016, 122.695393953566,
119.617766459935, 118.785623163818, 130.831514405023, 61.756262527081,
96.8279502585635, 99.8866109055696, 129.196132513915, 100.484491385581,
126.11256167432, 119.411360427872, 99.3219706412825, 114.233606464787,
85.7119791685036, 130.467610095058, 112.469680863445, 141.219595669374,
107.42847854836, 108.245236352338, 99.7562461898892, 125.500229256936,
122.603272894611, 140.858949561694, 127.283168181566, 124.003695368084,
127.799900621368, 111.05222857869, 123.422997655113, 123.389105069617,
110.298869169031, 111.024067639178, 94.9843404478164, 150.783474706561,
126.3180435826, 100.726570873835, 119.565181758901, 122.935086985841,
141.957127666565, 131.623575183857, 111.648889433675, 79.7759749825627,
86.5740112525524, 93.0267990053248, 111.314387796175, 121.148229426222,
146.59561919429, 113.787652486095, 110.791517820695, 84.8826511258123,
99.281267964766, 95.6569834353375, 139.898066773251, 116.718918712834,
98.5337807227119, 96.0645681960684, 63.3208562591806, 108.735190496635,
80.3261641922412, 188.974704795231, 117.766297421377, 129.541840071494,
134.2787846362, 130.058767183761, 123.568417985415, 84.8628879203198,
68.4933286167661, 116.547997910403, 123.002208002103, 94.3192200671762,
85.8888577862371, 141.693480608564, 125.100305847327, 121.625166716099,
109.272368981841, 107.346832906711, 100.57797732587, 108.425613182612,
78.6537787345208, 85.6841619149002, 121.150698692734, 131.428503129421,
113.792479159864, 135.83714795316, 112.501339659899, 88.7429637176584,
155.705150252501, 137.647519897197, 97.8102480290771, 135.083550736976,
140.716728877702, 98.6674738226451, 134.137954477671, 169.959982914227,
141.693480608564, 137.731456338842, 93.4183672266801)), class = "data.frame", row.names = c(NA,
-100L))

How do I remove white space from between letters and not numbers?

I have character strings that I want to convert to tables. The identifier in each row can have white spaces and I need them removed without also removing spaces between the numbers. Is it possible to use a regular expression to achieve this?
For example, the data would look like this:
A B C 5.65 7.8
DC 5.65 7.8
D AB 7.9 12.2
D AB C 7.9 1.2
A BC 13.88 2.4
AB C 7.9 12.2
And I want to get to this:
ABC 5.65 7.8
DC 5.65 7.8
DAB 7.9 12.2
DABC 7.9 1.2
ABC 13.88 2.4
ABC 7.9 12.2
EDIT: As requested, this is an example of the data type and the form in which I receive it. This has 16 rows, each with 6 columns of data, but the first column is an alphabetic identifier.
# Data as I receive it.
data <- c("A", "a", "2.07", "2.35", "39.00", "82.20", "8.8", "3.80",
"B", "2.26", "2.25", "40.00", "80.80", "8.1", "1.86", "D",
"Et", "2.07", "2.22", "41.00", "83.80", "8.8", "3.87", "F",
"2.05", "2.15", "43.00", "82.20", "8.4", "3.11", "Bc", "2.08",
"2.12", "48.00", "82.60", "8.3", "2.47", "Gf", "H", "I",
"2.08", "2.10", "46.00", "82.20", "8.1", "2.90", "J", "K",
"1.95", "2.08", "38.00", "83.40", "8.7", "1.63", "L", "M",
"1.89", "2.07", "45.00", "83.80", "9.0", "1.84", "N", "2.06",
"2.05", "41.00", "80.60", "9.0", "4.09", "O", "P", "1.86",
"2.04", "48.00", "81.60", "8.6", "2.60", "Qst", "R", "1.95",
"2.03", "44.00", "82.80", "8.8", "1.40", "S", "2.03", "2.02",
"40.00", "81.40", "8.2", "1.74", "T", "1.95", "2.01", "43.00",
"81.80", "9.0", "2.30", "Unh", "1.96", "2.00", "44.00", "82.60",
"9.2", "2.40", "V", "W", "C", "1.98", "1.97", "40.00",
"82.00", "8.1", "1.15", "Yu", "1.90", "1.96", "41.00", "82.80",
"9.6", "2.08", "Z", "a", "bi", "1.90", "1.95", "42.00",
"84.20", "9.6", "1.69")
# Required format
data2 <- c("Aa", "2.07", "2.35", "39.00", "82.20", "8.8", "3.80",
"B", "2.26", "2.25", "40.00", "80.80", "8.1", "1.86",
"DEt", "2.07", "2.22", "41.00", "83.80", "8.8", "3.87", "F",
"2.05", "2.15", "43.00", "82.20", "8.4", "3.11", "Bc", "2.08",
"2.12", "48.00", "82.60", "8.3", "2.47", "GfHI",
"2.08", "2.10", "46.00", "82.20", "8.1", "2.90", "JK",
"1.95", "2.08", "38.00", "83.40", "8.7", "1.63", "LM",
"1.89", "2.07", "45.00", "83.80", "9.0", "1.84", "N", "2.06",
"2.05", "41.00", "80.60", "9.0", "4.09", "OP", "1.86",
"2.04", "48.00", "81.60", "8.6", "2.60", "QstR", "1.95",
"2.03", "44.00", "82.80", "8.8", "1.40", "S", "2.03", "2.02",
"40.00", "81.40", "8.2", "1.74", "T", "1.95", "2.01", "43.00",
"81.80", "9.0", "2.30", "Unh", "1.96", "2.00", "44.00", "82.60",
"9.2", "2.40", "VWC", "1.98", "1.97", "40.00",
"82.00", "8.1", "1.15", "Yu", "1.90", "1.96", "41.00", "82.80",
"9.6", "2.08", "Zabi", "1.90", "1.95", "42.00",
"84.20", "9.6", "1.69")
df <- data.frame(matrix(data2, ncol=7, byrow=T))
To do as you request within your R environment, one approach is to convert the vector to a string, apply a regular expression filter to the string, then convert the string back to a vector.
See details below, hopefully this points you in the right direction.
Solution
data <- c("A", "a", "2.07", "2.35", "39.00", "82.20", "8.8", "3.80",
"B", "2.26", "2.25", "40.00", "80.80", "8.1", "1.86", "D",
"Et", "2.07", "2.22", "41.00", "83.80", "8.8", "3.87", "F",
"2.05", "2.15", "43.00", "82.20", "8.4", "3.11", "Bc", "2.08",
"2.12", "48.00", "82.60", "8.3", "2.47", "Gf", "H", "I",
"2.08", "2.10", "46.00", "82.20", "8.1", "2.90", "J", "K",
"1.95", "2.08", "38.00", "83.40", "8.7", "1.63", "L", "M",
"1.89", "2.07", "45.00", "83.80", "9.0", "1.84", "N", "2.06",
"2.05", "41.00", "80.60", "9.0", "4.09", "O", "P", "1.86",
"2.04", "48.00", "81.60", "8.6", "2.60", "Qst", "R", "1.95",
"2.03", "44.00", "82.80", "8.8", "1.40", "S", "2.03", "2.02",
"40.00", "81.40", "8.2", "1.74", "T", "1.95", "2.01", "43.00",
"81.80", "9.0", "2.30", "Unh", "1.96", "2.00", "44.00", "82.60",
"9.2", "2.40", "V", "W", "C", "1.98", "1.97", "40.00",
"82.00", "8.1", "1.15", "Yu", "1.90", "1.96", "41.00", "82.80",
"9.6", "2.08", "Z", "a", "bi", "1.90", "1.95", "42.00",
"84.20", "9.6", "1.69")
# Use stringi base regular expression engine
require(stringi)
# Convert the vector data to be a string sequence - so we can manipulate as text
data1 <- toString(data)
# Now we can apply the regular expression substitution to the data (formatted as a string...
# Here we do a:
#
# (?<!\d) - Negative look behind to prevent a digit.
# , - A literal combination of quotes, comma and space. We drop the ", " in conversion to string...
# (?!\d) - Negative look ahead to prevent a digit.
#
data3 = stri_replace_all_regex(str = data1, pattern = '(?<!\\d), (?!\\d)', replacement = '')
# OK, check the string data...
data3
# Now we convert the string back to be a vector...
newData = strsplit(data3, " ")[[1]]
newData
# Now we convert to a dataframe...
df <- data.frame(matrix(newData, ncol=7, byrow=T))
df
# Done
Output
> data <- c("A", "a", "2.07", "2.35", "39.00", "82.20", "8.8", "3.80",
+ "B", "2.26", "2.25", "40.00", "80.80", "8.1", "1.86", "D",
+ "Et", "2.07", "2.22", "41.00", "83.80", "8.8", "3.87", "F",
+ "2.05", "2.15", "43.00", "82.20", "8.4", "3.11", "Bc", "2.08",
+ "2.12", "48.00", "82.60", "8.3", "2.47", "Gf", "H", "I",
+ "2.08", "2.10", "46.00", "82.20", "8.1", "2.90", "J", "K",
+ "1.95", "2.08", "38.00", "83.40", "8.7", "1.63", "L", "M",
+ "1.89", "2.07", "45.00", "83.80", "9.0", "1.84", "N", "2.06",
+ "2.05", "41.00", "80.60", "9.0", "4.09", "O", "P", "1.86",
+ "2.04", "48.00", "81.60", "8.6", "2.60", "Qst", "R", "1.95",
+ "2.03", "44.00", "82.80", "8.8", "1.40", "S", "2.03", "2.02",
+ "40.00", "81.40", "8.2", "1.74", "T", "1.95", "2.01", "43.00",
+ "81.80", "9.0", "2.30", "Unh", "1.96", "2.00", "44.00", "82.60",
+ "9.2", "2.40", "V", "W", "C", "1.98", "1.97", "40.00",
+ "82.00", "8.1", "1.15", "Yu", "1.90", "1.96", "41.00", "82.80",
+ "9.6", "2.08", "Z", "a", "bi", "1.90", "1.95", "42.00",
+ "84.20", "9.6", "1.69")
>
> # Use stringi base regular expression engine
> require(stringi)
>
> # Convert the vector data to be a string sequence - so we can manipulate as text
> data1 <- toString(data)
>
> # Now we can apply the regular expression substitution to the data (formatted as a string...
> # Here we do a:
> #
> # (?<!\d) - Negative look behind to prevent a digit.
> # , - A literal combination of quotes, comma and space. We drop the ", " in conversion to string...
> # (?!\d) - Negative look ahead to prevent a digit.
> #
> data3 = stri_replace_all_regex(str = data1, pattern = '(?<!\\d), (?!\\d)', replacement = '')
> # OK, check the string data...
> data3
[1] "Aa, 2.07, 2.35, 39.00, 82.20, 8.8, 3.80, B, 2.26, 2.25, 40.00, 80.80, 8.1, 1.86, DEt, 2.07, 2.22, 41.00, 83.80, 8.8, 3.87, F, 2.05, 2.15, 43.00, 82.20, 8.4, 3.11, Bc, 2.08, 2.12, 48.00, 82.60, 8.3, 2.47, GfHI, 2.08, 2.10, 46.00, 82.20, 8.1, 2.90, JK, 1.95, 2.08, 38.00, 83.40, 8.7, 1.63, LM, 1.89, 2.07, 45.00, 83.80, 9.0, 1.84, N, 2.06, 2.05, 41.00, 80.60, 9.0, 4.09, OP, 1.86, 2.04, 48.00, 81.60, 8.6, 2.60, QstR, 1.95, 2.03, 44.00, 82.80, 8.8, 1.40, S, 2.03, 2.02, 40.00, 81.40, 8.2, 1.74, T, 1.95, 2.01, 43.00, 81.80, 9.0, 2.30, Unh, 1.96, 2.00, 44.00, 82.60, 9.2, 2.40, VWC, 1.98, 1.97, 40.00, 82.00, 8.1, 1.15, Yu, 1.90, 1.96, 41.00, 82.80, 9.6, 2.08, Zabi, 1.90, 1.95, 42.00, 84.20, 9.6, 1.69"
>
> # Now we convert the string back to be a vector...
> newData = strsplit(data3, " ")[[1]]
> newData
[1] "Aa," "2.07," "2.35," "39.00," "82.20," "8.8," "3.80," "B," "2.26," "2.25," "40.00," "80.80,"
[13] "8.1," "1.86," "DEt," "2.07," "2.22," "41.00," "83.80," "8.8," "3.87," "F," "2.05," "2.15,"
[25] "43.00," "82.20," "8.4," "3.11," "Bc," "2.08," "2.12," "48.00," "82.60," "8.3," "2.47," "GfHI,"
[37] "2.08," "2.10," "46.00," "82.20," "8.1," "2.90," "JK," "1.95," "2.08," "38.00," "83.40," "8.7,"
[49] "1.63," "LM," "1.89," "2.07," "45.00," "83.80," "9.0," "1.84," "N," "2.06," "2.05," "41.00,"
[61] "80.60," "9.0," "4.09," "OP," "1.86," "2.04," "48.00," "81.60," "8.6," "2.60," "QstR," "1.95,"
[73] "2.03," "44.00," "82.80," "8.8," "1.40," "S," "2.03," "2.02," "40.00," "81.40," "8.2," "1.74,"
[85] "T," "1.95," "2.01," "43.00," "81.80," "9.0," "2.30," "Unh," "1.96," "2.00," "44.00," "82.60,"
[97] "9.2," "2.40," "VWC," "1.98," "1.97," "40.00," "82.00," "8.1," "1.15," "Yu," "1.90," "1.96,"
[109] "41.00," "82.80," "9.6," "2.08," "Zabi," "1.90," "1.95," "42.00," "84.20," "9.6," "1.69"
>
> # Now we convert to a dataframe...
> df <- data.frame(matrix(newData, ncol=7, byrow=T))
> df
X1 X2 X3 X4 X5 X6 X7
1 Aa, 2.07, 2.35, 39.00, 82.20, 8.8, 3.80,
2 B, 2.26, 2.25, 40.00, 80.80, 8.1, 1.86,
3 DEt, 2.07, 2.22, 41.00, 83.80, 8.8, 3.87,
4 F, 2.05, 2.15, 43.00, 82.20, 8.4, 3.11,
5 Bc, 2.08, 2.12, 48.00, 82.60, 8.3, 2.47,
6 GfHI, 2.08, 2.10, 46.00, 82.20, 8.1, 2.90,
7 JK, 1.95, 2.08, 38.00, 83.40, 8.7, 1.63,
8 LM, 1.89, 2.07, 45.00, 83.80, 9.0, 1.84,
9 N, 2.06, 2.05, 41.00, 80.60, 9.0, 4.09,
10 OP, 1.86, 2.04, 48.00, 81.60, 8.6, 2.60,
11 QstR, 1.95, 2.03, 44.00, 82.80, 8.8, 1.40,
12 S, 2.03, 2.02, 40.00, 81.40, 8.2, 1.74,
13 T, 1.95, 2.01, 43.00, 81.80, 9.0, 2.30,
14 Unh, 1.96, 2.00, 44.00, 82.60, 9.2, 2.40,
15 VWC, 1.98, 1.97, 40.00, 82.00, 8.1, 1.15,
16 Yu, 1.90, 1.96, 41.00, 82.80, 9.6, 2.08,
17 Zabi, 1.90, 1.95, 42.00, 84.20, 9.6, 1.69
> # Done

ggplot density plot: Different x-axis for each group

I am trying to plot multiple density plots for some data.
I have the following code:
ggplot(data=stack) +
geom_density(aes(x=OfferPrice, group=Country, fill=Country),
alpha=0.5, adjust=2) +
facet_grid(~Country) +
theme_bw()
My problem arises with the x-axis on each of the density plots.
Looking a little closer at the data:
stack %>%
group_by(Country) %>%
summarise(min(OfferPrice),
mean(OfferPrice),
max(OfferPrice))
It looks like Country - JN has some very high numbers. Earlier I cut the top and bottom 5% of extreme values for each group so there shouldn´t necessarily be extreme values. What I think is wrong is the x-axis on the plots are using the maximum from the JN group. How is it possible to have different x-axis depending on each group?
Data:
stack <- structure(list(Country = c("US", "GB", "US", "HK", "JN", "US",
"CH", "CA", "US", "US", "CA", "JN", "GB", "AU", "US", "GB", "US",
"GB", "HK", "CH", "CA", "CA", "US", "GB", "TA", "JN", "CA", "CA",
"CA", "CA", "CH", "GB", "CA", "HK", "CA", "US", "US", "CA", "US",
"AU", "CA", "CA", "CA", "US", "GB", "GB", "AU", "US", "US", "AU",
"CA", "CA", "CA", "US", "CA", "GB", "CA", "US", "GB", "US", "AU",
"AU", "US", "CA", "US", "GB", "AU", "CH", "CA", "CA", "GB", "AU",
"AU", "CH", "CA", "AU", "CH", "US", "CH", "TA", "AU", "AU", "GB",
"CH", "HK", "AU", "AU", "CA", "US", "TA", "GB", "US", "AU", "US",
"CA", "CA", "US", "AU", "CA", "US", "CA", "US", "CA", "US", "CA",
"US", "US", "AU", "CA", "AU", "GB", "US", "HK", "AU", "US", "CA",
"JN", "JN", "GB", "JN", "CA", "CA", "AU", "GB", "GB", "US", "US",
"US", "AU", "GB", "CA", "CA", "US", "CH", "GB", "US", "US", "AU",
"GB", "CH", "JN", "CA", "AU", "CA", "US", "US", "AU", "AU", "CA",
"US", "GB", "GB", "US", "US", "CA", "US", "HK", "AU", "US", "GB",
"US", "GB", "GB", "US", "CA", "JN", "CA", "AU", "CA", "CA", "GB",
"CA", "HK", "HK", "US", "CH", "US", "AU", "TA", "US", "CH", "HK",
"AU", "US", "HK", "GB", "AU", "CH", "US", "AU", "US", "CH", "US",
"CH", "CA", "AU", "HK", "CA", "US", "CH", "GB", "CA", "CA", "CA",
"CA", "US", "CA", "CA", "US", "HK", "US", "HK", "AU", "GB", "AU",
"CH", "US", "AU", "CA", "CA", "US", "GB", "AU", "US", "CH", "CA",
"CA", "CA", "US", "AU", "GB", "GB", "CA", "AU", "CA", "AU", "US",
"HK", "AU", "US", "AU", "CA", "US", "US", "US", "CA", "GB", "CA",
"US", "CA", "US", "AU", "CA", "US", "AU", "CH", "GB", "CA", "CA",
"CA", "CA", "HK", "AU", "TA", "AU", "GB", "AU", "CA", "JN", "US",
"CA", "CA", "AU", "US", "US", "GB", "CA", "US", "GB", "US", "CA",
"CA", "CH", "US", "US", "US", "US", "US", "HK", "CH", "CA", "CA",
"CA", "AU", "GB", "CH", "CA", "GB", "CA", "AU"), EffectiveDate = structure(c(17617,
17500, 17556, 17596, 17618, 17667, 17786, 17728, 17569, 17760,
17585, 17613, 17806, 17847, 17786, 17665, 17702, 17683, 17574,
17725, 17723, 17658, 17563, 17847, 17584, 17578, 17842, 17522,
17697, 17521, 17680, 17794, 17813, 17697, 17773, 17578, 17556,
17595, 17669, 17501, 17655, 17562, 17668, 17589, 17582, 17780,
17812, 17667, 17611, 17758, 17751, 17617, 17505, 17505, 17725,
17506, 17513, 17541, 17644, 17702, 17828, 17688, 17696, 17519,
17850, 17746, 17779, 17547, 17806, 17701, 17638, 17759, 17786,
17770, 17835, 17750, 17841, 17653, 17788, 17553, 17794, 17654,
17724, 17675, 17802, 17638, 17625, 17514, 17709, 17758, 17681,
17507, 17708, 17758, 17800, 17569, 17821, 17493, 17834, 17848,
17639, 17549, 17590, 17575, 17630, 17639, 17760, 17724, 17701,
17725, 17756, 17794, 17511, 17494, 17822, 17758, 17709, 17492,
17605, 17709, 17602, 17644, 17814, 17696, 17760, 17603, 17730,
17675, 17590, 17724, 17743, 17680, 17690, 17711, 17560, 17570,
17702, 17512, 17626, 17618, 17576, 17731, 17527, 17709, 17729,
17672, 17515, 17527, 17641, 17597, 17812, 17540, 17602, 17743,
17662, 17709, 17505, 17737, 17603, 17515, 17568, 17843, 17738,
17508, 17816, 17578, 17590, 17812, 17743, 17528, 17616, 17687,
17646, 17515, 17816, 17557, 17507, 17564, 17777, 17802, 17511,
17842, 17584, 17556, 17547, 17844, 17590, 17674, 17759, 17583,
17836, 17721, 17724, 17801, 17578, 17808, 17682, 17849, 17708,
17515, 17746, 17633, 17759, 17591, 17802, 17540, 17560, 17588,
17800, 17787, 17821, 17724, 17645, 17527, 17722, 17556, 17704,
17844, 17619, 17792, 17577, 17637, 17843, 17765, 17688, 17562,
17834, 17738, 17653, 17645, 17718, 17676, 17637, 17570, 17490,
17534, 17646, 17625, 17766, 17808, 17675, 17786, 17808, 17555,
17739, 17802, 17617, 17619, 17667, 17634, 17662, 17711, 17806,
17513, 17627, 17673, 17574, 17647, 17609, 17619, 17521, 17543,
17686, 17807, 17613, 17543, 17543, 17528, 17694, 17576, 17584,
17521, 17605, 17618, 17723, 17641, 17683, 17823, 17634, 17844,
17836, 17816, 17539, 17583, 17618, 17687, 17589, 17602, 17717,
17535, 17718, 17625, 17822, 17651, 17521, 17751, 17617, 17563,
17578, 17772), class = "Date"), OfferPrice = c(44, 13, 33, 0.3,
3000, 23, 6.26, 0.35, 10, 6.25, 0.25, 7110, 109.5, 0.11, 16,
2, 5.5, 15, 0.5, 8.5, 0.5, 0.2, 5, 92.5, 22, 103740, 0.23, 0.75,
8.65, 1.23, 17.4, 1.5, 0.38, 0.84, 8.1, 27.5, 10, 1, 14, 0.42,
0.1, 1.82, 2, 39.8, 238, 340, 0.3, 4.5, 41.5, 0.2, 0.25, 0.27,
0.35, 5, 0.3, 115, 0.15, 5, 142.25, 14, 2.43, 0.02, 24, 0.115,
8.25, 25, 0.155, 9.6, 0.67, 6, 52.5, 0.2, 0.2, 1.37, 1.6, 0.65,
2.9, 4, 7, 72, 0.025, 0.14, 22.5, 6.75, 0.64, 0.8, 0.8, 0.4,
22, 94.8, 15, 10, 2.45, 34, 1, 9.3, 6.25, 0.018, 0.2, 24.5, 0.3,
2.9, 0.35, 2.05, 0.4, 29.5, 2.26, 0.36, 0.75, 0.027, 2.8, 16,
3.54, 0.018, 10, 0.15, 1780, 1602, 120, 3900, 0.25, 0.18, 2.32,
269, 175, 18, 18, 23, 0.2, 10, 0.1, 0.6, 4.8, 6, 164.5, 7, 26.42,
0.02, 190, 11, 992, 0.2, 1.42, 0.55, 23, 33.5, 0.35, 0.065, 1.16,
29.5, 65, 8, 27.5, 18.9, 0.15, 17, 0.63, 0.34, 26.25, 0.65, 6.9,
10, 6.75, 1.21, 0.95, 73125, 2.5961, 0.054, 1.2, 9.64, 251, 2.46,
0.18, 0.375, 9.97, 20.43, 25.5, 0.025, 60, 3, 1.55, 0.5, 0.2,
17, 0.443, 8, 0.05, 5.25, 1.15, 0.45, 7.155, 17, 24.5, 12.5,
2.1, 0.75, 0.35, 0.39, 38.2, 0.63, 16, 0.15, 0.1, 0.12, 2.32,
10, 0.3, 1.66, 17, 0.4, 1.3, 0.3, 1.08, 30, 0.8, 10.88, 0.9,
0.21, 0.17, 1.7, 2.25, 1, 0.08, 1.5, 14.75, 0.35, 0.44, 0.35,
17, 2, 37, 195, 0.165, 0.02, 0.2, 0.015, 25, 1.09, 0.45, 10,
0.145, 0.92, 36, 13.25, 4, 0.6, 101, 0.7, 15, 0.3, 0.7, 0.06,
0.25, 6.5, 1.1, 16.72, 1.25, 0.1, 0.12, 0.28, 1.18, 0.4, 0.02,
75, 0.08, 5, 0.02, 0.5, 2878, 8, 0.15, 0.33, 0.1, 21.25, 1, 12,
5.83, 4.25, 1.65, 3.15, 3, 0.15, 7.4, 31.25, 12, 24, 19.75, 41.5,
0.88, 13.65, 0.25, 0.15, 0.25, 2.35, 101, 1.26, 1.65, 10, 1.32,
0.5)), row.names = c(NA, -300L), class = c("tbl_df", "tbl", "data.frame"
))
You can specify whether you want free scales (x, y or both) in facet_grid.
ggplot(data=stack) +
geom_density(aes(x=OfferPrice, group=Country, fill=Country),
alpha=0.5, adjust=2) +
facet_grid(~Country, scales = "free_x") +
theme_bw()

How to Plot Time-series data on Horizontal bar in R?

R::How to Plot Single Horizontal Bar Showing different stages on Continous Time-series Data from startdate to present date and for navigating time a horizontal scrollbar in R?
This is my data:
var_events time_date event_duration veh_id
LD 17-06-2018 13:25 6.52 B33
WL 17-06-2018 13:25 14.52 B31
TL 17-06-2018 13:26 0.32 B32
TE 17-06-2018 13:26 4.58 B13
UL 17-06-2018 13:26 3.45 B12
WT 17-06-2018 13:26 5.46 B25
UL 17-06-2018 13:26 1.56 B17
TL 17-06-2018 13:26 13.6 B33
SL 17-06-2018 13:26 0.05 B32
Here is a Example of line chart of Previous code:
require(ggplot2)
require(dplyr)
df = structure(list(Event_stage = c("SE", "MN", "MN", "TE", "TE", "TE", "TE", "TE", "TE", "TE", "TE", "WL", "TE", "TE", "SE", "TE", "TE", "WL", "WT", "MN", "WL", "TE", "WL", "WL", "WT", "WL", "LD", "WT", "WL", "WT", "WT", "TE", "WL", "LD", "WT", "LD", "MN", "TL", "TE", "WL", "TL", "TL", "WT", "TE", "TE", "LD", "WT", "TL", "LD" ), event_date = structure(c(1529573704, 1529573710, 1529573713, 1529573724, 1529573855, 1529573874, 1529573880, 1529573895, 1529573906, 1529573918, 1529573925, 1529573931, 1529573931, 1529573941, 1529573947, 1529573969, 1529574006, 1529574054, 1529574088, 1529574114, 1529574120, 1529574123, 1529574134, 1529574137, 1529574148, 1529574163, 1529574164, 1529574148, 1529574169, 1529574170, 1529574178, 1529574188, 1529574189, 1529574196, 1529574178, 1529574188, 1529574203, 1529574213, 1529574214, 1529574214, 1529574215, 1529574227, 1529574231, 1529574242, 1529574244, 1529574245, 1529574248, 1529574260, 1529574262), class = c("POSIXct", "POSIXt"), tzone = "UTC"), stage_duration = c(3.78, 3.47, 2.78, 3.45, 3.32, 4.93, 4.23, 4.22, 3.85, 3.37, 5.88, 5.92, 3.97, 3.7, NA, 4.08, 3.05, 0.57, 11.18, 12.08, 2.6, 3.3, 0.23, 0.85, 0.27, 0.25, 0.82, 10.42, 0.15, 0.43, 1.4, 0.25, 0.7, 0.52, 1.12, 0.45, 12.87, 12.18, 2.92, 0.57, 14.07, 12.72, 17.12, 4.13, 3.13, 0.25, 0.33, 18.98, 1.05), veh_id = c("B35", "B05", "B04", "B08", "B14", "B13", "B04", "B17", "B41", "B05", "B26", "B08", "B35", "B19a", "B10a", "B01a", "B28", "B14", "B14", "B18", "B05", "B37", "B04", "B41", "B04", "B19a", "B04", "B17", "B35", "B13", "B35", "B02b", "B28", "B13", "B19a", "B41", "B02b", "B04", "B15", "B01a", "B41", "B13", "B28", "B27", "B33", "B19a", "B01a", "B19a", "B35")), .Names = c("Event_stage", "event_date", "stage_duration", "veh_id"), row.names = c(NA, -49L), class = c("tbl_df", "tbl", "data.frame"))
# create ggplot
ggplot(data = df %>% filter(veh_id == "B35"), aes(x = event_date,
y = stage_duration)) +
geom_point(aes(color = Event_stage), size= 3) +
geom_line(alpha = 1/2)+
labs(x = "Event date", y = "Stage duration")
enter image description here
This is Sample bar plot, Everything same as in above line chart but instead of line with spikes a Horizontal line or I just want a single bar which is interactive with a Slider/Scrollbar to navigate time ::
enter image description here
Something resembling this plot,But only a Single Horizontal bar with a scrollbar from start-time to present-time::
enter image description here
df %>% filter(veh_id == "B35") %>%
ggplot(
aes(
x = event_date,
y = stage_duration)
) +
geom_bar(stat = "identity") +
labs(x = "Event date", y = "Stage duration") +
coord_flip()

Remove margin inside plot pf ggplot2

this is my script and the associated plot:
library(ggplot2)
library(reshape)
df <- structure(list(ID = structure(1:19, .Label = c("2818/22/0834",
"2818/22/0851", "2818/22/0853", "2818/22/0886", "B0F", "B12T",
"B1T", "B21T", "B22F", "B26T", "B33F", "B4F", "P1", "P21", "P24",
"P25", "P27", "P28", "P29"), class = "factor"), K = c(0.089,
0.094, 0.096, 0.274, 0.09, 0.312, 0.33, 0.178, 0.05, 0.154, 0.083,
0.098, 0.035, 0.084, 0.053, 0.061, 0.043, 0.094, 0.101), Na = c(2.606,
3.822, 4.977, 2.522, 15.835, 83.108, 52.041, 41.448, 11.849,
40.531, 5.854, 10.151, 3.52, 8.445, 5.273, 7.246, 6.177, 14.813,
15.569), Cl = c(3.546, 6.181, 8.422, 3.733, 14.685, 96.911, 65.518,
79.01, 10.349, 53.361, 6.12, 10.832, 2.313, 10.312, 5.641, 8.708,
6.138, 12.302, 20.078), Mg = c(1.487, 1.773, 1.992, 1.143, 2.991,
1.678, 2.23, 3.288, 1.148, 2.428, 3.428, 2.729, 0.777, 2.554,
2.374, 4.075, 1.993, 1.881, 3.034), Ca = c(5.529, 6.205, 6.59,
4.099, 10.631, 4.564, 6.652, 13.374, 4.332, 10.542, 11.194, 10.053,
2.969, 7.73, 8.163, 11.539, 6.166, 5.968, 9.299), SO4 = c(0.663,
0.831, 0.607, 0.882, 9.013, 0.896, 0.652, 0.021, 1.446, 0.012,
8.832, 6.665, 1.003, 2.575, 3.685, 7.121, 3.64, 5.648, 2.397),
HCO3 = c(7.522, 5.498, 6.15, 5.242, 8.582, 4.067, 5.65, 9.364,
5.435, 8.068, 9.054, 8.326, 4.805, 7.235, 7.488, 9.234, 6.352,
6.98, 8.34)), .Names = c("ID", "K", "Na", "Cl", "Mg", "Ca",
"SO4", "HCO3"), class = "data.frame", row.names = c(NA, -19L))
df_melted<-melt(df, na.rm=T)
ggplot(df_melted, aes(variable, value, group=ID, color=ID))+
geom_point(size=2) +
geom_line() +
theme(legend.position="none") +
scale_y_log10(breaks=seq(0, 100, 10))
Is there a way to remove the spaces at the beginning and at the end of the plot? I tried with xlim but the problem is that the x variable is not a numerical variable, so, something like xlim(c("K", "HCO3")) doesn't work.
This is a discrete scale, but you can still used the expand argument as follows. Whether the output looks acceptable or not is another matter. Play with the c(0,0) values until you find something that suits. Using 0.1 for the second value gives a slightly better plot, in my view...
ggplot(df_melted, aes(variable, value, group=ID, color=ID))+
geom_point(size=2) +
geom_line() +
theme(legend.position="none") +
scale_y_log10(breaks=seq(0, 100, 10)) +
scale_x_discrete(expand = c(0,0))

Resources