annotation_logticks and facet plots in ggplot2 - r

This should be very simple. If I produce facet plots and I want to add the annotation_logticks, ticks appear in all the plots.
What if I want to have the ticks on only the first "columns" of the faceting? Is it possible?
For example, using the CO2 dataset:
structure(list(Plant = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 11L), .Label = c("Qn1", "Qn2", "Qn3", "Qc1", "Qc3", "Qc2",
"Mn3", "Mn2", "Mn1", "Mc2", "Mc3", "Mc1"), class = c("ordered",
"factor")), Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Quebec",
"Mississippi"), class = "factor"), Treatment = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("nonchilled", "chilled"), class = "factor"),
conc = c(95, 175, 250, 350, 500, 675, 1000, 95, 175, 250,
350, 500, 675, 1000, 95, 175, 250, 350, 500, 675, 1000, 95,
175, 250, 350, 500, 675, 1000, 95, 175, 250, 350, 500, 675,
1000, 95, 175, 250, 350, 500, 675, 1000, 95, 175, 250, 350,
500, 675, 1000, 95, 175, 250, 350, 500, 675, 1000, 95, 175,
250, 350, 500, 675, 1000, 95, 175, 250, 350, 500, 675, 1000,
95, 175, 250, 350, 500, 675, 1000, 95, 175, 250, 350, 500,
675, 1000), uptake = c(16, 30.4, 34.8, 37.2, 35.3, 39.2,
39.7, 13.6, 27.3, 37.1, 41.8, 40.6, 41.4, 44.3, 16.2, 32.4,
40.3, 42.1, 42.9, 43.9, 45.5, 14.2, 24.1, 30.3, 34.6, 32.5,
35.4, 38.7, 9.3, 27.3, 35, 38.8, 38.6, 37.5, 42.4, 15.1,
21, 38.1, 34, 38.9, 39.6, 41.4, 10.6, 19.2, 26.2, 30, 30.9,
32.4, 35.5, 12, 22, 30.6, 31.8, 32.4, 31.1, 31.5, 11.3, 19.4,
25.8, 27.9, 28.5, 28.1, 27.8, 10.5, 14.9, 18.1, 18.9, 19.5,
22.2, 21.9, 7.7, 11.4, 12.3, 13, 12.5, 13.7, 14.4, 10.6,
18, 17.9, 17.9, 17.9, 18.9, 19.9)), .Names = c("Plant", "Type",
"Treatment", "conc", "uptake"), row.names = 1:84, class = c("nfnGroupedData",
"nfGroupedData", "groupedData", "data.frame"), formula = uptake ~
conc | Plant, outer = ~Treatment * Type, labels = structure(list(
x = "Ambient carbon dioxide concentration", y = "CO2 uptake rate"), .Names = c("x",
"y")), units = structure(list(x = "(uL/L)", y = "(umol/m^2 s)"), .Names = c("x",
"y")))
With this small piece of code:
ggplot(data, aes(conc, uptake))+
geom_point()+
scale_y_log10(breaks=c(1,10,100))+
facet_grid(~Type)+
annotation_logticks(sides="l")

Given the example plot
p <- ggplot(mtcars, aes(disp, hp)) +
facet_wrap(~vs) +
geom_point() +
scale_y_log10()
you can use the following "hack" to have the ticks only for facets identified by vs=c(0)
a <- annotation_logticks(sides='l')
a$data <- data.frame(x=NA, vs=c(0))
p + a
Unfortunately, it provokes a warning.

Related

geom_text not matching group aes

