Save graphs (facet) on multiple pages - r

I need to represent an important number of graphs. To do that I used a the facet_wrap_paginate function, but I only have the second page printed/plotted.
Here is a sample of my data:
df <- structure(list(oxygen = c(60.86414, 62.50372, 89.76055, 61.15914,
61.49936, 61.90402, 61.60323, 61.2873, 75.12329, 60.83503, 63.03796,
61.78298, 70.16533, 62.60887, 61.49043, 64.42095, 61.78358, 62.29302,
62.88122, 60.47264, 65.92805, 65.0235, 64.32414, 65.56869, 64.65729,
66.73502, 67.01285, 67.47757, 69.62232, 66.09624, 66.18106, 64.28226,
64.85658, 67.46348, 68.93226, 65.23919, 65.67461, 64.64727, 64.90544,
68.10878, 70.00292, 59.54631, 61.43627, 78.75032, 61.94263, 82.08963,
62.75281, 61.93749, 72.86382, 62.06785, 61.04865, 95.49206, 69.79339,
66.82354, 61.53702, 85.51962, 101.53748, 61.64096, 92.14781,
60.41892, 77.55303, 75.39007, 62.15703, 82.38292, 87.27823, 69.28644,
73.72369, 62.02667, 61.34973, 61.61716, 70.61777, 61.59655, 60.31008,
61.65777, 71.82138, 61.9823, 79.38791, 60.31816, 108.2866, 61.54543,
69.87043, 64.30304, 65.05726, 68.69304, 84.0604, 121.05592, 68.67872,
69.53102, 68.31013, 64.76675, 122.22451, 91.29598, 69.43838,
68.73515, 77.99483, 68.56142, 64.14964, 73.2695, 69.67608, 69.01853,
68.5685, 63.9001, 68.93557, 71.84125, 72.73052, 69.17045, 76.28085,
68.41978, 69.53005, 76.15901, 86.43705, 72.95997, 64.0582, 70.29805,
64.10386, 88.09503, 68.07687, 69.62147, 67.77802, 125.09757,
91.96934, 97.26108, 89.16293, 80.245, 124.97964, 97.28237, 95.95074,
99.24807, 82.59685, 89.2217, 77.50349, 88.54909, 91.61343, 77.38885,
94.06567, 84.77561, 80.46941, 92.28428, 74.25416, 97.07623),
depth = c(707.5, 195, 47.5, 750, 315.5, 506.5, 660.5, 678,
146, 821.5, 197, 365, 141.5, 448, 574.5, 325.5, 645.5, 236,
446, 872.5, 315, 729.5, 786.5, 573.5, 752, 202, 455, 412,
147, 552.5, 517, 904, 339.5, 184, 308.5, 307.5, 610, 705.5,
700, 375, 228.5, 261.5, 398.5, 168.5, 360, 136.5, 504, 299.5,
187, 473.5, 249.5, 64.5, 178.5, 251.5, 369.5, 95, 66.5, 599.5,
72, 474, 178, 33.5, 109, 86.5, 26.5, 240, 51.5, 367, 295.5,
408.5, 244.5, 106, 314, 304.5, 69.5, 361.5, 91.5, 125.5,
11, 281.5, 101.5, 472.5, 433, 257, 24, 6, 192.5, 265, 226,
449, 1.5, 21.5, 362.5, 238.5, 33, 246, 477.5, 46, 109, 315,
275.5, 636, 284.5, 267, 387, 336.5, 225.5, 291, 205, 136,
60.5, 171, 515.5, 260.5, 563, 50, 436, 123, 227, 7.5, 12,
87, 32.5, 239, 14.5, 103.5, 90.5, 120.5, 38.5, 36.5, 406,
38.5, 143.5, 371, 87, 54.5, 234, 53.5, 260.5, 123), ctd_file = c("BA16007",
"BA16002", "BA16011", "BA16003", "BA16004", "BA16006", "BA16001",
"BA16002", "BA16012", "BA16003", "BA16004", "BA16004", "BA16005",
"BA16004", "BA16009", "BA16005", "BA16012", "BA16004", "BA16006",
"BA16003", "BA16013", "BA16014", "BA16017", "BA16016", "BA16018",
"BA16016", "BA16014", "BA16020", "BA16020", "BA16020", "BA16016",
"BA16018", "BA16016", "BA16018", "BA16021", "BA16015", "BA16014",
"BA16013", "BA16015", "BA16021", "BA16025", "BA16023", "BA16024",
"BA16025", "BA16024", "BA16025", "BA16025", "BA16025", "BA16024",
"BA16025", "BA16023", "BA16025", "BA16023", "BA16025", "BA16024",
"BA16022", "BA16022", "BA16025", "BA16024", "BA16024", "BA16026",
"BA16035", "BA16030", "BA16035", "BA16031", "BA16026", "BA16032",
"BA16031", "BA16027", "BA16032", "BA16035", "BA16028", "BA16031",
"BA16027", "BA16033", "BA16032", "BA16035", "BA16028", "BA16027",
"BA16032", "BA16034", "BA16034", "BA16034", "BA16034", "BA16034",
"BA16034", "BA16034", "BA16034", "BA16034", "BA16034", "BA16034",
"BA16034", "BA16034", "BA16034", "BA16034", "BA16034", "BA16034",
"BA16034", "BA16034", "BA16034", "BA16040", "BA16036", "BA16043",
"BA16047", "BA16048", "BA16041", "BA16049", "BA16036", "BA16042",
"BA16044", "BA16048", "BA16045", "BA16039", "BA16046", "BA16036",
"BA16048", "BA16044", "BA16038", "BA16041", "BA16045", "BA16052",
"BA16055", "BA16054", "BA16054", "BA16053", "BA16053", "BA16053",
"BA16055", "BA16055", "BA16053", "BA16055", "BA16054", "BA16052",
"BA16055", "BA16054", "BA16053", "BA16054", "BA16055", "BA16052",
"BA16053"), Transect = structure(c(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, 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, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("1",
"2", "3", "4", "5", "6", "7"), class = "factor"), Station = structure(c(7L,
2L, 11L, 3L, 4L, 6L, 1L, 2L, 12L, 3L, 4L, 4L, 5L, 4L, 9L,
5L, 12L, 4L, 6L, 3L, 1L, 2L, 5L, 4L, 6L, 4L, 2L, 8L, 8L,
8L, 4L, 6L, 4L, 6L, 9L, 3L, 2L, 1L, 3L, 9L, 4L, 2L, 3L, 4L,
3L, 4L, 4L, 4L, 3L, 4L, 2L, 4L, 2L, 4L, 3L, 1L, 1L, 4L, 3L,
3L, 1L, 9L, 5L, 9L, 6L, 1L, 7L, 6L, 2L, 7L, 9L, 3L, 6L, 2L,
8L, 7L, 9L, 3L, 2L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 4L, 8L,
9L, 3L, 10L, 1L, 4L, 5L, 9L, 6L, 1L, 7L, 1L, 9L, 5L, 1L,
3L, 6L, 3L, 6L, 5L, 5L, 4L, 4L, 4L, 6L, 6L, 4L, 6L, 5L, 3L,
6L, 5L, 4L, 5L, 6L, 3L, 4L), .Label = c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", "12"), class = "factor")), row.names = c(NA,
-140L), groups = structure(list(Transect = structure(1:7, .Label = c("1",
"2", "3", "4", "5", "6", "7"), class = "factor"), .rows = structure(list(
1:20, 21:40, 41:60, 61:80, 81:100, 101:120, 121:140), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -7L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
And there is the code I used:
library(dplyr)
library(tidyverse)
library(ggforce)
i <- ceiling(
length(levels(df$Transect)) / 4) # set the number of pages
pdf("multi_page.pdf", width = 16 / 2.54, height = 12 / 2.54)
SdesGG <- df %>% #launch each time or does not work
group_by(Transect) %>% #mandatory or need to fortify
ggplot(aes(x = oxygen, y = depth, color = Station)) +
geom_line() +
scale_color_brewer(palette = "Paired") +
scale_y_reverse() +
facet_wrap_paginate(~Transect, ncol = 2, nrow = 2, page = i) #ggforce
dev.off()
I have 180 000 observations, therefore it might be interesting to duplicate the number of row
lapply(df, rep, 1000)

With the page argument you only specify
The page to draw (see ?facet_wrap_paginate)
That's why you get only the last or second page with page = i.
If you want all pages you have to loop over the pages:
library(ggplot2
library(ggforce)
i <- ceiling(
length(levels(df$Transect)) / 4) # set the number of pages
pdf("multi_page.pdf", width = 16 / 2.54, height = 12 / 2.54)
lapply(seq(i), function(page) {
SdesGG <- df %>% #launch each time or does not work
group_by(Transect) %>% #mandatory or need to fortify
ggplot(aes(x = oxygen, y = depth, color = Station)) +
geom_line() +
scale_color_brewer(palette = "Paired") +
scale_y_reverse() +
facet_wrap_paginate(~Transect, ncol = 2, nrow = 2, page = page) #ggforce
})
dev.off()
#> [[1]]
#>
#> [[2]]

Related

Boxplot colour mismatch

I would like to color the dots of the plot with the legend Episode values. What I am missing?
I replaced fill with color still is not the plot that I would like to have
Sample code:
(p <- ggplot(df, aes(x=Type, y=Value, fill=Episode, group=Type)) +
geom_boxplot()+
geom_line()+
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.8))+
theme_bw())
Sample data:
df<-structure(list(Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L,
7L, 7L, 7L), .Label = c("A", "B", "C", "D", "E", "F", "G"), class = "factor"),
Episode = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L,
1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
4L, 5L, 6L), .Label = c("t1", "t2", "t3", "t4", "t5", "t6"
), class = "factor"), Value = c(32, 36, 57, 83, 88, 40, 40,
44, 67, 77, 66, 45, 88, 46, 56, 99, 65, 0, 66, 46, 59, 77,
74, 79, 38, 45, 60, 78, 66, 75, 45, 55, 68, 77, 88, 35, 36,
118, 80, 73, 71, 0)), row.names = c(NA, -42L), class = "data.frame")
I am not entirely sure if I understand your question correctly, but for me, the cleanest plot would be:
library(ggplot2)
df<-structure(list(Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L,
7L, 7L, 7L), .Label = c("A", "B", "C", "D", "E", "F", "G"), class = "factor"),
Episode = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L,
1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
4L, 5L, 6L), .Label = c("t1", "t2", "t3", "t4", "t5", "t6"
), class = "factor"), Value = c(32, 36, 57, 83, 88, 40, 40,
44, 67, 77, 66, 45, 88, 46, 56, 99, 65, 0, 66, 46, 59, 77,
74, 79, 38, 45, 60, 78, 66, 75, 45, 55, 68, 77, 88, 35, 36,
118, 80, 73, 71, 0)), row.names = c(NA, -42L), class = "data.frame")
p <- ggplot(df, aes(x=Type, y=Value, group = Type)) +
geom_boxplot() +
geom_line() +
geom_point(aes(col=Episode))
p
Created on 2021-04-15 by the reprex package (v0.3.0)

