Specify shape for points in ggplot2 - r

I have a CSV with a Detect column where the result is Y or N. I've got my script to change shape dependent on that column, but I need to specify that Y is a filled in circle, while N is hollow circle.
library("ggplot2")
Report213 <- read.csv("FILE_NAME")
ggplot(data = Report213, aes(x = factor(Station_ID, level = c("NEB","NWB","LBC","WB","HR","FDP","FS","NR","PB")), y = Result, Group = Detect, colour = Station_ID,shape = Detect
)) + geom_point(aes(shape=Detect,size = 2)) +
facet_grid( . ~ Chemical ) +facet_wrap( ~ Chemical, scales= "free_y",ncol = 1) + theme(
panel.background = element_rect(fill = "white",
colour = "white",
size = 0.5, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',
colour = "gray"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
colour = "white"),
strip.background =element_rect(fill="#454545"),
strip.text = element_text(colour = 'white')
)
Appreciate any pointers.
dput output off Report213:
structure(list(Station_ID = structure(c(4L, 4L, 4L, 4L, 4L, 9L,
3L, 9L, 3L, 3L, 9L, 3L, 3L, 5L, 7L, 2L, 6L, 7L, 5L, 7L, 8L, 1L,
5L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L, 1L,
2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 7L,
7L, 7L, 8L, 9L, 9L, 9L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L), .Label = c("FDP",
"FS", "HR", "LBC", "NEB", "NR", "NWB", "PB", "WB"), class = "factor"),
Chemical = 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, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 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, 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("4,4'-DDT", "CHLORDANE", "Total Aroclors",
"Total PAHs", "Total PCB Congeners"), class = "factor"),
Result = c(78.4176, 66.8307, 59.7295, 50.4102, 40.9341, 36.6868,
34.6394, 26.7728, 23.192, 18.091, 15.47568, 14.539, 13.8006,
4.489, 2.0159, 1.99509, 1.71768, 1.69251, 1.5165, 1.39725,
1.27822, 1.22813, 0.89586, 507.7, 135, 684, 8911, 4946, 780,
4920, 137.9, 559.5, 239.51, 902, 376, 655.4, 8299, 6500,
889, 502.8, 361.1, 17440, 555.8, 953, 5691, 1790, 0.3, 1,
14, 12, 20, 20, 21, 10, 14, 7.6, 7.3, 23, 7.7, 11, 1.5, 0.28,
8.1, 5.4, 11, 0.31, 0.62, 20, 22, 4.2, 6.8, 3.9, 6.7, 4.6,
6.4, 13, 51, 4.2, 50.8, 43.1, 41.9, 4.1, 4.4, 3.9, 4, 4.2,
4.5, 2.3, 4.3, 13, 6.8, 35, 1.1, 0.62, 0.053, 1, 7.4, 23,
3.7, 0.056, 2, 0.055, 0.054, 0.12, 0.053, 0.057, 0.13, 0.088,
0.11, 0.058, 1.1, 21, 1.5, 4.7, 1.6), Detect = structure(c(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,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("N", "Y"), class = "factor")), class = "data.frame", row.names = c(NA,
-115L))