I'm pretty sure this is a silly question, but I've been stuck with it for a while now. I want to match the geom_text() to my means from stat_summary according to My_group
the plot:
my code:
### getting model's predictions:
mod1 <- lmer(MY_CONT ~ YEAR * GROUP_2 + (1|ID), data = data, REML = FALSE)
###
data$predictions <- predict(mod1)
### put model's predictions in a sep df:
dfPred <- data %>% group_by(YEAR, MY_GROUP) %>% rstatix::get_summary_stats(predictions)
### check it:
dfPred %>% select(YEAR, MY_GROUP, variable, n, mean)
# A tibble: 4 x 5
YEAR MY_GROUP variable n mean
<fct> <fct> <chr> <dbl> <dbl>
1 A G1 predictions 21 17.6
2 A G2 predictions 21 18.5
3 B G1 predictions 21 18.8
4 B G2 predictions 21 19.1
### the model:
data %>%
mutate_if(is.numeric, round, 2) %>%
ggplot(., aes(x = YEAR, y = predictions)) +
stat_boxplot(aes(x = YEAR, fill = MY_GROUP), geom = "errorbar",
width = 0.15, position = position_dodge(.75)) +
geom_boxplot(aes(fill = MY_GROUP),
outlier.colour = "lightgrey",
outlier.shape = 19,
outlier.size= 2, notch = T) +
geom_text(data = dfPred,
aes(label = round(mean, 2),
y = round(mean, 2) + 0.8)) +
stat_summary(aes(group = MY_GROUP),
fun = mean, geom = "point",
shape = 20, size= 3, color= "black",
position = position_dodge(.75))
Warning message:
In stat_boxplot(aes(x = YEAR, fill = MY_GROUP), geom = "errorbar", :
Ignoring unknown aesthetics: fill
My Question:
How can I put the means' labels according to their group ? (MY_GROUP) ?
side question 1 : I get the fill warning, but it doesn't work without fill
side question 2 : The bars are showing the standard error aroung the means, right? not the 95%
data (and more details on the model and on how I obtained the above effects here):
data <- structure(list(PARTICIPANTS = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L,
10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 17L,
17L, 17L, 17L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 20L, 20L,
20L, 20L, 21L, 21L, 21L, 21L), CONT_Y = c(19.44, 20.07, 19.21,
16.35, 11.37, 12.82, 19.42, 18.94, 19.59, 20.01, 19.7, 17.92,
18.78, 19.21, 19.27, 18.46, 19.52, 20.02, 16.19, 19.97, 13.83,
15.93, 14.79, 21.55, 18.8, 19.42, 19.27, 19.37, 17.14, 14.45,
17.63, 20.01, 20.28, 17.93, 19.36, 20.15, 16.06, 17.04, 19.16,
20.1, 16.44, 18.39, 18.01, 19.05, 18.04, 19.69, 19.61, 16.88,
19.02, 20.42, 18.27, 18.43, 18.08, 17.1, 19.98, 19.43, 19.71,
19.93, 20.11, 18.41, 20.31, 20.1, 20.38, 20.29, 13.6, 18.92,
19.05, 19.13, 17.75, 19.15, 20.19, 18.3, 19.43, 19.8, 19.83,
19.53, 16.14, 21.14, 17.37, 18.73, 16.51, 17.51, 17.06, 19.42
), CATEGORIES = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("A",
"B"), class = "factor"), MY_GROUP = structure(c(1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), .Label = c("G1", "G2"), class = "factor")), row.names = c(NA,
-84L), class = c("tbl_df", "tbl", "data.frame"))
### rename column:
data <- data %>% rename(., YEAR = CATEGORIES)

Connecting points by levels of a variable