Arules Package: Discretize a continuous vector into a discrete vector with specific categories to produce a table of frequencies in R

Overview
I have a continuous vector called "Canopy_Index" and another vector called "Species", which is a factor.
My aim is to discretize the Canopy_Index vector using the R-code below into a discrete vector containing specific percentage categories by intervals of 10 i.e. 0-10, 10-20, 20-30, 30-40, 50-60, 60-70, 70-80, 80-90, 90-100.
The objective is to show the frequency distribution for each category of Canopy Index for two Species of oak trees (Quercus petraea and Quercus robur) by following this R-code example at the bottom of the page
Ideally, I would like to produce a table with 3 columns, and 11 rows. Each row represents percentage categories of which there are 10 categories in total.
Column Headings:
Column 1 = Discrete Percentage Categories for Canopy_Index
Column 2 = Frequencies of Canopy_Index Categories for Q. Petraea
Column 3 = Frequencies of Canopy_Index Categories for Q. robur
However, I keep on producing the error message below.
If anyone is able to help, I would be deeply appreciative.
R-code
Attempt 1
Canopy_Interval1<-table(discretize(Discrete_Canopy, methods=list(
Canopy_Index=list(method="frequency", breaks=10,
labels=c("0-10", "10-20","20-30", "30-40", "50-60", "60-70", "70-80",
"80-90", "90-100"),
Species=list(method="frequency", breaks=2,
labels=c("Quercus petraea", "Quercus robur"),
default = list(method = "none"))))))
**Error message**
Error in (1 - h) * qs[i] : non-numeric argument to binary operator
Attempt 2
breaks <- seq(from = 0, to = 100, by = 10)
labels <- map_chr(breaks[1:10], ~ paste(.x, .x + 10, sep="-"))
#########Compartmentalise the number of tree species per canopy interval
indices <- SpeciesLatitudeCanopy$Canopy_Index
table(indices)
########Place the percentages into table form
percents <- discretize(indices,
method = "fixed",
breaks = breaks,
labels = labels)
###Produce the table of percents
table(percents)
##Produce the data table with binned data
SpecLatCan<-SpeciesLatitudeCanopy %>%
mutate(Canopy_Index_Binned = percents)
##Structure
head(SpecLatCan)
###Produce a data frame
SpeciesLatitudeCanopy1<-data.frame(SpecLatCan)
###Check the structure of the data frame
str(SpeciesLatitudeCanopy1)
##Count Canopy/Species
SpeciesLatitudeCanopy1_Count<-count(SpeciesLatitudeCanopy1, c(Canopy_Index_Binned, Species))
##Error Message
Error in count(SpeciesLatitudeCanopy1, c(Canopy_Index_Binned, Species)) :
object 'Canopy_Index_Binned' not found
Structure of the data frame
Head of the data frame
Data frame
structure(list(Date_observed = structure(c(10L, 15L, 3L, 3L,
3L, 3L, 13L, 13L, 11L, 11L, 11L, 11L, 9L, 9L, 9L, 9L, 12L, 12L,
12L, 12L, 6L, 6L, 10L, 10L, 10L, 10L, 14L, 14L, 14L, 14L, 13L,
13L, 13L, 9L, 9L, 9L, 9L, 14L, 14L, 14L, 14L, 8L, 8L, 8L, 8L,
14L, 7L, 7L, 7L, 7L, 12L, 12L, 12L, 12L, 12L, 12L, 5L, 5L, 5L,
5L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 13L, 13L, 6L, 8L,
8L, 8L, 1L, 1L, 1L, 1L, 6L, 6L, 6L, 6L, 13L, 13L, 13L, 13L, 12L,
12L, 12L, 12L, 2L, 2L, 2L, 2L, 11L, 11L, 11L, 3L, 3L, 3L, 3L,
14L, 14L, 14L, 9L, 9L, 9L, 9L, 14L, 14L, 13L, 13L, 13L, 13L,
3L, 3L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 3L, 3L, 3L, 3L,
14L, 14L, 14L, 14L, 11L, 11L, 11L, 11L, 13L, 13L, 13L, 13L, 3L,
3L, 3L, 3L, 14L, 14L, 6L, 6L, 6L, 12L, 12L, 12L, 12L, 14L, 13L,
13L, 13L, 13L, 14L, 14L, 14L, 14L, 11L, 11L, 11L, 14L, 14L, 14L,
10L, 10L, 10L, 10L, 6L, 5L, 5L, 5L, 10L, 10L, 10L, 4L, 4L, 13L,
13L, 13L, 13L, 9L, 9L, 9L, 13L, 13L, 13L, 14L, 14L, 14L, 14L,
6L, 6L, 6L, 6L, 13L, 13L, 13L, 13L, 12L, 12L, 12L, 12L, 14L,
14L, 14L, 14L, 6L, 6L, 6L, 6L), .Label = c("10/23/18", "11/18/18",
"11/30/18", "11/6/18", "12/1/18", "12/10/18", "12/12/18", "12/2/18",
"12/3/18", "12/4/18", "12/6/18", "12/7/18", "12/8/18", "12/9/18",
"9/10/18"), class = "factor"), Latitude = c(51.41752, 52.243806,
52.947709, 52.947709, 52.947709, 52.947709, 51.491811, 51.491811,
51.60157, 51.60157, 51.60157, 51.60157, 52.68959, 52.68959, 52.68959,
52.68959, 50.697802, 50.697802, 50.697802, 50.697802, 53.62417,
53.62417, 50.446841, 50.446841, 50.446841, 50.446841, 53.959679,
53.959679, 53.959679, 53.959679, 52.01434, 52.01434, 52.01434,
51.78375, 51.78375, 51.78375, 51.78375, 51.456965, 51.456965,
51.456965, 51.456965, 52.011812, 52.011812, 52.011812, 52.011812,
55.91924, 50.121978, 50.121978, 50.121978, 50.121978, 51.43474,
51.43474, 51.10708, 51.10708, 51.10708, 51.10708, 50.435984,
50.435984, 50.435984, 50.435984, 51.78666, 51.78666, 51.78666,
51.78666, 53.38728, 53.38728, 53.38728, 53.38728, 52.441088,
52.441088, 52.552344, 53.582285, 53.582285, 53.582285, 49.259471,
49.259471, 49.259471, 49.259471, 50.462, 50.462, 50.462, 50.462,
51.746642, 51.746642, 51.746642, 51.746642, 52.2501, 52.2501,
52.2501, 52.2501, 52.42646, 52.42646, 52.42646, 52.42646, 50.79387,
50.79387, 50.79387, 53.615575, 53.615575, 53.615575, 53.615575,
51.08478, 51.08478, 51.08478, 53.19329, 53.19329, 53.19329, 53.19329,
55.968437, 55.968437, 56.52664, 56.52664, 56.52664, 56.52664,
52.04252, 52.04252, 51.8113, 51.8113, 51.8113, 51.8113, 50.52008,
50.52008, 50.52008, 50.52008, 51.48417, 51.48417, 51.48417, 51.48417,
54.58243, 54.58243, 54.58243, 54.58243, 52.58839, 52.58839, 52.58839,
52.58839, 52.717283, 52.717283, 52.717283, 52.717283, 50.740764,
50.740764, 50.740764, 50.740764, 50.733412, 50.733412, 50.79926,
50.79926, 50.79926, 53.675788, 53.675788, 53.675788, 53.675788,
55.43828, 48.35079, 48.35079, 48.35079, 48.35079, 51.36445, 51.36445,
51.36445, 51.36445, 52.36286, 52.36286, 52.36286, 52.122402,
52.122402, 52.122402, 52.16104, 52.16104, 52.16104, 52.16104,
54.7311, 51.61842, 51.61842, 51.61842, 55.920966, 55.920966,
55.920966, 57.158724, 57.158724, 51.88468, 51.88468, 51.88468,
51.88468, 52.34015, 52.34015, 52.34015, 53.37687, 53.37687, 53.37687,
54.27745, 54.27745, 54.27745, 54.27745, 52.026042, 52.026042,
52.026042, 52.026042, 51.319032, 51.319032, 51.319032, 51.319032,
51.51365, 51.51365, 51.51365, 51.51365, 53.43202, 53.43202, 53.43202,
53.43202, 51.50797, 51.50797, 51.50797, 51.50797), Longitude = c(-0.32116,
1.30786, -1.435407, -1.435407, -1.435407, -1.435407, -3.210324,
-3.210324, -3.67111, -3.67111, -3.67111, -3.67111, -3.3081, -3.3081,
-3.3081, -3.3081, -2.11692, -2.11692, -2.11692, -2.11692, -2.43155,
-2.43155, -3.706923, -3.706923, -3.706923, -3.706923, -1.061008,
-1.061008, -1.061008, -1.061008, 1.04007, 1.04007, 1.04007, -0.65046,
-0.65046, -0.65046, -0.65046, -2.624917, -2.624917, -2.624917,
-2.624917, -0.70082, -0.70082, -0.70082, -0.70082, -3.20936,
-5.555169, -5.555169, -5.555169, -5.555169, 0.45981, 0.45981,
-2.32027, -2.32027, -2.32027, -2.32027, -4.105617, -4.105617,
-4.105617, -4.105617, -0.71433, -0.71433, -0.71433, -0.71433,
-2.95811, -2.95811, -2.95811, -2.95811, -0.176158, -0.176158,
-1.337177, -2.802239, -2.802239, -2.802239, -123.107788, -123.107788,
-123.107788, -123.107788, -3.5607, -3.5607, -3.5607, -3.5607,
0.486416, 0.486416, 0.486416, 0.486416, -0.8825, -0.8825, -0.8825,
-0.8825, -1.78771, -1.78771, -1.78771, -1.78771, 0.26684, 0.26684,
0.26684, -2.432959, -2.432959, -2.432959, -2.432959, -0.73626,
-0.73626, -0.73626, -0.63793, -0.63793, -0.63793, -0.63793, -3.179732,
-3.179732, -3.40313, -3.40313, -3.40313, -3.40313, -2.43733,
-2.43733, -0.22894, -0.22894, -0.22894, -0.22894, -4.20756, -4.20756,
-4.20756, -4.20756, -0.34854, -0.34854, -0.34854, -0.34854, -5.93229,
-5.93229, -5.93229, -5.93229, -1.96843, -1.96843, -1.96843, -1.96843,
-2.410575, -2.410575, -2.410575, -2.410575, -2.361234, -2.361234,
-2.361234, -2.361234, -2.014029, -2.014029, -3.19446, -3.19446,
-3.19446, -1.272404, -1.272404, -1.272404, -1.272404, -4.64226,
10.91812, 10.91812, 10.91812, 10.91812, -0.23106, -0.23106, -0.23106,
-0.23106, -2.06327, -2.06327, -2.06327, -0.487443, -0.487443,
-0.487443, 0.18702, 0.18702, 0.18702, 0.18702, -5.8041, -0.16034,
-0.16034, -0.16034, -3.193503, -3.193503, -3.193503, -2.166099,
-2.166099, -0.17853, -0.17853, -0.17853, -0.17853, -1.27795,
-1.27795, -1.27795, -1.34506, -1.34506, -1.34506, -0.47911, -0.47911,
-0.47911, -0.47911, -0.503113, -0.503113, -0.503113, -0.503113,
-0.472994, -0.472994, -0.472994, -0.472994, -3.18722, -3.18722,
-3.18722, -3.18722, -2.27968, -2.27968, -2.27968, -2.27968, -0.25931,
-0.25931, -0.25931, -0.25931), Altitude = c(0, 0, 103.9, 103.9,
103.9, 103.9, 15, 15, 184, 184, 184, 184, 176, 176, 176, 176,
12, 12, 12, 12, 178, 178, 36, 36, 36, 36, 11, 11, 11, 11, 47,
47, 47, 210, 210, 210, 210, 97, 97, 97, 97, 0, 0, 0, 0, 100,
68, 68, 68, 68, 4, 4, 200, 200, 200, 200, 160, 160, 160, 160,
165.8, 165.8, 165.8, 165.8, 0, 0, 0, 0, 0, 0, 0, 36, 36, 36,
47, 47, 47, 47, 0, 0, 0, 0, 43, 43, 43, 43, 97, 97, 97, 97, 133,
133, 133, 133, 18, 18, 18, 123, 123, 123, 123, 127, 127, 127,
15, 15, 15, 15, 14, 14, 65, 65, 65, 65, 45, 45, 129, 129, 129,
129, 18, 18, 18, 18, 30, 30, 30, 30, 19, 19, 19, 19, 0, 0, 0,
0, 0, 0, 0, 0, 96, 96, 96, 96, 0, 0, 0, 0, 0, 49, 49, 49, 49,
0, 0, 0, 0, 0, 48, 48, 48, 48, 123, 123, 123, 43, 43, 43, 75,
75, 75, 75, 0, 73, 73, 73, 115, 115, 115, 119, 119, 94, 94, 94,
94, 112, 112, 112, 34, 34, 34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.7, 40.1, 68, 45,
60, 54, 104, 122, 85, 71, 81, 118, 39.8, 43.6, 44.6, 22.6, 19.8,
16.6, 15.1, 11.9, 48.1, 83.7, 77.9, 81.8, 102.5, 75.5, 57.3,
0.3, 0.2, 0.3, 0.3, 99, 85, 74, 68, 60, 67.3, 36.9, 82, 51.8,
19.9, 14.6, 196, 122, 118, 180, 58.6, 54.1, 58, 61.5, 58.4, 40.6,
61, 68.6, 117, 240, 210, 310, 134, 64, 52.2, 57, 73.9, 37.1,
170, 114, 127, 158, 147.4, 135.3, 122.9, 104.1, 263, 237, 322,
302, 173, 186, 144, 155, 89, 41, 68, 83, 81.5, 29.3, 43.3, 141.6,
85.5, 82.8, 114.1, 129, 127, 143, 125, 92, 68, 90, 25, 20, 63.7,
39.8, 66.2, 112.4, 41.9, 43.8, 124.5, 94.1, 68.6, 74.4, 24.2,
54.7, 43, 33.1, 306, 274, 56, 60, 72.5, 128.5, 22, 16, 143, 103,
53, 130, 48.4, 69.8, 6.4, 18.6, 129.2, 41.7, 57.6, 14, 320, 352,
120.9, 108.3, 53.2, 240, 274, 122, 85, 21, 52, 43, 38, 37, 219,
215, 216, 175, 124, 133, 119, 85.9, 49.7, 97.1, 40.8, 79.3, 62.4,
62.4, 70, 115.9, 111.1, 88.9, 73, 54, 75, 159, 244, 181.5, 149.7,
122, 143.6, 148, 145, 99, 49, 57.9, 54.8, 53.5, 88.8, 71.3, 101.9,
27.5, 32, 54, 54.1, 169, 152, 160, 138, 90.8, 87.9, 77.4, 81.2,
91.7, 62.7, 50, 72.9, 24.8, 61, 88.6, 80.1), Urbanisation_index = c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 4L, 4L, 4L,
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4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 3L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L)), class = "data.frame", row.names = c(NA, -215L
))
Does this look like the table you are aiming for?
library("arules")
#> Loading required package: Matrix
#>
#> Attaching package: 'arules'
#> The following objects are masked from 'package:base':
#>
#> abbreviate, write
library("tidyverse")
# Smaller tibble with only the two necessary columns
data <- tibble(
Canopy_Index = c(15, 95, 45, 5),
Species = c("A", "A", "B", "B"))
breaks <- seq(from = 0, to = 100, by = 10)
labels <- map_chr(breaks[1:10], ~ paste(.x, .x + 10, sep = "-"))
indices <- data$Canopy_Index
table(indices)
#> indices
#> 5 15 45 95
#> 1 1 1 1
percents <- discretize(indices,
method = "fixed",
breaks = breaks,
labels = labels)
table(percents)
#> percents
#> 0-10 10-20 20-30 30-40 40-50 50-60 60-70 70-80 80-90 90-100
#> 1 1 0 0 1 0 0 0 0 1
data %>%
mutate(Canopy_Index_Binned = percents) %>%
count(Canopy_Index_Binned, Species) %>%
spread(Species, n, fill = 0)
#> # A tibble: 4 x 3
#> Canopy_Index_Binned A B
#> <fct> <dbl> <dbl>
#> 1 0-10 0 1
#> 2 10-20 1 0
#> 3 40-50 0 1
#> 4 90-100 1 0
# There is also a base function that can convert a numeric vector to a factor in the same way:
percents <- cut(indices,
breaks = breaks,
labels = labels)
Created on 2019-03-14 by the reprex package (v0.2.1)

