How to Plot Time-series data on Horizontal bar in R? - 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()

Related

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

Mean ±SE in scatterplot of three groups

edit: It's occurred to me that standard error cannot / will not be computed by R for the non-HG species (in particular, GB) where n is rather small (n = 4 for GB). Even if this is not the case for my failure to produce the plots, this low n is likely to invalidate standard error calculations. I'd appreciate others' advice.
I've seen a few similar answers, but nothing that closely approximates to what I want to do.
I've successfully used the code located here -
https://community.rstudio.com/t/using-stat-instead-of-dplyr-to-summarize-groups-in-a-ggplot/13916/2
- to produce a biplot showing the mean +/- s.e. whiskers for two groups of samples.
However, I've not been successful in re-working this code for use when there are three groups of samples. Can anyone tell me where I'm going wrong and how to rectify it please?
Thank you very much indeed in advance for advice; much appreciated :-)
Sample data and code (below):
Species d13C lin d34S lin
GB -20.1 8.09
HG -20.1 8.09
HG -19.51 9.46
HG -17.55 16.81
HG -23.72 8.03
HG -23.79 9.02
HG -18.09 8.64
HG -21.98 10.95
HG -18.6 8.5
HG -22.75 10.9
HG -21.7 9.08
HG -25 7.23
HG -17.61 16.56
HG -20.5 10.17
HG -18.14 15.4
GB -22.01 15.78
GB -19.62 12.62
LB -21.69 14.96
LB -25.56 8.4
LB -23.43 13.61
LB -22.92 12.68
LB -23.81 15.47
LB -25.42 8.63
HG -26.11 14.15
HG -20.61 9.27
HG -19.18 15.67
HG -19.76 14.49
HG -17.98 8.77
HG -22.71 9.26
HG -23.33 9.8
HG -22.7 10.84
HG -25.05 14.48
HG -24.63 9.98
HG -21.08 10.53
HG -24.93 8.42
HG -19.62 15.6
HG -23.82 7.7
HG -20.08 8.83
HG -24.36 11.01
HG -24.55 9.25
HG -21.89 16.59
HG -19 14.9
HG -18.96 17.69
HG -24.49 7.73
HG -19.15 8.82
HG -21.68 15.42
HG -23.6 8.03
HG -21.07 7.7
HG -19.56 14.34
HG -18.31 7.19
HG -23.39 14.24
HG -19.48 17.21
HG -18.25 8.71
HG -23.9 12.06
HG -23.19 8.59
HG -25.03 15.09
HG -21.01 11.74
HG -20.44 14.47
HG -24.4 10.5
HG -24.17 13.62
HG -22.41 15.63
HG -23.23 15.03
HG -25.22 13.36
HG -25.74 7.21
HG -18.01 12.47
HG -19.51 13.72
HG -25 14.56
HG -23.8 8.04
HG -21.99 12.24
HG -19.43 15.52
HG -24.93 9.45
HG -24.86 9.27
LB -25.27 12.94
LB -25.58 12.73
HG -26.02 10.28
HG -24.02 15.89
GB -24.85 7.14
LB -23.24 16.17
library(pillar)
library(rlang)
library (dplyr)
library(ggplot2)
library(ggstance)
fnc = function(data, group, x, y, z, adj=1) {
group=enquo(group)
x = enquo(x)
y = enquo(y)
z = enquo(z)
# Set size of whisker end caps
wv = data %>% pull(!!y) %>% range %>% diff/100*adj
wh = data %>% pull(!!x) %>% range %>% diff/100*adj
# If grouping variable is numeric, turn it into a factor
if(data %>% pull(!!group) %>% is.numeric) {
data = data %>%
mutate(!!quo_name(group) := factor(!!group))
}
# Generate column names for the x and y means that we'll calculate below
xmean = paste0(quo_text(x), "_mean")
ymean = paste0(quo_text(y), "_mean")
zmean = paste0(quo_text(z), "_mean")
left_join(data,
data %>%
group_by(!!group) %>%
summarise_at(vars(!!x, !!y, !!z) funs(mean=mean))
) %>%
ggplot(aes(colour=!!group)) +
geom_point(aes(x=!!x, y=!!y, z=!!z), size=5) +
theme_classic() +
geom_point(aes(x=!!sym(xmean), y=!!sym(ymean), z=!!sym(zmean)), shape=18, size=3) +
stat_summary(fun.data=mean_se, fun.args=list(mult=1.96),
aes(x=!!sym(xmean), y=!!y, z=!!z),
geom="errorbar", width=wh) +
stat_summaryh(fun.data=mean_se_h, fun.args=list(mult=1.96),
aes(x=!!x, y=!!y, z=!!z, sym(ymean)),
geom="errorbarh", width=wv)
}
##plot the ERROR BAR
fnc(data, Species, d13C.lin, d34S.lin) +
theme(axis.text=element_text(size=12, face = "bold", colour = "black"),
axis.title=element_text(size=17,face="bold", colour = "black")) +
theme(legend.text=element_text(size=14, face = "bold", colour = "black")) +
theme(legend.background = element_rect(fill="white",
size=0.5, linetype="solid",
colour ="black")) +
theme(legend.title = element_text(size = 14, face = "bold", colour = "black")) +
labs(y = expression(delta^{34}*"S"*" (‰)"), x = expression(delta^{13}*"C"*" (‰)"))
This is the error message I receive:
Error in is_call(expr, paren_sym) :
argument "expr" is missing, with no default
As I say, any help greatly appreciated please.
The error comes from the z argument which is not passed as argument in the function.
fnc <- function(data, group, x, y, adj=1) {
group <- enquo(group)
x <- enquo(x)
y <- enquo(y)
# Set size of whisker end caps
wv <- data %>%
pull(!!y) %>%
range %>%
diff/100*adj
wh <- data %>%
pull(!!x) %>%
range %>%
diff/100*adj
# If grouping variable is numeric, turn it into a factor
if(data %>%
pull(!!group) %>%
is.numeric) {
data <- data %>%
mutate(!!quo_name(group) := factor(!!group))
}
# Generate column names for the x and y means that we'll calculate below
xmean <- str_c(as_name(x), "_mean")
ymean <- str_c(as_name(y), "_mean")
left_join(data,
data %>%
group_by(!!group) %>%
summarise_at(vars(!!x, !!y), list(mean = ~ mean(.)))) %>%
ggplot(aes(colour=!!group)) +
geom_point(aes(x=!!x, y=!!y), size=5) +
theme_classic() +
geom_point(aes(x=!!sym(xmean), y=!!sym(ymean)), shape=18, size=3) +
stat_summary(fun.data=mean_se, fun.args=list(mult=1.96),
aes(x=!!sym(xmean), y=!!y),
geom="errorbar", width=wh) +
stat_summaryh(fun.data=mean_se_h, fun.args=list(mult=1.96),
aes(x=!!x, y = !!y),
geom="errorbarh", width=wv)
}
-testing
fnc(df1, Species, d13C.lin, d34S.lin)
data
df1 <- structure(list(Species = c("GB", "HG", "HG", "HG", "HG", "HG",
"HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "GB", "GB",
"LB", "LB", "LB", "LB", "LB", "LB", "HG", "HG", "HG", "HG", "HG",
"HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG",
"HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG",
"HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG",
"HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG", "HG",
"LB", "LB", "HG", "HG", "GB", "LB"), d13C.lin = c(-20.1, -20.1,
-19.51, -17.55, -23.72, -23.79, -18.09, -21.98, -18.6, -22.75,
-21.7, -25, -17.61, -20.5, -18.14, -22.01, -19.62, -21.69, -25.56,
-23.43, -22.92, -23.81, -25.42, -26.11, -20.61, -19.18, -19.76,
-17.98, -22.71, -23.33, -22.7, -25.05, -24.63, -21.08, -24.93,
-19.62, -23.82, -20.08, -24.36, -24.55, -21.89, -19, -18.96,
-24.49, -19.15, -21.68, -23.6, -21.07, -19.56, -18.31, -23.39,
-19.48, -18.25, -23.9, -23.19, -25.03, -21.01, -20.44, -24.4,
-24.17, -22.41, -23.23, -25.22, -25.74, -18.01, -19.51, -25,
-23.8, -21.99, -19.43, -24.93, -24.86, -25.27, -25.58, -26.02,
-24.02, -24.85, -23.24), d34S.lin = c(8.09, 8.09, 9.46, 16.81,
8.03, 9.02, 8.64, 10.95, 8.5, 10.9, 9.08, 7.23, 16.56, 10.17,
15.4, 15.78, 12.62, 14.96, 8.4, 13.61, 12.68, 15.47, 8.63, 14.15,
9.27, 15.67, 14.49, 8.77, 9.26, 9.8, 10.84, 14.48, 9.98, 10.53,
8.42, 15.6, 7.7, 8.83, 11.01, 9.25, 16.59, 14.9, 17.69, 7.73,
8.82, 15.42, 8.03, 7.7, 14.34, 7.19, 14.24, 17.21, 8.71, 12.06,
8.59, 15.09, 11.74, 14.47, 10.5, 13.62, 15.63, 15.03, 13.36,
7.21, 12.47, 13.72, 14.56, 8.04, 12.24, 15.52, 9.45, 9.27, 12.94,
12.73, 10.28, 15.89, 7.14, 16.17)), class = "data.frame",
row.names = c(NA,
-78L))

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 can I plot this data in 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")

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