I have a dataset that I'm using in RStudio, and I have the code ggplot(desktop_2015) + geom_point(aes(Month, CV, color = Day), size = 2.5) in order to get a graph that I need.
I am plotting the variable CV by Month, and for each month there are 7 points along the vertical, each a different color representing a level of the variable Day.
What I am trying to do is connect the points for each day across the months, ie a line connecting each Friday point across the 12 months, and so on for each day of the week. I have attached images of what my dataset looks like in addition to the graph I currently have. TIA!
Here's the dput output of my dataset:
structure(list(Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("2015",
"2016", "2017", "2018", "2019"), class = "factor"), Quarter = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"),
Month = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L,
12L, 12L, 12L, 12L), .Label = c("Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = "factor"),
Device = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("D", "M", "T"), class = "factor"), Day = structure(c(4L,
2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L,
6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L,
7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L,
5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L,
1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L,
3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L), .Label = c("Friday", "Monday",
"Saturday", "Sunday", "Thursday", "Tuesday", "Wednesday"), class = "factor"),
Clicks = c(1479, 1631, 1471, 1382, 1926, 1724, 1928, 1233,
1380, 1164, 1145, 1187, 1082, 1201, 1927, 1825, 1592, 1232,
1225, 1181, 1320, 1437, 1357, 1487, 1769, 1655, 1256, 1318,
1512, 1508, 1358, 1176, 1111, 1364, 1316, 1441, 2131, 1956,
1455, 1431, 1280, 1288, 2106, 2326, 2109, 2474, 2397, 2200,
1721, 2598, 2767, 2112, 2045, 1997, 1771, 2352, 2075, 2441,
2670, 2543, 1973, 1876, 1920, 2206, 2529, 2134, 2000, 2514,
2551, 2758, 3087, 3219, 2314, 2150, 1906, 1997, 2335, 1957,
2272, 2617, 2489, 2199, 1657, 1945), Conversions = c(67,
95, 110, 101, 88, 105, 114, 89, 92, 79, 67, 72, 96, 76, 139,
125, 89, 47, 63, 73, 78, 97, 127, 69, 96, 61, 50, 90, 83,
91, 85, 56, 117, 66, 94, 48, 86, 71, 63, 53, 46, 56, 67,
75, 64, 64, 63, 55, 59, 74, 71, 62, 59, 57, 40, 71, 69, 84,
80, 101, 61, 76, 56, 93, 69, 50, 47, 73, 67, 98, 108, 127,
59, 67, 68, 88, 77, 60, 69, 82, 72, 55, 44, 54), CV = c(9089.21,
7811.24, 13201.19, 11394.8, 12631.15, 12389.61, 11742.6,
10265.62, 12449.76, 9329.68, 8255.08, 9002.71, 13173.41,
6235.05, 15480.72, 17940.65, 13667.19, 5766.98, 7583.03,
6817.59, 6412.43, 10441.66, 23018.46, 9243.69, 10521.5, 15117.06,
5791.93, 7783.52, 8156.31, 9996.18, 12973.64, 6329.24, 20080.53,
6289.64, 10891.91, 7176.93, 10281.64, 10292.1, 10077.85,
9299.89, 5979.86, 6888.64, 6799.56, 13162.34, 10267.85, 10599.65,
8868.4, 7285.48, 8393, 9930.09, 10857.6, 12568.96, 9998.93,
8465.09, 6733.55, 11107.85, 10919.87, 12933.21, 14653.55,
22648.43, 13272.86, 15214.25, 9733.4, 18128.61, 12915.65,
10267.21, 9804.48, 11928.58, 14135.84, 19990.35, 15482.84,
20116.57, 12705.79, 12891.93, 11266.43, 16632.9, 11890.34,
9290.67, 11417.62, 18980.21, 11025.63, 7806.93, 7246.12,
7737.87), `Conv. rate` = c(0.0453, 0.0582, 0.0748, 0.0731,
0.0457, 0.0609, 0.0591, 0.0722, 0.0667, 0.0679, 0.0585, 0.0607,
0.0887, 0.0633, 0.0721, 0.0685, 0.0559, 0.0381, 0.0514, 0.0618,
0.0591, 0.0675, 0.0936, 0.0464, 0.0543, 0.0369, 0.0398, 0.0683,
0.0549, 0.0603, 0.0626, 0.0476, 0.1053, 0.0484, 0.0714, 0.0333,
0.0404, 0.0363, 0.0433, 0.037, 0.0359, 0.0435, 0.0318, 0.0322,
0.0303, 0.0259, 0.0263, 0.025, 0.0343, 0.0285, 0.0257, 0.0294,
0.0289, 0.0285, 0.0226, 0.0302, 0.0333, 0.0344, 0.03, 0.0397,
0.0309, 0.0405, 0.0292, 0.0422, 0.0273, 0.0234, 0.0235, 0.029,
0.0263, 0.0355, 0.035, 0.0395, 0.0255, 0.0312, 0.0357, 0.0441,
0.033, 0.0307, 0.0304, 0.0313, 0.0289, 0.025, 0.0266, 0.0278
), `CV/Click` = c(6.15, 4.79, 8.97, 8.25, 6.56, 7.19, 6.09,
8.33, 9.02, 8.02, 7.21, 7.58, 12.18, 5.19, 8.03, 9.83, 8.58,
4.68, 6.19, 5.77, 4.86, 7.27, 16.96, 6.22, 5.95, 9.13, 4.61,
5.91, 5.39, 6.63, 9.55, 5.38, 18.07, 4.61, 8.28, 4.98, 4.82,
5.26, 6.93, 6.5, 4.67, 5.35, 3.23, 5.66, 4.87, 4.28, 3.7,
3.31, 4.88, 3.82, 3.92, 5.95, 4.89, 4.24, 3.8, 4.72, 5.26,
5.3, 5.49, 8.91, 6.73, 8.11, 5.07, 8.22, 5.11, 4.81, 4.9,
4.74, 5.54, 7.25, 5.02, 6.25, 5.49, 6, 5.91, 8.33, 5.09,
4.75, 5.03, 7.25, 4.43, 3.55, 4.37, 3.98), Impressions = c(86045,
89512, 81503, 81356, 101254, 95972, 100790, 73492, 81709,
71678, 67884, 68429, 61978, 69537, 99440, 99735, 95689, 71773,
71414, 65363, 69422, 77640, 76419, 81980, 97540, 90953, 67780,
68886, 81265, 79079, 70807, 65774, 59298, 72504, 71965, 92817,
132684, 120931, 93380, 89791, 82604, 79651, 121598, 141042,
132627, 167622, 146056, 133295, 103366, 151998, 170043, 142676,
126557, 121835, 121060, 139303, 113975, 127019, 151171, 140981,
110230, 108527, 106218, 123960, 136940, 123136, 120845, 145673,
136340, 144527, 185146, 210133, 157902, 135150, 124981, 132650,
136682, 127909, 156160, 219576, 187283, 143617, 107303, 128768
), Cost = c(1376.23, 1799.57, 1646.93, 1631.22, 2088.67,
1869.83, 1779.56, 1152.91, 1643.25, 1281.38, 1368.1, 1299.16,
1184.99, 1183.82, 1690.38, 2065.43, 1737.26, 1351.85, 1432.21,
1395.46, 1192.53, 1385.88, 1548.41, 1754.96, 2148.9, 2061.52,
1481.82, 1400.12, 1595.65, 1808.54, 1643.06, 1417.31, 1343.52,
1794.69, 1317.59, 1436.56, 2344.1, 2124.41, 1602.12, 1449.17,
1417.73, 1337.39, 1773.49, 2018.75, 1813.7, 2181.56, 2069.48,
1938.4, 1528.46, 1907.15, 2163.95, 1645.47, 1620.2, 1552.78,
1326.68, 1749.51, 1466.75, 1851.91, 1997.14, 1909.85, 1506.9,
1391.86, 1420.54, 1671.03, 1948.89, 1657.35, 1577.12, 1888.6,
1934.2, 2055.61, 2357.6, 2426.16, 1730.51, 1652.82, 1464.03,
1550.73, 1736.98, 1364.01, 1625.97, 1835.38, 1714.8, 1584.55,
1109.67, 1340.77)), row.names = c(2L, 4L, 7L, 12L, 15L, 18L,
19L, 23L, 26L, 28L, 31L, 36L, 38L, 40L, 44L, 47L, 51L, 52L, 57L,
58L, 63L, 64L, 69L, 72L, 74L, 78L, 81L, 82L, 85L, 89L, 92L, 95L,
97L, 100L, 105L, 107L, 111L, 113L, 116L, 119L, 121L, 124L, 127L,
130L, 135L, 136L, 141L, 142L, 147L, 149L, 152L, 154L, 158L, 161L,
163L, 167L, 171L, 174L, 177L, 178L, 181L, 185L, 188L, 191L, 194L,
198L, 201L, 202L, 207L, 208L, 211L, 215L, 218L, 221L, 225L, 228L,
230L, 232L, 236L, 238L, 242L, 246L, 247L, 250L), class = "data.frame")
I think you need to provide both group and color in your aes:
library(ggplot2)
ggplot(df, aes(x = Month, y = CV, color = Day, group = Day))+
geom_point()+
geom_line()