How to draw polygon/ convex hull around Partitioned Around Medoids (PAM) clusters in R?

Good Day
Is it possible to produce a plot based on the output of a PAM dissimilarity clustering analysis with polygons drawn around the outer point of the clusters?
I have currently achieved something similar using the function clusplot however am more interested in seeing the clusters demarcated using straight lines.
# Installing packages
library(cluster)
library(fpc)
library(ggplot2)
library(ggfortify)
#Importing Koeberg matrix into R
KoebergAllCSV <- read.csv("C:/R/Koeberg Cluster/KoebergAllCSV.csv", row.names=1, sep=";")
#Checking if data is in the correct format/Checking class/mode of each column
sapply(KoebergAllCSV, mode)
sapply(KoebergAllCSV, class)
#Creating gower dissimilarity matrix using function "daisy"
#specifying variable type(numerics all ratioscaled and log transformed)
#and weighting all columns as 1
Koeberg.Diss = daisy(KoebergAllCSV, metric = "gower", type = list(logratio = c("Mass", "EF")), weights = rep.int(1,5))
attributes(Koeberg.Diss)
#Determine k
pamk(Koeberg.Diss, krange=2:50, criterion="asw", usepam=TRUE, scaling=FALSE, diss=TRUE, critout=FALSE)
#Run cluster analysis using PAM (Partitioning around medoids)
pam_fit= pam(Koeberg.Diss, diss = TRUE, k = 28)
#Export cluster info
KoebergClusInfo = paste("KoebergClusInfo", ".txt")
write.table(pam_fit$clustering, file = KoebergClusInfo, sep=",")
## Default S3 method:
clusplot(Koeberg.Diss, pam_fit$clustering, diss = TRUE,
stand = FALSE,
lines = 0, labels= 4, xlim = c(-1,1), plotchar = TRUE, span = TRUE,
shade = TRUE, color = TRUE, col.p = "black",
main = "Koeberg gower/pam Clusterplot",
verbose = getOption("verbose"))
I am aware that the function autoplot in ggplot2 accepts objects of class pam however when attempting to use it for my data and replacing the above clusplot function with
autoplot(pam(pam_fit), frame = TRUE)
or
autoplot(pam(Koeberg.Diss, diss = TRUE, k = 28), frame = TRUE)
I get the following errors...
Error in pam(pam_fit) : x is not a numeric dataframe or matrix.
and
Error in as.data.frame.default(x[[i]], optional = TRUE,
stringsAsFactors = stringsAsFactors) : cannot coerce class ""waiver""
to a data.frame Respectively...
I am relatively new to R and posting questions in these forums, so any help would be massively appreciated.
Edit: Got it to work using the fviz_cluster() in the factoextra package
# Installing packages
library(cluster)
library(fpc)
library(factoextra)
#Importing Koeberg matrix into R
KoebergAllCSV <- read.csv("C:/R/Koeberg Cluster/KoebergAllCSV.csv",
row.names=1, sep=";")
#creating gower dissimilarity matrix using daisy
Koeberg.Gower = as.matrix(daisy(KoebergAllCSV, metric = "gower", type =
list(logratio = c("Mass", "EF"))))
attributes(Koeberg.Gower)
pamk(Koeberg.Gower, krange=2:50, criterion="asw", usepam=TRUE,
scaling=FALSE, diss=TRUE, critout=FALSE)
Koeberg.Pam = pam(Koeberg.Gower, 28, diss = TRUE, keep.diss = TRUE)
fviz_cluster(object = list(data=Koeberg.Gower, cluster =
Koeberg.Pam$clustering), geom = c("point", "text"), ellipse.type =
"convex", stand = FALSE)
fviz_silhouette(silhouette(Koeberg.Pam))
# Installing packages
library(cluster)
library(fpc)
library(factoextra)
#Importing Koeberg matrix into R
KoebergAllCSV <- read.csv("C:/R/Koeberg Cluster/KoebergAllCSV.csv",
row.names=1, sep=";")
#creating gower dissimilarity matrix using daisy
Koeberg.Gower = as.matrix(daisy(KoebergAllCSV, metric = "gower", type =
list(logratio = c("Mass", "EF"))))
attributes(Koeberg.Gower)
pamk(Koeberg.Gower, krange=2:50, criterion="asw", usepam=TRUE,
scaling=FALSE, diss=TRUE, critout=FALSE)
Koeberg.Pam = pam(Koeberg.Gower, 28, diss = TRUE, keep.diss = TRUE)
fviz_cluster(object = list(data=Koeberg.Gower, cluster =
Koeberg.Pam$clustering), geom = c("point", "text"), ellipse.type =
"convex", stand = FALSE)
fviz_silhouette(silhouette(Koeberg.Pam))
Data used:
"KoebergAllCSV"
structure(list(Mass = c(157000, 775, 197, 15000, 3250, 628, 1815,
2070, 2000, 1218, 614, 536, 379, 235, 800, 672, 1960, 768, 1540,
1790, 3500, 7450, 4030, 2200, 830, 1180, 1310, 955, 590, 1168,
820, 790, 5000, 883, 824, 280, 184, 941, 293, 1250, 3900, 1700,
925, 220, 1040, 510, 690, 600, 539, 1018, 122, 1086, 118, 737,
370, 1236, 5820, 229, 226, 220, 305.5, 94.5, 390, 198, 445, 623,
1100, 377, 340, 418, 326, 202, 139, 47, 35.1, 46.1, 580, 1150,
66, 44, 50, 30, 34.2, 30, 91, 71, 59, 78.9, 110, 405, 19.5, 73,
64, 39, 54, 39, 37, 48, 21.2, 26.3, 24.2, 29, 15.2, 35, 16.1,
16.8, 29.7, 12.5, 55, 612, 630, 865, 22.4, 8.6, 47.3, 32.5, 28.8,
17.3, 38, 23.5, 22, 15.5, 18.1, 34, 23, 13.1, 13, 14.7, 19.1,
14, 18.6, 15.5, 37, 14.5, 24.6, 25, 28.5, 50.8, 52, 68.8, 76.1,
100, 85, 158, 113, 88, 25.6, 13, 10.2, 30.5, 38, 55, 45.5, 30,
52, 11, 17.8, 29, 13, 23.2, 38, 21, 25, 27.3, 427, 1572, 78.9,
15, 61, 212.9, 700, 11.1, 44, 29.6, 124, 3200, 5800, 5300, 950,
62.4, 205, 270, 93, 40.2, 102, 240, 90, 33, 16.6, 39.2, 47, 60.8,
13, 20.8, 8, 11, 165000, 180000, 63600, 11400, 21200, 41000,
11300, 840000, 240000, 320000, 900, 4090, 1250, 19000, 19000,
6400, 2610, 47, 4500, 1258, 238, 55, 113, 9990, 5360, 17800,
110.1973216, 238.1629085, 89.33169378, 245.0708356, 83.49190575,
7.323754897, 17.91558243, 2.259871723, 1.992123644, 78.63046291,
235.6804221, 413.5582987, 486.5966599, 7.418054089, 8.4510848,
8.4510848, 42.83324573, 8.4510848, 3.14445177, 2000, 496.2334891,
119.4158615, 805.4349144, 8.212468482, 25.0905618), Diet = structure(c(4L,
2L, 2L, 6L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 5L, 4L, 5L, 5L, 5L, 5L, 2L, 5L, 5L, 5L, 5L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L,
5L, 5L, 5L, 2L, 2L, 2L, 5L, 1L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 4L, 5L, 5L, 5L, 6L, 5L, 3L, 1L, 1L, 3L, 3L, 3L,
3L, 3L, 3L, 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 1L, 1L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 5L, 3L, 3L, 3L, 3L, 5L, 5L, 5L,
1L, 1L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 1L, 3L, 3L, 3L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L, 3L, 5L, 5L, 5L, 1L,
3L, 5L, 5L, 3L, 3L, 5L, 5L, 5L, 5L, 3L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 2L, 5L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 2L,
2L, 1L, 1L, 5L, 5L, 2L, 2L, 5L, 2L, 1L, 5L, 3L, 5L, 1L, 3L,
5L, 5L, 5L, 1L, 3L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 5L, 4L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L,
5L, 4L, 4L, 5L, 5L, 5L, 5L, 3L, 5L, 5L, 4L, 5L), .Label = c("Ae",
"Aq", "Ar", "Fo", "Te"), class = "factor"), EF = c(36274.12643,
974.5491757, 383.4606456, 15194.1663, 4179.125464, 1043.599331,
1739.739739, 1902.677158, 1858.620513, 1325.913225, 831.6334703,
758.1419376, 598.7459669, 432.4008244, 995.8492032, 1104.982804,
1833.224631, 968.5460848, 1555.574839, 2526.177199, 2720.81891,
4551.218864, 2995.035921, 1983.25768, 1021.131045, 1297.600326,
1393.320578, 1123.496167, 809.3558665, 1288.599252, 1012.736663,
987.3550419, 3468.868783, 1065.095472, 1016.098313, 487.1962293,
366.0414243, 1112.253632, 502.488525, 1349.53769, 2928.89833,
1663.891544, 1099.339465, 413.4082553, 1190.663398, 732.8997761,
900.420823, 818.6726853, 354.4240516, 652.168291, 85.2606497,
693.8895747, 270.483605, 941.7478954, 589.026282, 1339.226017,
3846.819879, 533.5361702, 528.1460593, 517.3161179, 666.1149499,
269.8814521, 803.9152978, 477.0047921, 889.8724349, 1153.045225,
1786.335023, 783.201274, 723.3180648, 640.0447608, 540.3690726,
390.0619447, 302.4004117, 144.5066856, 118.4522857, 142.6164524,
799.9885345, 1275.042111, 182.0952927, 543.7188737, 634.8358004,
341.8068213, 400.635478, 341.8068213, 1311.800923, 191.3798609,
168.7094925, 205.6358063, 257.8562828, 626.4208001, 79.37849663,
195.0348258, 178.3190991, 127.263627, 158.8361858, 127.263627,
122.7819915, 144.6363257, 82.97394225, 96.07341942, 90.78786186,
102.6748266, 66.17393783, 116.6808568, 68.813704, 70.83430724,
104.3536694, 57.93381766, 158.6645733, 816.6280747, 832.8847551,
1033.247332, 86.13942963, 44.9254345, 143.1986471, 110.9466069,
102.1927841, 72.26112014, 123.3917674, 88.99374555, 85.0904386,
67.05928121, 74.51689843, 114.4034188, 87.7017517, 59.81055334,
59.49970587, 64.68582581, 77.29226408, 62.57493448, 75.91055058,
67.05928121, 121.1742978, 64.08606129, 91.80560821, 92.81807245,
101.4677065, 150.3213487, 152.7269268, 184.7538415, 197.8677001,
238.2502359, 213.3232585, 325.1772534, 258.896799, 218.4145451,
94.32710718, 59.49970587, 50.45235653, 106.2569217, 123.3917674,
158.6645733, 139.4700925, 105.0692888, 152.7269268, 53.11049696,
73.67479581, 102.6748266, 59.49970587, 88.21961721, 123.3917674,
82.44084978, 92.81807245, 98.54258242, 649.397604, 1577.514936,
202.7896107, 65.58060147, 170.2384385, 404.275036, 909.2871722,
53.43834074, 136.3267745, 104.1146163, 265.4017335, 2559.743414,
3837.812585, 3609.284914, 1119.48705, 196.0571475, 393.9976926,
605.6763891, 266.5768403, 139.7476799, 286.2283703, 438.6449711,
224.9201933, 112.1044413, 70.25978867, 126.0282381, 142.5804177,
169.8586911, 59.49970587, 81.90613014, 42.76953519, 53.11049696,
44893.11086, 48012.29543, 21505.57155, 5704.435068, 9209.019243,
15323.26221, 5665.766265, 157697.8254, 59952.20689, 74861.38869,
616.5285774, 2297.756619, 820.2217331, 23289.68486, 8728.776034,
3390.680499, 1555.167143, 82.25108625, 2783.313695, 2329.752262,
567.6985933, 163.9110073, 301.8499294, 4992.739194, 2906.435392,
8247.673366, 12.81581191, 25.42711978, 10.63408241, 26.08172622,
10.0137771, 1.178076499, 2.549050089, 0.353356528, 0.31350088,
9.49371787, 25.19136319, 41.52955076, 47.98985328, 1.091673606,
1.235456699, 1.235456699, 5.76431571, 1.235456699, 0.483456886,
112.0018952, 48.83385255, 13.76461928, 75.11335195, 1.274157763,
3.438909954)), .Names = c("Mass", "Diet", "Time", "Space",
"EF"), class = "data.frame", row.names = c("CommonOstrich", "GreatCrestedGrebe",
"LittleGrebe", "GreatWhitePelican", "White-breastedCormorant",
"ReedCormorant", "AfricanDarter", "GreyHeron", "Black-headedHeron",
"PurpleHeron", "LittleEgret", "Yellow-billedEgret", "CattleEgret",
"Green-backedHeron", "Black-crownedNight-Heron", "HamerkopHamerkop",
"AfricanSacredIbis", "GlossyIbis", "HadedaIbis", "AfricanSpoonbill",
"GreaterFlamingo", "Spur-wingedGoose", "EgyptianGoose", "SouthAfricanShelduck",
"CapeShoveler", "AfricanBlackDuck", "Yellow-billedDuck", "Red-billedTeal",
"CapeTeal", "SouthernPochard", "MaccoaDuck", "White-backedDuck",
"Secretarybird", "PeregrineFalcon", "LannerFalcon", "RockKestrel",
"LesserKestrel", "Yellow-billedKite", "Black-shoulderedKite",
"BootedEagle", "AfricanFish-eagle", "JackalBuzzard", "SteppeBuzzard",
"Rufous-chestedSparrowhawk", "BlackSparrowhawk", "AfricanGoshawk",
"AfricanMarsh-harrier", "BlackHarrier", "Grey-wingedFrancolin",
"CapeSpurfowl", "CommonQuail", "HelmetedGuineafowl", "BlackCrake",
"AfricanPurpleSwamphen", "CommonMoorhen", "Red-knobbedCoot",
"BlueCrane", "CrownedLapwing", "BlacksmithLapwing", "RuffRuff",
"CommonGreenshank", "WoodSandpiper", "PiedAvocet", "Black-wingedStilt",
"WaterThick-knee", "SpottedThick-knee", "KelpGull", "Grey-headedGull",
"Hartlaub'sGull", "SpeckledPigeon", "Red-eyedDove", "CapeTurtle-dove",
"LaughingDove", "NamaquaDove", "Klaas'sCuckoo", "DiderickCuckoo",
"BarnOwl", "SpottedEagle-owl", "Fiery-neckedNightjar", "CommonSwift",
"AfricanBlackSwift", "White-rumpedSwift", "HorusSwift", "LittleSwift",
"AlpineSwift", "SpeckledMousebird", "White-backedMousebird",
"Red-facedMousebird", "PiedKingfisher", "GiantKingfisher", "MalachiteKingfisher",
"EuropeanBee-eater", "AfricanHoopoe", "AcaciaPiedBarbet", "GreaterHoneyguide",
"LesserHoneyguide", "CardinalWoodpecker", "Large-billedLark",
"Grey-backedSparrowlark", "Red-cappedLark", "BarnSwallow", "White-throatedSwallow",
"Pearl-breastedSwallow", "GreaterStripedSwallow", "RockMartin",
"Brown-throatedMartin", "BandedMartin", "Black(Southernrace)Saw-wing",
"Fork-tailedDrongo", "PiedCrow", "CapeCrow", "White-neckedRaven",
"GreyTit", "CapePenduline-tit", "CapeBulbul", "CappedWheatear",
"FamiliarChat", "AfricanStonechat", "CapeRobin-chat", "KarooScrub-robin",
"LesserSwamp-warbler", "AfricanReed-warbler", "LittleRush-warbler",
"CapeGrassbird", "Long-billedCrombec", "Bar-throatedApalis",
"CloudCisticola", "Grey-backedCisticola", "Levaillant'sCisticola",
"AfricanDuskyFlycatcher", "Chestnut-ventedTit-babbler", "Layard'sTit-babbler",
"FiscalFlycatcher", "CapeBatis", "AfricanParadise-flycatcher",
"CapeWagtail", "AfricanPipit", "CapeLongclaw", "Common(Southern)Fiscal",
"SouthernBoubou", "Bokmakierie", "CommonStarling", "WattledStarling",
"Red-wingedStarling", "PiedStarling", "CapeSugarbird", "MalachiteSunbird",
"Orange-breastedSunbird", "SouthernDouble-collaredSunbird", "HouseSparrow",
"CapeSparrow", "CapeWeaver", "SouthernMasked-weaver", "SouthernRedBishop",
"YellowBishop", "CommonWaxbill", "Pin-tailedWhydah", "CapeCanary",
"Black-headedCanary", "BrimstoneCanary", "White-throatedCanary",
"YellowCanary", "Streaky-headedSeedeater", "CapeBunting", "RockDove",
"MallardDuck", "OliveThrush", "CapeWhite-eye", "CapeLong-billedLark",
"Burchell'sCoucal", "SouthernBlackKorhaan", "KarooPrinia", "CapeClapperLark",
"SouthernGrey-headedSparrow", "LittleBittern", "BlackStork",
"Verreaux'sEagle", "MartialEagle", "AfricanHarrier-Hawk", "BlackrumpedButtonquail",
"AfricanRail", "AfricanJacana", "CommonSandpiper", "LittleStint",
"White-wingedTern", "NamaquaSandgrouse", "Red-chestedCuckoo",
"KarooLark", "SandMartin", "SombreGreenbul", "MountainChat",
"Ant-eatingChat", "ZittingCisticola", "SpottedFlycatcher", "FairyFlycatcher",
"DuskySunbird", "RedHartebeest", "BlueWildebeest", "Bontebok",
"CapeGrysbok", "CommonDuiker", "Springbok", "Steenbok", "CommonEland",
"Gemsbok", "PlainsZebra", "YellowMongoose", "LargeGreyMongoose",
"SmallGreyMongoose", "CapePorcupine", "Caracal", "AfricanWildCat",
"Small-spottedGenet", "CapeGoldenMole", "ScrubHare", "CapeDuneMole-Rat",
"VleiRat", "FourStripedGrassMouse", "CapeGerbil", "Black-BackedJackal",
"Bat-earedFox", "AfricanClawlessOtter", "HeraldSnake", "RhombicEgg-eater",
"SpottedHarlequinSnake", "OliveHouseSnake", "SpottedHouseSnake",
"Knox'sDesertLizard", "NamaquaDwarfChameleon", "Austen'sThick-toedGecko",
"OcelatedThick-toedGecko", "CrossedWhipSnake", "CapeWhipSnake",
"Spotted/RhombicSkaapsteker", "MoleSnake", "Short-leggedseps",
"SilveryDwarfBurrowingSkink", "BloubergDwarfBurrowingSkink",
"CapeSkink", "Red-SidedSkink", "VariegatedSkink", "AngulateTortoise",
"Boomslang", "KarooWhipSnake", "CapeCobra", "Delalande'sBeakedBlindSnake",
"CapeGirdledLizard"))