You can specify the shape by using scale_shape_manual
P.S: Use either facet_grid or facet_wrap not both at the same time
Edit: with ggplot2 v3.0.0 released in July 2018, you can use text/string to specify the shape. E.g. scale_shape_manual(values = c("circle", "circle open")). See more here
library(tidyverse)
Report213 <- Report213 %>%
mutate(Station_ID = factor(Station_ID,
level = c("NEB","NWB","LBC","WB","HR","FDP","FS","NR","PB")))
ggplot(data = Report213,
aes(x = Station_ID,
y = Result)) +
geom_point(aes(color = Station_ID, shape = Detect), size = 2) +
scale_shape_manual(values = c(19, 1)) +
facet_wrap( ~ Chemical, scales = "free_y", ncol = 1) +
theme(
panel.background = element_rect(fill = "white",
colour = "white",
size = 0.5, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',
colour = "gray"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
colour = "white"),
strip.background =element_rect(fill = "#454545"),
strip.text = element_text(colour = 'white')
)
Edit 2: Add string ~ integer shape table for future references
pch_table <- c(
"square open" = 0,
"circle open" = 1,
"triangle open" = 2,
"plus" = 3,
"cross" = 4,
"diamond open" = 5,
"triangle down open" = 6,
"square cross" = 7,
"asterisk" = 8,
"diamond plus" = 9,
"circle plus" = 10,
"star" = 11,
"square plus" = 12,
"circle cross" = 13,
"square triangle" = 14,
"triangle square" = 14,
"square" = 15,
"circle small" = 16,
"triangle" = 17,
"diamond" = 18,
"circle" = 19,
"bullet" = 20,
"circle filled" = 21,
"square filled" = 22,
"diamond filled" = 23,
"triangle filled" = 24,
"triangle down filled" = 25
)
Data used
Report213 <- structure(list(Station_ID = structure(c(4L, 4L, 4L, 4L, 4L, 9L,
3L, 9L, 3L, 3L, 9L, 3L, 3L, 5L, 7L, 2L, 6L, 7L, 5L, 7L, 8L, 1L,
5L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L, 1L,
2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 7L,
7L, 7L, 8L, 9L, 9L, 9L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L), .Label = c("FDP",
"FS", "HR", "LBC", "NEB", "NR", "NWB", "PB", "WB"), class = "factor"),
Chemical = 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, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 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, 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("4,4'-DDT", "CHLORDANE", "Total Aroclors",
"Total PAHs", "Total PCB Congeners"), class = "factor"),
Result = c(78.4176, 66.8307, 59.7295, 50.4102, 40.9341, 36.6868,
34.6394, 26.7728, 23.192, 18.091, 15.47568, 14.539, 13.8006,
4.489, 2.0159, 1.99509, 1.71768, 1.69251, 1.5165, 1.39725,
1.27822, 1.22813, 0.89586, 507.7, 135, 684, 8911, 4946, 780,
4920, 137.9, 559.5, 239.51, 902, 376, 655.4, 8299, 6500,
889, 502.8, 361.1, 17440, 555.8, 953, 5691, 1790, 0.3, 1,
14, 12, 20, 20, 21, 10, 14, 7.6, 7.3, 23, 7.7, 11, 1.5, 0.28,
8.1, 5.4, 11, 0.31, 0.62, 20, 22, 4.2, 6.8, 3.9, 6.7, 4.6,
6.4, 13, 51, 4.2, 50.8, 43.1, 41.9, 4.1, 4.4, 3.9, 4, 4.2,
4.5, 2.3, 4.3, 13, 6.8, 35, 1.1, 0.62, 0.053, 1, 7.4, 23,
3.7, 0.056, 2, 0.055, 0.054, 0.12, 0.053, 0.057, 0.13, 0.088,
0.11, 0.058, 1.1, 21, 1.5, 4.7, 1.6), Detect = structure(c(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,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L),
.Label = c("N", "Y"), class = "factor")),
class = "data.frame", row.names = c(NA,
-115L))
Created on 2018-06-09 by the reprex package (v0.2.0).

Related

error bars should not be very long in barplots in r

