R ggplot2 Facet wrapping with four boxplots in each plot - r

I have a dataframe called dataw that I'm trying to plot into dual facet wrapped boxplots.
dataw <- structure(list(base = 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, 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, 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, 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, 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, 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), .Label = c("A", "C", "G", "T"), class = "factor"), pos = 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, 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, 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, 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, 1, 1.709,
2.121, 2.061, 1, 5, 6, 1, 0.856, 1.116, 1.207, 76, 45, 5, 15,
8.558, 5.44, 1.147, 10, 7, 40, 5, 1.547, 1.174, 4.777, 7, 0,
1, 8, 1.322, 0.728, 0.83, 4, 2, 0, 0, 1.098, 0.96, 0.63, 13,
22, 16, 1, 1.709, 2.121, 2.061, 3, 6, 7, 11, 0.952, 1.474, 1.45,
13, 22, 16, 1, 1.709, 2.121, 2.061, 3, 8, 15, 2, 1.014, 1.583,
2.289, 10, 3, 8, 2, 1.504, 1.03, 1.244, 4, 1, 0, 1, 1.066, 0.862,
0.689, 2, 0, 0, 1, 0.919, 0.723, 0.479, 7, 8, 0, 0, 1.299, 1.236,
0.779, 13, 22, 16, 1, 1.709, 2.121, 2.061, 45, 38, 41, 1, 2.817,
2.264, 2.398, 3, 0, 1, 14, 0.973, 0.641, 0.846, 13, 22, 16, 1,
1.709, 2.121, 2.061, 7, 0, 0, 1, 1.37, 0.436, 0.706, 0, 5, 5,
1, 0.597, 1.113, 1.079, 3, 1, 4, 8, 0.992, 0.84, 1.07, 17, 7,
18, 1, 2.4, 1.315, 1.948, 21, 8, 50, 12, 2.412, 1.254, 3.857,
13, 22, 16, 1, 1.709, 2.121, 2.061), type = structure(c(2L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L), .Label = c("ipdRatio", "score"), class = "factor")), .Names = c("base",
"pos", "values", "type"), row.names = c("1", "2", "3", "3942",
"3943", "3944", "3945", "11", "21", "31", "63", "64", "65", "66",
"12", "22", "32", "2953", "2954", "2955", "2956", "13", "23",
"33", "2461", "2462", "2463", "2464", "14", "24", "34", "7493",
"7494", "7495", "7496", "111", "212", "311", "4874", "4875",
"4876", "4877", "121", "221", "321", "9356", "9357", "9358",
"9359", "131", "231", "331", "9221", "9222", "9223", "9224",
"15", "25", "35", "93561", "93571", "93581", "93591", "112",
"213", "312", "1579", "1580", "1581", "1582", "122", "222", "322",
"1782", "1783", "1784", "1785", "132", "232", "332", "3398",
"3399", "3400", "3401", "16", "26", "36", "2257", "2258", "2259",
"2260", "113", "214", "313", "1027", "1028", "1029", "1030",
"123", "223", "323", "8654", "8655", "8656", "8657", "133", "233",
"333", "702", "703", "704", "705", "17", "27", "37", "8123",
"8124", "8125", "8126", "114", "215", "314", "93562", "93572",
"93582", "93592", "124", "224", "324", "3700", "3701", "3702",
"3703", "134", "234", "334", "8233", "8234", "8235", "8236",
"18", "28", "38", "1542", "1543", "1544", "1545", "115", "216",
"315", "533", "534", "535", "536", "125", "225", "325", "208",
"209", "210", "211", "135", "235", "335", "93563", "93573", "93583",
"93593"), class = "data.frame")
I'm plotting it like this:
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 = type)) +
geom_boxplot() +
facet_wrap(type ~ pos, scales="free", nrow = 2) +
theme_gray() %+replace% prettify
But I keep getting only one boxplot in each plot square like so, when in fact I want 4 boxplots for each square:
Does anyone see what I am doing wrong here? Thanks!

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, 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, 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, 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, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L,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 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, 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,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L, 3L, 1L, 2L, 2L, 3L, 1L, 2L, 2L, 3L, 1L, 2L, 2L,
3L, 1L, 2L, 2L, 3L), .Label = c("SpA", "SpB", "SpC"), class = "factor"),
quarter = 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, 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))