How can I order the months chronologically in ggplot2 short of writing the months out?

I am trying to plot count v/s month
ggplot(dat, aes(x=month, y=count,group=region)) +
geom_line(data=mcount[mcount$region == "West coast", ],colour="black",stat="identity", position="dodge")+
geom_point(data=mcount[mcount$region == "West coast", ],colour="black", size=2, shape=21, fill="white")+
theme_bw()+
theme(legend.key = element_rect(colour = "black")) +
guides(fill = guide_legend(override.aes = list(colour = NULL)))+
ggsave("test.png",width=6, height=4,dpi=300)
But I want to order the months chronologically from Jan to Dec. How can I do this short of writing all the months out?
dput
structure(list(region = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("West coast", "Arizona", "Front range", "Flash flood alley",
"Mississippi valley", "Appalachians"), class = "factor"), month = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 4L, 12L, 11L,
5L, 2L, 9L, 8L, 6L, 10L, 3L, 7L, 8L, 10L, 5L, 1L, 6L, 7L, 4L,
6L, 8L, 2L, 1L, 7L, 5L, 3L, 11L, 12L, 9L, 10L, 2L, 7L, 3L, 6L,
12L, 11L, 10L, 9L, 4L, 1L, 11L, 4L, 2L, 1L, 12L, 9L, 3L, 8L,
5L, 6L, 10L, 7L, 5L, 8L, 11L, 12L, 4L, 3L, 9L, 2L), .Label = c("Apr",
"Dec", "Oct", "Mar", "May", "Jul", "Sep", "Jun", "Nov", "Aug",
"Jan", "Feb"), class = "factor"), count = c(566, 545, 427, 751,
357, 399, 568, 433, 454, 347, 511, 251, 267, 207, 167, 142, 417,
109, 117, 373, 207, 130, 125, 145, 7, 14, 2, 2, 7, 3, 107, 74,
135, 48, 80, 53, 117, 125, 59, 53, 103, 30, 21, 18, 8, 22, 26,
37, 20, 5, 11, 1, 96, 29, 109, 8, 33, 53, 6, 1, 5, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0)), .Names = c("region", "month", "count"), row.names = c(NA,
-72L), class = c("data.table", "data.frame"))
Use the built-in month.name or month.abb variable to specify the levels of your factor in the correct order. In this case, you have abbreviations so month.abb is appropriate.
your_data$month = factor(your_data$month, levels = month.abb)
I think creating the factor in the correct order is the best way, but you can also just order the axis using the limits argument of the discrete scale (see ?discrete_scale for more info).
+ scale_x_discrete(limits = month.abb)
Locales
If you are in a non-English locale, you can construct your own month name constants with a little date formatting (basically stolen from Brian Ripley in this R-Help thread):
month.name.loc = format(ISOdate(2004, 1:12, 1), "%B")
month.abb.loc = format(ISOdate(2004, 1:12, 1), "%b")
If you want to use month names/abbreviations from a different locale than you're in, the withr package is useful.