I am plotting grouped barplots with error bars, but my error bars are very long as in this image
[![https://i.stack.imgur.com/VUByO.png][1]][1].
I would like shorter error bars as in this image
[![https://i.stack.imgur.com/JhaUJ.png][2]][2]
The code used
per$Leaf_Location <- factor(per$Leaf_Location, levels = unique(per$Leaf_Location))
per$Time <- factor(per$Time, levels = unique(per$Time))
ggplot(per, aes(x=Leaf_Location, y=Damage, fill=as.factor(Time))) +
stat_summary(fun.y=mean,
geom="bar",position=position_dodge(),colour="black",width=.7,size=.7) +
stat_summary(fun.ymin=min,fun.ymax=max,geom="errorbar",
color="black",position=position_dodge(.7), width=.2) +
stat_summary(geom = 'text', fun.y = max, position = position_dodge(.7),
label = c("a","b","c","d","d","a","b","c","d","d","a","b","c","d","d"), vjust = -0.5) +
scale_fill_manual("Legend", values = c("grey36","grey46","grey56","grey76","grey86","grey96")) +
xlab("Leaf Location") +
ylab("Damage ") +
theme_bw()
data:
per =
structure(list(Site = structure(c(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,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 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("20_days",
"40_days", "60_days", "80_days", "100_days"), class = "factor"),
Damage = c(25.25, 26.07, 24.43, 20.73, 17.8, 6.9, 45.05,
33.47, 24.43, 51.67, 41.72, 34.17, 81.67, 73.33, 55.83, 34.28,
26.08, 13.28, 26.27, 14.1, 6.93, 37.55, 29.33, 23.62, 49.17,
38.45, 31.38, 70.83, 60.83, 44.2, 31.03, 25.2, 14.97, 14.38,
6.5, 4.33, 52.2, 39.17, 30.97, 75, 62.5, 38.33, 87.5, 62.5,
57.5, 45.02, 31.02, 26.07, 46.72, 34.32, 21.5, 50.83, 34.23,
25.25, 45.83, 33.47, 27.7, 67.67, 57.5, 52.67, 30.98, 23.62,
9.1, 18.17, 18.57, 10.15, 46.67, 34.27, 23.62, 54.17, 40.05,
29.37, 70.83, 59.17, 47.53, 8.67, 5.63, 0.87, 9.87, 3.03,
0, 17.75, 6.88, 0, 62.5, 37.5, 27.7, 70.83, 57.5, 50.83,
6.5, 2.17, 1.3, 6.93, 3.03, 0.53, 14.82, 5.2, 0, 37.5, 28.52,
13, 75, 37.5, 37.5, 15.3, 9.53, 5.63, 9.43, 3.03, 0.43, 16.4,
6.07, 0, 57.5, 34.23, 21.98, 78.33, 62.5, 37.5, 12.08, 6.5,
1.3, 10.73, 3.03, 0, 15.2, 3.9, 0.43, 62.5, 37.5, 21.98,
64.17, 55.83, 41.73, 8.73, 3.57, 0, 8.57, 2.17, 0, 16.5,
7.7, 0.43, 42.58, 36.68, 13, 65.83, 47.5, 37.5, 8.03, 5.07,
0.43, 10.68, 7.27, 3.5, 48.38, 38.42, 24.83, 45.03, 38.4,
30.8, 73.33, 63.33, 50.83, 3.37, 2.17, 0.9, 9, 6.02, 5.2,
21.07, 12.37, 6.02, 45.02, 32.65, 21.67, 68.78, 56.68, 50,
0, 0, 0, 7.8, 4.33, 4.33, 25.17, 20.65, 13.15, 48.37, 39.23,
27.17, 75.83, 62.5, 49, 11.78, 12.72, 3.8, 20.18, 14.87,
8.95, 46.7, 39.32, 33.03, 49.18, 40.05, 24.43, 69.17, 60,
48.33, 0, 0, 0, 15.25, 9.82, 7.75, 45.9, 38.47, 35.52, 50.88,
37.61, 33.47, 79.17, 71.67, 58.33)), .Names = c("Site", "Field",
"Leaf_Location", "Time", "Damage"), row.names = c(NA, -225L), class = "data.frame")
Here's a simplified reproducible example to explain
first, some dummy data:
per = data.frame(x=rep(c('a','b'), each=100), y=c(2+rnorm(100), 3+rnorm(100,0,2)))
Now you are plotting the error bars, using fun.ymin=min, fun.ymax=max, which will cause them to extend the full range of the data, as in the following graph:
ggplot(per, aes(x, y)) +
stat_summary(fun.y = mean, geom="bar") +
geom_point(position = position_jitter(0.1)) +
stat_summary(fun.ymin=min, fun.ymax=max, geom="errorbar", width=0.4) +
theme_bw()
Whereas, it is more conventional to use error bars that extend either +/- one standard deviation, as in the following:
ggplot(per, aes(x, y)) +
stat_summary(fun.y = mean, geom="bar") +
stat_summary(
fun.ymin=function(y) {mean(y) - sd(y)},
fun.ymax=function(y) {mean(y) + sd(y)},
geom="errorbar", width=0.2) +
theme_bw()
Or one standard error, like this:
ggplot(per, aes(x, y)) +
stat_summary(fun.y = mean, geom="bar") +
stat_summary(
fun.ymin=function(y) {mean(y) - sqrt(var(y)/length(y))},
fun.ymax=function(y) {mean(y) + sqrt(var(y)/length(y))},
geom="errorbar", width=0.2) +
theme_bw()
EDIT - example data were added to question, after this answer was originally posted
We can applying exactly the same approach as above to your example data:
ggplot(per, aes(x=Leaf_Location, y=Damage, fill=as.factor(Time))) +
stat_summary(fun.y=mean, geom="bar",position=position_dodge(),colour="black",width=.7,size=.7) +
stat_summary(
fun.ymin=function(y) {mean(y) - sqrt(var(y)/length(y))},
fun.ymax=function(y) {mean(y) + sqrt(var(y)/length(y))},
geom="errorbar",
position=position_dodge(.7), width=.2)

How to make create two y-axis labels with a grid of facets with a single x-axis label

I have been struggling with ggplot to display these plots how I would like. My data have 2 factors, quarter and species. Station will be on the x-axis, value on the y-axis, and the constituent will be used with the facet_wrap. I want quarter differentiated with shapes, and species with colors.
The issue is I'm trying to replicate a figure done in SigmaPlot. It is 4x4 grid of plots, with the first two rows of the first column are empty, to allow for the placement of the legend. My original plan was to have two separate facets made using facet-wrap, and combine those, however, this doesn't maintain the 4x4 arrangement, it transforms it into a 1x2, which ruins alignment of plots and shrinks the larger faceted grid.
My next thought was to create each plot individually, then arrange them in a grid using cowplot. This presents the plots how I'd like them arranged, but I can't figure out how to have two y-axis labels, due to different units. One label would be centered on the two leftmost plots, and one centered on the left of the next column of 4 plots.
I'm trying to use this code (just copy the example data below, and run):
library(ggplot)
library(gridExtra)
test.data1 <- test.data[1:95, ]
test.data2 <- test.data[96:111, ]
testplot1 <- ggplot(test.data1, aes(Station, value)) +
geom_point(aes(shape = factor(quarter), fill = Species)) +
scale_shape_manual(values = c(21, 22)) +
labs(x = "Station", y = "Unit a", shape = "Sampling Quarter", fill = "Species") +
theme(legend.position = "none", legend.title = element_blank()) +
guides(fill = guide_legend(override.aes = list(shape = 21), nrow = 2, byrow = TRUE), shape = guide_legend(nrow = 2, byrow = TRUE)) +
facet_wrap( ~ constituent, ncol = 3, scales = "free_y")
testplot2 <- ggplot(test.data2, aes(Station, value)) +
geom_point(aes(shape = factor(quarter), fill = Species))
scale_shape_manual(values = c(21, 22)) +
labs(x = "Station", y = "Unit b", shape = "Sampling Quarter", fill = "Species") +
theme(legend.position = "top", legend.title = element_blank()) +
guides(fill = guide_legend(override.aes = list(shape = 21), nrow = 2, byrow = TRUE), shape = guide_legend(nrow = 2, byrow = TRUE)) +
facet_wrap( ~ constituent, ncol = 1, scales = "free_y")
grid.arrange(testplot2, testplot1, ncol = 2)
Which generates this:
But I want it to be arranged like this, where the XX and YY plots from above are normalized in size with the other plots (this was done using individual plots, and using plot_grid):
Example data from a larger set:
test.data <- structure(list(Station = structure(c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("StA", "StB"), class = "factor"),
CollectionDate = structure(c(3L, 2L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 2L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 2L, 3L, 1L, 3L, 1L,
3L, 1L, 3L, 2L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 2L, 3L, 1L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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, 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("2017 Q2",
"2017 Q4"), class = "factor"), constituent = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L
), .Label = c("A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "XX", "YY"), class = "factor"), value = c(16,
35, 46, 23, 40, 19, 9, 50, 0.2, 1, 0.5698, 0.322, 1, 0.45,
0.322, 0.5, 16, 9, 6, 19, 14, 13, 16, 9, 0, 0.004, 0, 0.004,
1, 0.32, 1, 0.678, 0, 0.39, 0.23, 0, 0, 1.1, 0.5, 0.5, 9,
4.9, 7, 4.768, 9, 8.65, 4.768, 6.54, 195, 195, 46, 46, 124,
124, 218, 218, 2, 1, 1, 1, 1, 2, 1, 1, 0.1, 0.4, 0.22, 0.4,
0.22, 0.4, 0.22, 0.1, 0.99, 0.99, 1.2, 0.45, 0.765, 0.99,
0.99, 0.99, 0.99, 1.2, 4.3, 0.98, 0.99, 1.2, 1.2, 34, 34,
65, 98, 150, 34, 65, 65, 2, 0, 4, 1.3, 5, 3.3, 1.56, 1, 9,
0.36, 4, 4, 11, 2, 2.22, 11)), class = "data.frame", row.names = c(NA,
-111L))

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,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L, 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"))

boxplot with multiple factor labels using base R functions

How can one possibly reproduce the ggplot-based boxplot shown in this answer but using base R boxplot function?
Sample date from the above link:
d<-data.frame(x=rnorm(1500),f1=rep(seq(1:20),75),f2=rep(letters[1:3],500))
# first factor has 20+ levels
d$f1<-factor(d$f1)
# second factor a,b,c
d$f2<-factor(d$f2)
boxplot(x~f2*f1,data=d,col=c("red","blue","green"),frame.plot=TRUE,axes=FALSE)
It would be great if the groups on the x-axis are spaced from each other.
I have limited knowledge about ggplot2.
EDIT
While waiting for more suggestions using base R functions, I am making some progress with ggplot2.
Using this sample data how can I produce a plot with well aligned x-axis as the one in the link above?
The following does not give me the correct alignment (I want the numbers 1:8 aligned at the center of each group):
library(ggplot2)
ggplot(dat3, aes(x = ID, y = value, group=interaction(obs, ID), fill=obs)) +
geom_boxplot() +
scale_fill_manual(values = c("yellow", "orange"))
dat3=structure(list(values = c(0, 0, 0, 0, 0, 0, 0, 0, -0.0169491525423729,
0, 0, 0, 0, 1, 1, 0.64367816091954, 0.64367816091954, 0, 0, -0.0163934426229508,
-0.021978021978022, 0.109195402298851, 0, 0, 0, 0, 0.207650273224044,
0.4375, 0, 0, 0, 0, 0.302325581395349, 0.303370786516854, 0.270588235294118,
-0.0188679245283019, 0.156462585034014, 0.092436974789916, 0.69,
-0.021978021978022, 0.64367816091954, 0.614906832298137, 0.612903225806452,
0.274853801169591, 0, 0.303370786516854, 0, 0, -0.03125, 0.229813664596273,
0.557142857142857, 0, 0.109195402298851, 0.0746268656716418,
0.180616740088106, 0.210526315789474, 0.310344827586207, 1, 1,
0.0825688073394495, 0.294117647058824, 0, 0.4375, 0, 0.230769230769231,
0.347826086956522, -0.0163934426229508, 0.156462585034014, 0,
0, 0, 1, 0, 0, 0, 0.483333333333333, 0.483333333333333, 0, 0,
0, 0, 0, -0.0169491525423729, 0, 0.310344827586207, 0, 0.296875,
0.302325581395349, 0, 0, 0, 0, 0, 0, 0.482758620689655, 0, 0,
0, 0, 0, 0, 0, 0, 0.150684931506849, 0.150684931506849, 0, 0,
-0.021978021978022, -0.021978021978022, 0.270588235294118, 0,
0, 0.482758620689655, 0.482758620689655, 0.272727272727273, 0.272727272727273,
0, 1, 0, 0, 0.642857142857143, 0.211864406779661, 0.156462585034014,
-0.0449438202247191, -0.0449438202247191, 0.389763779527559,
0.389763779527559, -0.021978021978022, 0.211864406779661, 0.213197969543147,
0.213197969543147, 0.358620689655172, -0.0163934426229508, 0.483333333333333,
0, 0, 0.362139917695473, 0.362139917695473, 0.261904761904762,
0.483333333333333, 1, 1, 0.236453201970443, 0.302325581395349,
0.310344827586207, 1, 1, 0.358974358974359, 0.358974358974359,
-0.0606060606060606, 0.0721649484536082, 0.615384615384615, 0.615384615384615,
0.347826086956522, 1, 0, 0, 0, -0.0273972602739726, -0.0273972602739726,
-0.0169491525423729, -0.0256410256410256, 0.107142857142857,
0.107142857142857, 0.302325581395349, -0.0163934426229508, -0.0264900662251656,
0.311111111111111, 0.311111111111111, 0.156462585034014, 0.156462585034014,
-0.0483091787439614, 0.311111111111111, -0.0333333333333333,
-0.0333333333333333, 0.311111111111111), ind = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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L, 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("obs",
"capa"), class = "factor")), .Names = c("values", "ind", "ID",
"obs"), row.names = c(NA, 176L), class = "data.frame")
You can specify the location of the boxes using at option.
set.seed(1)
d<-data.frame(x=rnorm(1500),f1=rep(seq(1:20),75),f2=rep(letters[1:3],500))
# first factor has 20+ levels
d$f1<-factor(d$f1)
# second factor a,b,c
d$f2<-factor(d$f2)
boxplot(x~f2*f1,data=d, at = (1:80)[-4*(1:20)], col=c("red","blue","green"),frame.plot=TRUE,axes=FALSE)
axis(1,at=seq(2,80,4),labels=1:20,cex.axis=0.7)

Changing labels R ggplot in two variable facet wrapped plot

I'm trying to create a facet wrapped ggplot boxplot with dataframe dataw and I'm trying to modify the labels of each subplot.
dataw <- structure(list(base = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L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L, 3L, 3L, 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, 4L, 4L, 4L, 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, 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, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L), values = c(13, 22, 16, 21, 52, 1,
1.709, 2.121, 2.061, 2.233, 3.388, 1, 5, 6, 6, 2, 1, 0.856, 1.116,
1.207, 1.175, 0.95, 76, 45, 5, 1, 1, 15, 8.558, 5.44, 1.147,
0.857, 0.831, 10, 7, 40, 4, 10, 5, 1.547, 1.174, 4.777, 1.071,
1.356, 7, 0, 1, 6, 1, 8, 1.322, 0.728, 0.83, 1.178, 0.831, 4,
2, 0, 1, 3, 0, 1.098, 0.96, 0.63, 0.888, 1.013, 13, 22, 16, 21,
52, 1, 1.709, 2.121, 2.061, 2.233, 3.388, 3, 6, 7, 2, 9, 11,
0.952, 1.474, 1.45, 0.967, 1.306, 13, 22, 16, 21, 52, 1, 1.709,
2.121, 2.061, 2.233, 3.388, 3, 8, 15, 0, 5, 2, 1.014, 1.583,
2.289, 0.773, 1.135, 10, 3, 8, 1, 4, 2, 1.504, 1.03, 1.244, 0.884,
1.047, 4, 1, 0, 2, 5, 1, 1.066, 0.862, 0.689, 0.963, 1.125, 2,
0, 0, 2, 0, 1, 0.919, 0.723, 0.479, 0.922, 0.721, 7, 8, 0, 8,
7, 0, 1.299, 1.236, 0.779, 1.298, 1.224, 13, 22, 16, 21, 52,
1, 1.709, 2.121, 2.061, 2.233, 3.388, 45, 38, 41, 13, 34, 1,
2.817, 2.264, 2.398, 1.374, 3.848, 3, 0, 1, 1, 2, 14, 0.973,
0.641, 0.846, 0.866, 0.909, 13, 22, 16, 21, 52, 1, 1.709, 2.121,
2.061, 2.233, 3.388, 7, 0, 0, 1, 2, 1, 1.37, 0.436, 0.706, 0.685,
0.902, 0, 5, 5, 0, 7, 1, 0.597, 1.113, 1.079, 0.71, 1.222, 3,
1, 4, 0, 23, 8, 0.992, 0.84, 1.07, 0.762, 2.399, 17, 7, 18, 6,
10, 1, 2.4, 1.315, 1.948, 1.135, 1.306, 21, 8, 50, 4, 6, 12,
2.412, 1.254, 3.857, 1.075, 1.168, 13, 22, 16, 21, 52, 1, 1.709,
2.121, 2.061, 2.233, 3.388), type = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 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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, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 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, 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,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L
), .Label = c("D<U+2192>", "G<U+2192>", "A<U+2192>", "K<U+2192>",
"C<U+2192>", "T<U+2192>"), class = "factor")), .Names = c("base",
"pos", "values", "type", "labels"), row.names = c("1", "2", "3",
"4", "5", "3942", "3943", "3944", "3945", "3946", "3947", "11",
"21", "31", "41", "51", "63", "64", "65", "66", "67", "68", "12",
"22", "32", "42", "52", "2953", "2954", "2955", "2956", "2957",
"2958", "13", "23", "33", "43", "53", "2461", "2462", "2463",
"2464", "2465", "2466", "14", "24", "34", "44", "54", "7493",
"7494", "7495", "7496", "7497", "7498", "111", "214", "311",
"411", "511", "4874", "4875", "4876", "4877", "4878", "4879",
"121", "221", "321", "421", "521", "9356", "9357", "9358", "9359",
"9360", "9361", "131", "231", "331", "431", "531", "9221", "9222",
"9223", "9224", "9225", "9226", "15", "25", "35", "45", "55",
"93561", "93571", "93581", "93591", "93601", "93611", "112",
"215", "312", "412", "512", "1579", "1580", "1581", "1582", "1583",
"1584", "122", "222", "322", "422", "522", "1782", "1783", "1784",
"1785", "1786", "1787", "132", "232", "332", "432", "532", "3398",
"3399", "3400", "3401", "3402", "3403", "16", "26", "36", "46",
"56", "2257", "2258", "2259", "2260", "2261", "2262", "113",
"216", "313", "413", "513", "1027", "1028", "1029", "1030", "1031",
"1032", "123", "223", "323", "423", "523", "8654", "8655", "8656",
"8657", "8658", "8659", "133", "233", "333", "433", "539", "702",
"703", "704", "705", "706", "707", "17", "27", "37", "47", "57",
"8123", "8124", "8125", "8126", "8127", "8128", "114", "217",
"314", "414", "514", "93562", "93572", "93582", "93592", "93602",
"93612", "124", "224", "324", "424", "524", "3700", "3701", "3702",
"3703", "3704", "3705", "134", "234", "334", "434", "5310", "8233",
"8234", "8235", "8236", "8237", "8238", "18", "28", "38", "48",
"58", "1542", "1543", "1544", "1545", "1546", "1547", "115",
"218", "315", "415", "515", "533", "534", "535", "536", "537",
"538", "125", "225", "325", "425", "525", "208", "209", "210",
"211", "212", "213", "135", "235", "335", "435", "5311", "93563",
"93573", "93583", "93593", "93603", "93613"), class = "data.frame")
These are the first few rows of dataw
head(dataw)
base pos values type labels
1 A 1 13 score D<U+2192>
2 A 1 22 score D<U+2192>
3 A 1 16 score D<U+2192>
4 A 1 21 score D<U+2192>
5 A 1 52 score D<U+2192>
3942 A 1 1 score D<U+2192>
I'm plotting it like so.
prettify <- theme(panel.background = element_rect(fill = NA,color="gray"),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(size=.1, color="black",linetype="dotted"),
panel.grid.minor.y = element_blank(),
panel.grid.minor.x = element_line(size=.1, color="black"),
legend.position="bottom")
ggplot(dataw,aes(x = base, y = values, color = type, group = base)) +
geom_boxplot() +
facet_wrap(type ~ pos, scales="free_y", nrow = 2) +
theme_gray() %+replace% prettify
Currently the sublabels are the type value followed by a comma and the pos value. However I would like to get rid of the type value, and label it so that the labels of each subplot are in the format: "Position [pos value], [labels value]"
What would be the best way to go about this? Thank you.
Try replacing the entire ggplot statement with
ggplot(data=transform(dataw, plt_labels = paste("Position ", pos, ", ", labels, sep="")),aes(x = base, y = values, color = type, group = base)) +
geom_boxplot() +
facet_grid(type ~ plt_labels, scales="free_y") +
theme_gray() %+replace% prettify
which should give

Resources