Agglomerative hierachial clustering using R

Plotting a dendogram from a agglomerative hierachial clustering does not yield the expected results. I have attached the example of the expected output in the image here . The y axis shows the treatment groups.
My MWE is
library(cluster)
dist<-daisy(cluster, metric = "gower")
kaari <-hclust(dist, method = "ward.D2")
plot(kaari,cex = 0.6, hang = -1)
Here is the data frame:
structure(list(Variety = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,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, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L,
4L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L,
4L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L,
4L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("% fruit damage",
"Intermediate", "Lower", "Upper"), class = "factor"), overall = c(8.33,
15.83, 18.33, 18.33, 16.67, 15.83, 17.5, 15, 14.17, 16.67, 15,
18.33, 6.67, 14.17, 6.67, 15.83, 10, 12.5, 10, 15, 35, 55, 50,
25, 12.5, 11.67, 12.5, 13.33, 15.83, 13.33, 14.17, 10, 11.67,
15.83, 8.33, 10.83, 7.5, 7.5, 10.83, 9.17, 5.83, 5.83, 10, 17.5,
20, 12.5, 20, 5, 18.33, 15, 15, 12.5, 10, 15.83, 20.83, 15.83,
18.33, 10, 11.67, 18.33, 10.83, 6.67, 7.5, 14.17, 6.67, 10.83,
37.5, 17.5, 25, 15, 30, 20, 24.17, 22.5, 16.67, 19.17, 14.17,
24.17, 26.67, 20.83, 16.67, 17.5, 14.17, 20, 12.5, 20.83, 11.67,
6.67, 12.5, 11.67, 55, 55, 55, 60, 55, 57.5, 24.17, 28.33, 19.17,
21.67, 20, 18.33, 24.17, 20.83, 17.5, 15, 16.67, 15, 15, 10.83,
11.67, 16.67, 14.17, 10, 30, 45, 55, 42.5, 55, 37.5, 33.33, 20.83,
20, 17.5, 18.33, 20, 28.33, 13.33, 17.5, 13.33, 20.83, 11.67,
11.67, 10.83, 13.33, 8.33, 8.33, 13.33, 55, 40, 55, 52.5, 45,
45, 12.5, 17.5, 15, 21.67, 17.5, 17.5, 14.17, 14.17, 16.67, 14.17,
19.17, 15, 10.83, 13.33, 6.67, 9.17, 8.33, 13.33, 45, 50, 40,
35, 55, 45, 10.83, 9.17, 23.33, 22.5, 15.83, 11.67, 26.67, 8.33,
20, 12.5, 10.83, 18.33, 9.17, 7.5, 9.17, 7.5, 5.83, 13.33, 37.5,
35, 45, 22.5, 30, 25, 15, 13.33, 20, 13.33, 20, 20, 9.17, 21.67,
12.5, 10, 14.17, 24.17, 10.83, 10, 13.33, 9.17, 11.67, 10.83,
45, 45, 42.5, 30, 55, 40, 11.67, 21.67, 18.33, 16.67, 16.67,
16.67, 14.17, 15, 15.83, 20.83, 12.5, 16.67, 10, 12.5, 9.17,
10, 7.5, 6.67, 27.5, 30, 32.5, 45, 17.5, 25, 15.83, 15.83, 17.5,
13.33, 12.5, 13.33, 13.33, 10.83, 19.17, 12.5, 13.33, 12.5, 7.5,
8.33, 9.17, 5.83, 10.83, 10.83, 47.5, 15, 20, 20, 30, 30, 10,
18.33, 12.5, 11.67, 10.83, 13.33, 13.33, 12.5, 10, 10, 13.33,
15, 6.67, 14.17, 7.5, 7.5, 10.83, 7.5, 22.5, 15, 22.5, 20, 25,
15)), .Names = c("Variety", "Sample.Part", "overall"), class = "data.frame", row.names = c(NA,
-288L))
My first and second columns in my data set are categorical while the third is numeric, I have attached the the data here.
Variety Sample.Part overall
Cal J Lower 8.33
Cal J Lower 15.83
Cal J Lower 18.33
Cal J Lower 18.33
Cal J Lower 16.67
Cal J Lower 15.83
Cal J Intermediate 17.50
Cal J Intermediate 15.00
Cal J Intermediate 14.17
Cal J Intermediate 16.67
Cal J Intermediate 15.00
Cal J Intermediate 18.33
Cal J Upper 6.67
Cal J Upper 14.17
Cal J Upper 6.67
Cal J Upper 15.83
Cal J Upper 10.00
Cal J Upper 12.50
Cal J % fruit damage 10.00
Cal J % fruit damage 15.00
Cal J % fruit damage 35.00
Cal J % fruit damage 55.00
Cal J % fruit damage 50.00
I would like to have the factor levels in the first column appear as leaf nodes in the y axis. Any help?