adding geom_text from different dataset to geom_bar

I have a bar plot with a facet grid, and I would like to add the number of observations per sub-plot which are stored in a separate dataframe.
The bar plot is produced with
bar.plot <- ggplot(BarDiff.m.s, aes(x=value.change, fill=incompatibility))+
geom_bar(binwidth=1)+
labs(x="score differences", y="count / years since start of PSA")+
geom_vline(aes(xintercept=0), linetype="dotted")+
theme(plot.title=element_text(face="bold", size=10),
legend.position= "bottom")+
scale_fill_brewer(palette="Set1")+
facet_grid(years.since.peace ~ strategy.cm6.YP, space="free")
I tried to add the geom_text by adding after the geom_bar line
geom_text(data=num.obs, aes(label=paste("obs=",num.obs),y=4,x=min(BarDiff.m.s$value.change)))
however, I obtain the error message
Error in eval(expr, envir, enclos) : object 'incompatibility' not found
Apparently, for some reason, I have to consider the "fill" variable in geom_text; I tried to add group=BarDiff.m.s$incompatibility to geom_text, but to no avail.
I have seen How to add custom labels from a dataset on top of bars using ggplot/geom_bar in R? , but if possible I would like to keep the two data.frames separate and understand how to solve the "fill" issue. Any suggestion would be very much welcome! thx.
The pertaining data for the plot is
BarDiff.m.s <- structure(list(value.change = c(-1, -1, -2, -2, 1, NA, 0, -2,
-1, -2, NA, 2, -3, NA, NA, -3, -2, -1, -4, -1, -3, -1, 2, 2,
NA, 1, -1, 0, 0, -2, -2, -2, -1, 1, NA, -1, -1, 0, -2, NA, 0,
-4, NA, NA, NA, -3, -1, -4, -2, -3, -2, -1, 0, NA, NA, 0, -4,
NA, -2, -2, -3, -1, NA, NA, -1, -1, 0, -2, NA, 0, NA, NA, NA,
NA, -4, NA, -4, -2, -3, -2, -2, 2, NA, NA, 0, -4, -2, NA, NA,
NA, NA, NA, NA, -1, NA, NA, NA, NA, 0, NA, NA, NA, NA, NA, NA,
-4, NA, -2, -1, -2, NA, NA, NA, NA, -3, 1), incompatibility = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L,
1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L,
2L, 1L, 1L), .Label = c("territory", "government"), class = "factor"),
years.since.peace = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L), .Label = c("y0", "y10", "y15", "y20", "diff.y5",
"diff.y10", "diff.y15", "diff.y20"), class = "factor"), strategy.cm6.YP = structure(c(4L,
4L, 5L, 1L, 1L, 4L, 3L, 4L, 3L, 1L, 1L, 1L, 3L, 4L, 4L, 4L,
4L, 3L, 3L, 4L, 4L, 4L, 1L, 4L, 5L, 1L, 4L, 1L, 4L, 4L, 4L,
5L, 1L, 1L, 4L, 3L, 4L, 3L, 1L, 1L, 1L, 3L, 4L, 4L, 4L, 4L,
3L, 3L, 4L, 4L, 4L, 1L, 4L, 5L, 1L, 4L, 1L, 4L, 4L, 4L, 5L,
1L, 1L, 4L, 3L, 4L, 3L, 1L, 1L, 1L, 3L, 4L, 4L, 4L, 4L, 3L,
3L, 4L, 4L, 4L, 1L, 4L, 5L, 1L, 4L, 1L, 4L, 4L, 4L, 5L, 1L,
1L, 4L, 3L, 4L, 3L, 1L, 1L, 1L, 3L, 4L, 4L, 4L, 4L, 3L, 3L,
4L, 4L, 4L, 1L, 4L, 5L, 1L, 4L, 1L, 4L), .Label = c("none",
"only offered", "communication/\nfacilitation", "procedural",
"directive", "unspecified"), class = "factor")), .Names = c("value.change",
"incompatibility", "years.since.peace", "strategy.cm6.YP"), class = "data.frame", row.names = c(1298L,
1299L, 1335L, 1339L, 1340L, 1341L, 1344L, 1372L, 1379L, 1386L,
1387L, 1402L, 1415L, 1439L, 1449L, 1454L, 1455L, 1456L, 1463L,
1466L, 1470L, 1496L, 1497L, 1498L, 1525L, 1536L, 1542L, 1546L,
1563L, 1617L, 1618L, 1654L, 1658L, 1659L, 1660L, 1663L, 1691L,
1698L, 1705L, 1706L, 1721L, 1734L, 1758L, 1768L, 1773L, 1774L,
1775L, 1782L, 1785L, 1789L, 1815L, 1816L, 1817L, 1844L, 1855L,
1861L, 1865L, 1882L, 1936L, 1937L, 1973L, 1977L, 1978L, 1979L,
1982L, 2010L, 2017L, 2024L, 2025L, 2040L, 2053L, 2077L, 2087L,
2092L, 2093L, 2094L, 2101L, 2104L, 2108L, 2134L, 2135L, 2136L,
2163L, 2174L, 2180L, 2184L, 2201L, 2255L, 2256L, 2292L, 2296L,
2297L, 2298L, 2301L, 2329L, 2336L, 2343L, 2344L, 2359L, 2372L,
2396L, 2406L, 2411L, 2412L, 2413L, 2420L, 2423L, 2427L, 2453L,
2454L, 2455L, 2482L, 2493L, 2499L, 2503L, 2520L))
The data for the number of observations is:
num.obs <- structure(list(years.since.peace = structure(c(5L, 5L, 5L, 5L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L), .Label = c("y0",
"y10", "y15", "y20", "diff.y5", "diff.y10", "diff.y15", "diff.y20"
), class = "factor"), strategy.cm6.YP = structure(c(1L, 3L, 4L,
5L, 1L, 3L, 4L, 5L, 1L, 3L, 4L, 5L, 1L, 3L, 4L, 5L), .Label = c("none",
"only offered", "communication/\nfacilitation", "procedural",
"directive", "unspecified"), class = "factor"), num.obs = c(8L,
5L, 14L, 2L, 8L, 5L, 14L, 2L, 8L, 5L, 14L, 2L, 8L, 5L, 14L, 2L
)), .Names = c("years.since.peace", "strategy.cm6.YP", "num.obs"
), row.names = c(NA, -16L), class = "data.frame")
Move the fill aesthetic to geom_bar and change the y position for geom_text should get you what you want.
bar.plot <- ggplot(BarDiff.m.s, aes(x = value.change)) +
geom_bar(aes( fill = incompatibility), binwidth = 1) +
geom_text(data = num.obs, aes(label = paste("obs=", num.obs),y = 4, x = -4)) +
labs(x = "score differences", y = "count / years since start of PSA") +
geom_vline(aes(xintercept = 0), linetype = "dotted") +
theme(plot.title = element_text(face = "bold", size = 10),
legend.position = "bottom") +
scale_fill_brewer(palette = "Set1") +
facet_grid(years.since.peace ~ strategy.cm6.YP, space = "free")
bar.plot
If you want the text labels to be positioned by value.change in the first data set, probably the easiest way to get that is to merge the relevant column into the second data set.

Resources