Negative valued factors in stacked barplot

I am trying to figure out a way of introducing negative values of factors in a stacked barplot in ggplot2. The data is level of support for basic income among Finnish MPs. It is at the bottom of the post.
I can get a plot that is like the one I want (minus the negatively valued factors) with the following code:
library(forcats)
library(ggplot2)
support.plot <- ggplot(mpsupport.df, aes(fct_infreq(Party))) +
geom_bar (aes(fill=Support)) +
coord_flip() +
theme(legend.position = "bottom")+
ylab("Party") +
xlab("Number of MPs")
This gives the following:
What I would like is for the graph to be centred on the green-turquoise border, so that support for basic income was to the right, while opposition was to the left. Does this make sense?
Data:
> dput(mpsupport.df)
structure(list(Party = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 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, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 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, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L,
3L, 3L, 3L, 3L, 3L, 5L, 6L, 7L, 7L, 7L), .Label = c("National Coalition",
"Centre Party", "Social Democratic Party", "Left Alliance", "Christian Democrats",
"True Finns", "Swedish People's Party", "Greens"), class = "factor"),
Support = 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, 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, 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, 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), .Label = c("fully.agree", "partially.agree",
"partially.disagree", "fully.disagree"), class = "factor")), .Names = c("Party",
"Support"), row.names = c("1", "2", "2.1", "2.2", "2.3", "2.4",
"4", "4.1", "4.2", "4.3", "4.4", "4.5", "4.6", "4.7", "6", "8",
"8.1", "8.2", "8.3", "8.4", "8.5", "8.6", "8.7", "8.8", "8.9",
"8.10", "8.11", "8.12", "8.13", "8.14", "9", "9.1", "9.2", "9.3",
"9.4", "9.5", "9.6", "9.7", "10", "10.1", "10.2", "10.3", "10.4",
"10.5", "10.6", "10.7", "10.8", "10.9", "10.10", "10.11", "10.12",
"10.13", "10.14", "10.15", "10.16", "10.17", "10.18", "10.19",
"10.20", "10.21", "10.22", "10.23", "10.24", "10.25", "10.26",
"10.27", "10.28", "10.29", "10.30", "10.31", "10.32", "10.33",
"11", "11.1", "11.2", "11.3", "12", "12.1", "12.2", "12.3", "13",
"14", "14.1", "14.2", "14.3", "14.4", "14.5", "14.6", "14.7",
"14.8", "14.9", "14.10", "14.11", "14.12", "14.13", "14.14",
"14.15", "14.16", "14.17", "14.18", "14.19", "14.20", "15", "15.1",
"17", "17.1", "17.2", "17.3", "17.4", "17.5", "17.6", "17.7",
"17.8", "17.9", "17.10", "17.11", "17.12", "17.13", "17.14",
"17.15", "17.16", "17.17", "17.18", "17.19", "18", "18.1", "18.2",
"18.3", "18.4", "18.5", "18.6", "18.7", "19", "19.1", "19.2",
"19.3", "19.4", "19.5", "19.6", "19.7", "19.8", "19.9", "19.10",
"19.11", "19.12", "19.13", "19.14", "19.15", "19.16", "19.17",
"19.18", "19.19", "19.20", "19.21", "19.22", "19.23", "21", "21.1",
"22", "22.1", "22.2", "22.3", "22.4", "22.5", "22.6", "22.7",
"22.8", "22.9", "22.10", "22.11", "22.12", "23", "23.1", "23.2",
"23.3", "25", "25.1", "25.2", "25.3", "25.4", "25.5", "25.6",
"27", "27.1", "27.2", "27.3", "27.4", "27.5", "29", "30", "31",
"31.1", "31.2"), class = "data.frame")
Try something along these lines:
library(ggplot)
library(forcats)
mpsupport.df$dummy = ifelse(mpsupport.df$Support %in% c("fully.agree", "partially.agree"), 1, -1)
agg = aggregate(dummy ~ Support + Party, data = mpsupport.df, FUN = sum)
ggplot(data = agg)+
geom_bar (aes(y = dummy, x= fct_infreq(Party), fill = factor(Support, levels = c("fully.agree", "partially.agree", "fully.disagree" ,"partially.disagree"))), stat= "identity") +
coord_flip()+
theme(legend.position = "bottom", legend.title = element_blank())