Passing an external function (and arguments) to dplyr summarize or mutate

I am trying to summarize a large set of data with an external function (sii package).
What I need to do is calculate SII for each subject, with each system, at each presentation level.
Example data:
data <- structure(list(Subject = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 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, 3L, 1L, 1L, 2L, 2L,
3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L,
1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L,
3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L,
1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L,
3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("55", "65", "75"
), class = "factor"), Frequency = c(125, 125, 125, 125, 125,
125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125,
125, 125, 125, 125, 125, 125, 125, 160, 160, 160, 160, 160,
160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160,
160, 160, 160, 160, 160, 160, 160, 200, 200, 200, 200, 200,
200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200,
200, 200, 200, 200, 200, 200, 200, 250, 250, 250, 250, 250,
250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250,
250, 250, 250, 250, 250, 250, 250, 315, 315, 315, 315, 315,
315, 315, 315, 315, 315, 315, 315, 315, 315, 315, 315, 315,
315, 315, 315, 315, 315, 315, 315, 400, 400, 400, 400, 400,
400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400,
400, 400, 400, 400, 400, 400, 400, 500, 500, 500, 500, 500,
500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
500, 500, 500, 500, 500, 500, 500, 630, 630, 630, 630, 630,
630, 630, 630, 630, 630, 630, 630, 630, 630, 630, 630, 630,
630, 630, 630, 630, 630, 630, 630, 800, 800, 800, 800, 800,
800, 800, 800, 800, 800, 800, 800, 800, 800, 800, 800, 800,
800, 800, 800, 800, 800, 800, 800, 1000, 1000, 1000, 1000,
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000,
1250, 1250, 1250, 1250, 1250, 1250, 1250, 1250, 1250, 1250,
1250, 1250, 1250, 1250, 1250, 1250, 1250, 1250, 1250, 1250,
1250, 1250, 1250, 1250, 1600, 1600, 1600, 1600, 1600, 1600,
1600, 1600, 1600, 1600, 1600, 1600, 1600, 1600, 1600, 1600,
1600, 1600, 1600, 1600, 1600, 1600, 1600, 1600, 2000, 2000,
2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000,
2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000,
2000, 2000, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,
2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,
2500, 2500, 2500, 2500, 2500, 2500, 3000, 3000, 3000, 3000,
3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000,
3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000,
3150, 3150, 3150, 3150, 3150, 3150, 3150, 3150, 3150, 3150,
3150, 3150, 3150, 3150, 3150, 3150, 3150, 3150, 3150, 3150,
3150, 3150, 3150, 3150, 4000, 4000, 4000, 4000, 4000, 4000,
4000, 4000, 4000, 4000, 4000, 4000, 4000, 4000, 4000, 4000,
4000, 4000, 4000, 4000, 4000, 4000, 4000, 4000, 5000, 5000,
5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000,
5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000,
5000, 5000, 6000, 6000, 6000, 6000, 6000, 6000, 6000, 6000,
6000, 6000, 6000, 6000, 6000, 6000, 6000, 6000, 6000, 6000,
6000, 6000, 6000, 6000, 6000, 6000, 6300, 6300, 6300, 6300,
6300, 6300, 6300, 6300, 6300, 6300, 6300, 6300, 6300, 6300,
6300, 6300, 6300, 6300, 6300, 6300, 6300, 6300, 6300, 6300,
8000, 8000, 8000, 8000, 8000, 8000, 8000, 8000, 8000, 8000,
8000, 8000, 8000, 8000, 8000, 8000, 8000, 8000, 8000, 8000,
8000, 8000, 8000, 8000), REM_SPL = c(43.68, 38.85, 51.43,
48.71, 59.22, 58.62, 38.51, 38.45, 48.33, 48.44, 58.18, 58.34,
52.51, 39.6, 58.89, 49.06, 64.63, 58.7, 40.42, 38.81, 49.03,
48.52, 58.3, 58.31, 54.92, 49.44, 62.59, 59.24, 70.32, 68.97,
48.43, 48.19, 58.21, 58.16, 68.17, 67.99, 63.83, 51.47, 68.49,
59.93, 73.25, 69.09, 48.69, 48.3, 58.35, 58.1, 68.19, 68.01,
60.9, 50.42, 68.57, 61.23, 76.59, 71.69, 53.6, 52.7, 63.7,
62.63, 73.63, 72.59, 69.31, 50.73, 73.51, 60.53, 78.59, 71.52,
52.12, 51.55, 62.76, 61.94, 73.15, 72.33, 60.74, 57.26, 68.37,
66.47, 76.5, 75.78, 52.44, 49.21, 62.49, 58.96, 72.44, 69.11,
68.65, 60.55, 72.73, 67.42, 78.12, 75.97, 50.62, 58.85, 58.9,
61.92, 70.33, 68.8, 55.39, 50.46, 62.96, 59.28, 71.08, 68.47,
48.88, 61.78, 58.9, 71.68, 68.8, 80.89, 64.34, 56.79, 68.32,
61.46, 73.3, 68.84, 62.02, 68.72, 68.09, 74.56, 73.49, 80.8,
56.69, 59.3, 64.04, 67.76, 72.05, 76.68, 51.93, 69.98, 61.96,
79.52, 71.84, 88.99, 67, 65.08, 70.76, 70.07, 75.18, 77.28,
71.03, 77.65, 75.63, 82.24, 79.81, 88.9, 51.92, 57.93, 59.21,
66.1, 67.46, 75.01, 56.59, 68.41, 65.69, 77.82, 75.5, 87.89,
63.7, 64.53, 67.67, 69.39, 71.81, 76.13, 69.27, 76.85, 73.64,
81.51, 78.2, 87.85, 48.87, 53.9, 55.78, 61.65, 63.82, 70.4,
58.82, 65.38, 67.34, 74.02, 76.93, 83.84, 61.42, 61.24, 65.35,
65.97, 69.53, 72.23, 68.71, 74.23, 73.86, 79.7, 79.37, 85.62,
48.01, 50.44, 54.41, 57.41, 61.56, 65.25, 55.58, 60.89, 63.1,
68.84, 71.74, 77.46, 60.05, 58.6, 63.73, 62.9, 67.59, 68.1,
66.15, 68.8, 70.82, 74.09, 76.38, 79.82, 47.18, 48.5, 53.45,
55.01, 60.08, 61.96, 50.95, 55.74, 57.98, 63.25, 65.43, 71.02,
59.17, 56.77, 63.17, 61.08, 67.06, 65.64, 62.25, 64.22, 66.68,
69.38, 71.26, 74.48, 45.35, 46.41, 51.51, 52.74, 57.89, 59.15,
49.19, 51.76, 55.54, 58.63, 61.76, 65.99, 57.58, 54.92, 61.84,
59.05, 65.64, 63.46, 61.35, 60.78, 64.83, 65.47, 68.72, 69.57,
46.65, 47.33, 51.94, 53.13, 57.31, 59, 49.36, 51.67, 55.5,
57.69, 60.82, 63.09, 60.43, 56.86, 64.43, 60.78, 68.14, 64.65,
64.16, 60.72, 66.77, 64.85, 70.22, 68.47, 52.05, 52.4, 57.11,
57.74, 62.04, 63.19, 54.05, 54.49, 58.79, 59.71, 62.52, 63.23,
66.43, 61.57, 70.32, 65.49, 73.94, 69.19, 68.14, 62.49, 70.3,
66.13, 72.44, 68.77, 58.81, 58.52, 63.77, 63.85, 68.05, 68.88,
60.06, 60.46, 64.5, 64.38, 67.55, 67.18, 70.69, 66.81, 74.87,
70.92, 78.38, 74.41, 72.09, 66.25, 74.33, 69.39, 76.22, 71.49,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 60.18, 59.65, 64.79,
64.88, 68.88, 70.03, 67.33, 66.6, 71.46, 71.19, 73.76, 73.17,
69.65, 67.08, 73.98, 71.05, 77.77, 74.67, 75.44, 69.74, 78.49,
73.39, 79.65, 75.22, 58.4, 59.01, 63.43, 64.86, 67.39, 69.91,
68.71, 66.86, 73.05, 75.1, 74.7, 76.83, 65.9, 65.64, 71.34,
70.31, 75.86, 73.99, 71.73, 67.53, 77.7, 75.64, 78.59, 77.33,
58.44, 58.86, 63.4, 64.32, 67.55, 69.36, 66.91, 66.78, 71.71,
75.09, 73.4, 76.7, 66.48, 64.59, 71.47, 68.87, 75.38, 72.33,
68.31, 66.76, 75.47, 75.42, 76.96, 76.82, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 55.72, 55.8, 60.74, 61.53, 65.31, 66.82,
67.76, 66.76, 71.6, 72.51, 73.49, 74.22, 63.62, 62.1, 68.72,
66.45, 72.44, 69.93, 68.97, 67.19, 75.25, 73, 76.63, 74.49,
52.18, 51.25, 57.97, 57.54, 62.94, 63.07, 67.82, 67.65, 71.4,
72, 73.4, 73.92, 60.18, 58.02, 66.23, 63.48, 70.87, 67.6,
68.92, 68.42, 75.79, 72.7, 77.23, 74.33), Thresh_SPL = c(40,
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 65, 60, 65, 60,
65, 60, 65, 60, 65, 60, 65, 60, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 33, 33, 33, 33,
33, 33, 33, 33, 33, 33, 33, 33, 58, 53, 58, 53, 58, 53, 58,
53, 58, 53, 58, 53, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 34.6, 34.6, 34.6, 34.6, 34.6,
34.6, 34.6, 34.6, 34.6, 34.6, 34.6, 34.6, 59.6, 54.6, 59.6,
54.6, 59.6, 54.6, 59.6, 54.6, 59.6, 54.6, 59.6, 54.6, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 35, 30, 35, 30, 35, 30, 35, 30, 35, 30, 35, 30, 50,
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 44.6,
44.6, 44.6, 44.6, 44.6, 44.6, 44.6, 44.6, 44.6, 44.6, 44.6,
44.6, 69.6, 59.6, 69.6, 59.6, 69.6, 59.6, 69.6, 59.6, 69.6,
59.6, 69.6, 59.6, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 72.5,
77.5, 72.5, 77.5, 72.5, 77.5, 72.5, 77.5, 72.5, 77.5, 72.5,
77.5, 87.5, 77.5, 87.5, 77.5, 87.5, 77.5, 87.5, 77.5, 87.5,
77.5, 87.5, 77.5, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 76.5,
81.5, 76.5, 81.5, 76.5, 81.5, 76.5, 81.5, 76.5, 81.5, 76.5,
81.5, 96.5, 81.5, 96.5, 81.5, 96.5, 81.5, 96.5, 81.5, 96.5,
81.5, 96.5, 81.5, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 77.5,
82.5, 77.5, 82.5, 77.5, 82.5, 77.5, 82.5, 77.5, 82.5, 77.5,
82.5, 82.5, 77.5, 82.5, 77.5, 82.5, 77.5, 82.5, 77.5, 82.5,
77.5, 82.5, 77.5, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 94,
104, 94, 104, 94, 104, 94, 104, 94, 104, 94, 104, 104, 89,
104, 89, 104, 89, 104, 89, 104, 89, 104, 89)), row.names = c(NA,
-504L), class = "data.frame")
The sii function takes several arguments:
sii(speech = speech, threshold = threshold, freq = frequency, method = "one-third octave", interpolate = T)
I want to fix the freq argument as:
freq = c(125, 250, 500, 1000, 2000, 3150, 4000, 6300, 8000)
One particularly difficult part is that I need to subset the speech and threshold arguments on slightly different values of Frequency:
For speech: c(125, 250, 500, 1000, 2000, 3150, 4000, 6300, 8000)
For threshold: c(125, 250, 500, 1000, 2000, 3000, 4000, 6000, 8000)
The other arguments need to be calculated based on the grouping. What I have tried so far doesn't work:
library(tidyverse)
library(sii)
data %>%
group_by(Subject, Ear, System, Pres_Level) %>%
summarize(SII = sii(speech = . %>%
filter(Frequency %in% c(125, 250, 500, 1000, 2000, 3150, 4000, 6300, 8000)) %>%
pull(REM_SPL),
threshold = . %>%
filter(Frequency %in% c(125, 250, 500, 1000, 2000, 3000, 4000, 6000, 8000)) %>%
pull(Thresh_SPL),
freq = frequency, method = "one-third octave", interpolate = T))
Error in sii(speech = . %>% filter(Frequency %in% c(125, 250, 500, 1000, :
`speech' must have the same length as `freq'.
Trying to maintain grouping for the arguments:
data %>%
select(-REM_Level) %>%
filter(Frequency >= 125, Frequency <= 8000) %>%
group_by(Subject, Ear, System, Pres_Level) %>%
mutate(Speech = tibble(REM_SPL) %>%
filter(Frequency %in% c(125, 250, 500, 1000, 2000, 3150, 4000, 6300, 8000))) %>%
pull(REM_SPL)),
threshold = tibble(Thresh_SPL) %>%
filter(Frequency %in% c(125, 250, 500, 1000, 2000, 3000, 4000, 6000, 8000) %>%
pull(Thresh_SPL))) %>%
mutate(SII = sii(speech = speech, threshold = threshold, freq = c(125, 250, 500, 1000, 2000, 3000, 4000, 6000, 8000),
method = "one-third octave", interpolate = T))
Error in mutate_impl(.data, dots) :
Column `Speech` is of unsupported class data.frame
I have attempted using some nested loops, but that hasn't worked at all.
My desired output is something like this (these are fake SII values):
Subject System Pres_Level SII
1 1 A 55 0.65
2 1 B 55 0.60
3 1 C 55 0.60
4 1 A 65 0.70
5 1 B 65 0.75
6 1 C 65 0.80
7 1 A 75 0.76
8 1 B 75 0.78
9 1 C 75 0.74
10 2 A 55 0.55
11 2 B 55 0.58
12 2 C 55 0.57
13 2 A 65 0.74
14 2 B 65 0.72
15 2 C 65 0.82
16 2 A 75 0.80
17 2 B 75 0.82
18 2 C 75 0.76
19 3 A 55 0.58
20 3 B 55 0.62
21 3 C 55 0.64
22 3 A 65 0.74
23 3 B 65 0.76
24 3 C 65 0.78
25 3 A 75 0.80
26 3 B 75 0.76
27 3 C 75 0.74
Can anyone suggest how I might achieve what I'm looking for?
I believe this is what you're looking for... One thing you need to look out for is what sii returns (an object of length 10 and not a vector length 1). You need to further extract the SII value from the result, hence sii(...)$sii in the summarize call.
After edit with the new data:
data %>%
group_by(Subject, Ear, System, Pres_Level) %>%
summarize(SII = sii(speech = REM_SPL[Frequency %in% c(125, 250, 500, 1000, 2000, 3150, 4000, 6300, 8000)],
threshold = Thresh_SPL[Frequency %in% c(125, 250, 500, 1000, 2000, 3000, 4000, 6000, 8000)],
freq = Frequency[Frequency %in% c(125, 250, 500, 1000, 2000, 3150, 4000, 6300, 8000)],
method = "one-third octave",
interpolate = T)$sii)
# A tibble: 24 x 5
# Groups: Subject, Ear, System [?]
Subject Ear System Pres_Level SII
<fct> <fct> <fct> <fct> <dbl>
1 1 Left A 55 0.788
2 1 Left A 65 0.782
3 1 Left A 75 0.759
4 1 Left B 55 0.806
5 1 Left B 65 0.774
6 1 Left B 75 0.742
7 1 Right A 55 0.749
8 1 Right A 65 0.749
9 1 Right A 75 0.737
10 1 Right B 55 0.765
# ... with 14 more rows

Multi level pie chart ggplot: Label overlap and legend

I have the following dataset:
data <- structure(list(Year = structure(c(1L, 2L, 1L, 2L, 2L, 1L, 3L, 1L, 3L, 1L, 2L, 2L, 3L, 2L, 3L,
1L, 3L, 2L, 2L, 2L, 3L, 1L, 2L, 3L, 3L, 3L, 2L, 1L, 3L, 1L,
1L, 2L, 1L, 2L, 3L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 3L,
3L, 2L, 3L, 2L, 1L, 1L, 2L, 2L, 1L),
.Label = c("2013", "2014", "2015"),
class = "factor"),
Place = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L),
.Label = c("Inside", "Outside"),
class = "factor"),
Variable = structure(c(6L, 4L, 8L, 13L, 16L, 11L, 12L, 13L, 4L, 10L, 10L, 11L,
1L, 3L, 13L, 7L, 11L, 7L, 6L, 2L, 6L, 1L, 1L, 7L, 5L,
3L, 14L, 3L, 14L, 2L, 9L, 6L, 6L, 9L, 2L, 5L, 9L, 5L,
9L, 9L, 15L, 1L, 13L, 3L, 6L, 3L, 3L, 9L, 15L, 1L, 13L,
1L, 13L, 15L),
.Label = c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8",
"Y1", "Y2", "Y3", "Y4", "Y5", "Y6", "Y7", "Y8"),
class = "factor"),
Group = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L,
2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L),
.Label = c("Var1", "Var2"),
class = "factor"),
Percent = c(0.2, 0.3, 0.4, 0.5, 0.5, 0.6, 0.7, 0.7, 1.3, 1.6, 1.9, 1.9, 2.3, 2.7,
2.9, 3.4, 3.7, 4.5, 4.7, 5.3, 5.7, 6.2, 7.6, 7.9, 10.6, 10.7, 12.5,
13.3, 14.4, 15.4, 15.8, 16.9, 17.7, 19.6, 20.5, 24.8, 25.3, 30.4, 31,
36.8, 41.6, 43.9, 43.9, 44.2, 45.4, 51.8, 52.8, 56.1, 57.4, 68.9, 68.9,
80.4, 80.4, 81.5)),
class = "data.frame", row.names = c(NA, -54L))
I would really like to display the data in a a multilevel like this:
I tried it by doing:
library(ggplot2)
ggplot(data, aes(x = Group, y = Percent, fill = Variable)) +
geom_bar(stat = "identity", position = "fill") +
facet_grid(Year ~ Place) +
geom_text(aes(label= paste(Percent, "%", Variable)) ,
position = position_fill(0.9), size = 3)+
coord_polar(theta = "y")
But because some percentages are very low, the layers overlap. I would like to either place the labels outside like the example if that's possible.
I have looked at the other forum topics, but because my data is structured in a different way I wasn't able to translate that for me. The other problem is, is that this is just an example but my data input is actually dependent on he Shiny input. So fixing specific angles for this example also doesn't work.
I would be very grateful if anyone could help me.

Resources