Separating ggplot using rectangles in the background

Here is an image of my plot so far. At the end of the post I provide the code to reproduce it.
For the time being i use horizontal lines to separate the four groups of lines (defined by variable de in the dataframe). But I would like to use colored rectangles in the background of each group. See the following image to get an idea.
I tried geom_rect and geom_tile with no success. Could anybody help me?
mdfr<-structure(list(name = structure(c(13L, 13L, 13L, 14L, 14L, 14L,
1L, 1L, 1L, 10L, 10L, 10L, 7L, 7L, 7L, 2L, 2L, 2L, 15L, 15L,
15L, 8L, 8L, 8L, 11L, 11L, 11L, 16L, 16L, 16L, 4L, 4L, 4L, 12L,
12L, 12L, 9L, 9L, 9L, 17L, 17L, 17L, 5L, 5L, 5L, 6L, 6L, 6L,
3L, 3L, 3L, 13L, 13L, 13L, 14L, 14L, 14L, 1L, 1L, 1L, 10L, 10L,
10L, 7L, 7L, 7L, 2L, 2L, 2L, 15L, 15L, 15L, 8L, 8L, 8L, 11L,
11L, 11L, 16L, 16L, 16L, 4L, 4L, 4L, 12L, 12L, 12L, 9L, 9L, 9L,
17L, 17L, 17L, 5L, 5L, 5L, 6L, 6L, 6L, 3L, 3L, 3L, 13L, 13L,
14L, 14L, 1L, 1L, 10L, 10L, 7L, 7L, 2L, 2L, 15L, 15L, 8L, 8L,
11L, 11L, 16L, 16L, 4L, 4L, 12L, 12L, 9L, 9L, 17L, 17L, 5L, 5L,
6L, 6L, 3L, 3L), .Label = c("10012/06", "541/13", "700-1/15",
"700/13", "737/13", "751/15", "512/12", "579/13", "715/14", "458/07",
"635/13", "705/13, \n705-1/15", "10004/07", "10005/07", "563/09",
"698/16", "717/14"), class = "factor"), Contr.finish = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 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), .Label = c("Initial", "Current",
"Forecast", "Cost"), class = "factor"), variable = 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, 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, 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("start_date", "end_date"
), class = "factor"), value = c("2007-05-30", "2009-03-30", "2016-06-29",
"2007-09-05", "2010-03-05", "2017-09-30", "2006-09-26", "2008-09-26",
"2015-08-31", "2007-11-20", "2011-11-20", "2014-03-20", "2012-01-31",
"2014-07-31", "2016-03-20", "2013-06-21", "2016-06-21", "2016-06-21",
"2009-04-15", "2011-04-15", "2017-12-31", "2013-06-21", "2016-06-21",
"2016-06-21", "2013-12-18", "2016-08-18", "2017-08-18", "2016-04-14",
"2018-02-14", "2018-02-14", "2013-06-03", "2014-10-03", "2016-05-10",
"2013-08-07", "2015-02-07", "2016-06-30", "2014-09-11", "2016-09-11",
"2016-09-11", "2014-09-26", "2016-09-26", "2016-09-26", "2013-03-20",
"2016-03-20", "2016-03-20", "2015-10-09", "2016-08-09", "2016-08-09",
"2015-11-10", "2016-05-10", "2016-05-10", "2009-03-30", "2016-06-29",
"2016-06-29", "2010-03-05", "2017-09-30", "2017-09-30", "2008-09-26",
"2015-08-31", "2016-08-31", "2011-11-20", "2014-03-20", "2015-12-31",
"2014-07-31", "2016-03-20", "2016-12-20", "2016-06-21", "2016-06-21",
"2016-12-30", "2011-04-15", "2017-12-31", "2017-12-31", "2016-06-21",
"2016-06-21", "2018-03-31", "2016-08-18", "2017-08-18", "2018-02-28",
"2018-02-14", "2018-02-14", "2018-02-14", "2014-10-03", "2016-05-10",
"2016-05-10", "2015-02-07", "2016-06-30", "2016-06-30", "2016-09-11",
"2016-09-11", "2017-07-28", "2016-09-26", "2016-09-26", "2016-09-26",
"2016-03-20", "2016-03-20", "2018-10-19", "2016-08-09", "2016-08-09",
"2016-08-09", "2016-05-10", "2016-05-10", "2016-05-10", "2007-05-30",
"2013-09-24", "2007-09-05", "2010-10-21", "2006-09-26", "2016-08-02",
"2007-11-20", "2015-10-19", "2012-01-31", "2015-11-23", "2013-06-21",
"2015-06-09", "2009-04-15", "2014-05-06", "2013-06-21", "2015-03-28",
"2013-12-18", "2015-05-24", "2016-04-14", "2016-04-14", "2013-06-03",
"2016-01-07", "2013-08-07", "2015-12-08", "2014-09-11", "2015-07-24",
"2014-09-26", "2015-06-18", "2013-03-20", "2017-02-22", "2015-10-09",
"2015-10-09", "2015-11-10", "2016-01-06"), bar = c(5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2.5, 2.5, 2.5, 2.5,
2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5,
2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5,
2.5, 2.5, 2.5, 2.5), de = structure(c(4L, 4L, 4L, 4L, 4L, 4L,
1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 2L,
2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L,
2L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L,
4L, 4L, 4L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 4L,
4L, 4L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 3L, 3L,
3L, 2L, 2L, 2L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
4L, 4L, 4L, 4L, 1L, 1L, 3L, 3L, 2L, 2L, 1L, 1L, 4L, 4L, 2L, 2L,
3L, 3L, 4L, 4L, 1L, 1L, 3L, 3L, 2L, 2L, 4L, 4L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("de1", "de2", "de3", "de4"), class = "factor")), row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46",
"47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57",
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68",
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90",
"91", "92", "93", "94", "95", "96", "97", "98", "99", "100",
"101", "102", "110", "410", "710", "103", "131", "161", "191",
"221", "251", "281", "311", "341", "371", "401", "431", "461",
"491", "521", "551", "581", "611", "641", "671", "701", "731",
"761", "791", "821", "851", "881", "911", "941", "971", "1001"
), .Names = c("name", "Contr.finish", "variable", "value", "bar",
"de"), class = "data.frame")
dfr<-structure(list(name = structure(c(2L, 4L, 3L, 1L), .Label = c("10004/07",
"10012/06", "458/07", "512/12"), class = "factor"), text = c("Region 1",
"Region 2", "Region 3", "Region 4"), name0 = c(0, 6.5, 9.5, 12.5
)), .Names = c("name", "text", "name0"), row.names = c(NA, -4L
), class = "data.frame")
library(ggplot2)
library(scales)
library(ggthemes)
ggplot(mdfr, aes(as.POSIXct(as.Date(value, "%Y-%m-%d")), name, colour = Contr.finish)) +
geom_line(aes(size=bar)) +
guides(colour = guide_legend(override.aes = list(size=5)), size="none", fill="none") +
geom_line(size=2.0) +
xlab("") + ylab("") +
theme_stata() +
geom_hline(data=dfr, aes(yintercept = name0), color = "#4d4d4d", size=0.8) + #
scale_fill_brewer(palette="Dark2") +
scale_x_datetime(breaks = date_breaks("1 year"),labels = abbreviate) +
scale_colour_manual(values=c("Initial" = "#67bf5c", "Current" = "#1f77b4",
"Forecast" = "#ff9e4a", "Cost" = "#c10534")) +
theme(legend.position = "bottom",
axis.text.y=element_text(angle=0)
)
You can use geom_rect() and there set xmin= and xmax= to minimal and maximal values of your dates or some other values outside the limits. For the ymin= and ymax= used name values converted to numeric (they have to factors in your dataframe) and then -0.5 and +0.5 (as for each discrete value there is place of 1 around it). Added expand=c(0,0) to scale_x_datetime() to remove white areas.
+ geom_rect(aes(xmin=min(as.POSIXct(as.Date(value, "%Y-%m-%d"))),
xmax=max(as.POSIXct(as.Date(value, "%Y-%m-%d"))),
ymin=as.numeric(name)-0.5,ymax=as.numeric(name)+0.5,
fill=de),alpha=0.05,linetype=0)

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