error bars should not be very long in barplots in r - 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, 1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L, 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)

Related

changing the colour of symbols in sclass plot

I would like to create a plot displaying two variables, with groupings (ellipses) for one and symbols coloured for the other. "RW1" on the x axis, "RW2" on the y axis, grouping ellipses according to "Morph.cluster" and colouring the geometric shapes according to "ALL", which is the genetic cluster. I'm using ade4 in the adegenet package for this. The issue is that I can only seem to get the plot working for one variable at a time.
My data looks like
ID ggpop AC70 AC50 ALL Morph.cluster RW1 RW2 RW3
LT-103 gg3 NA 2 2 1 0.00793 0.01137 -0.00379
LT-109 gg4 1 1 1 2 -0.0156 -0.01056 0.00869
LT-110 gg3 1 1 1 1 0.01585 0.00077 -0.00923
LT-12 gg5 4 4 4 1 0.01299 0.00383 0.00517
LT-122 gg4 NA 3 3 1 0.02727 0.01845 -0.0078
For some samples ggpop is NA. For these samples I hope that I I can just leave the colour empty. If not, I'll just make another gg category and label these ones as empty.
Using
library(ade4)
plot_All = plot(RWggclust2_eds$RW1,RWggclust2_eds$RW2, type="n", yaxt="n",
ylab="RW2 (17%)", xlab="RW1 (26%)")
axis(2, las=2)
myCol2=transp(c('#7F7F7FFF', '#D99694FF', '#00B0F0FF', '#00B050FF', '#000000FF'), 0.6)
abline(h=0,v=0,col="grey", lty=2)
s.class(cbind(RWggclust2_eds$RW1, RWggclust2_eds$RW2),
fac=as.factor(RWggclust2_eds$Morph.cluster),
add.plot=T,
col=myCol2,
cpoint=2,
clabel=0,
pch=c(15,16,17) [as.numeric(RWggclust2_eds$Morph.cluster)],
axesell=F,
addaxes=F,
cstar=0,
cellipse = 1.5)
I get the above with the ellipses drawn the way that I want, defining the morphological clusters, but the colours of the geometric shapes are not what I am looking for. I would like the colours to be according to the five groups defined in “ALL”, which are the genetic clusters.
However, using
RWggclust2_eds <- read.csv("RWggclust2_eds.csv")
str(RWggclust2_eds)
RWggclust2_eds$ALL = as.factor(RWggclust2_eds$ALL)
RWggclust2_eds$ALL = as.factor(RWggclust2_eds$ggpop)
RWggclust2_eds$Morph.cluster = as.factor(RWggclust2_eds$Morph.cluster)
plot_All = plot(RWggclust2_eds$RW1,RWggclust2_eds$RW2, type="n", yaxt="n",
ylab="RW2 (17%)", xlab="RW1 (26%)")
axis(2, las=2)
myCol2=transp(c('#7F7F7FFF', '#D99694FF', '#00B0F0FF', '#00B050FF', '#000000FF'), 0.6)
abline(h=0,v=0,col="grey", lty=2)
s.class(cbind(RWggclust2_eds$RW1, RWggclust2_eds$RW2),
fac=as.factor(RWggclust2_eds$ALL),
add.plot=T,
col=myCol2,
cpoint=2,
clabel=0,
pch=c(15,16,17) [as.numeric(RWggclust2_eds$Morph.cluster)],
axesell=F,
addaxes=F,
cstar=0,
cellipse = 1.5)
I get the above, with ellipses drawn on the genetic clusters (the “ALL” column in my dataset), which is not what I want (I want the shapes to be grouped according to my first plot), but the colours in the plot are correct.
I've tired all sorts of iterations of the "col" argument as well
#index=as.integer(RWggclust2_eds$ALL)
#sclasscolour=rainbow(6)
#sclasscolour=transp(c('#7F7F7FFF', '#D99694FF', '#00B0F0FF', '#00B050FF', '#000000FF'), 0.6)
#myCol2= transp(c(sclasscolour), 1) [index]
The issue is I think that the colour of symbols is being read from "fac", but is seems like the distrubtion of Morph.cluster is as well.
I would be glad to post my datafile if there is a way to do so.
You are almost there. I think in s.class, the color of the points will go together with the eclipse (at least as far as I have tried).
Since you had to layer the eclipse onto an empty plot, I think the easiest solution is to first plot the points according to what you like, then add on the eclipses without the points (setting pch=NA)
I used alpha from ggplot for the transparency but feel free to change it back
library(ade4)
library(ggplot2)
# i use alpha from ggplot2
# how i read and proceeded with your csv file
#CSV = read.csv("RWggclust2_eds.csv")
#RWggclust2_eds = CSV[,c("ggpop","ALL","Morph.cluster","RW1","RW2")]
# similar to what you did
# just providing names
myCol2=alpha(c('#7F7F7FFF', '#D99694FF', '#00B0F0FF', '#00B050FF', '#000000FF'), 0.6)
# set it according to popn
names(myCol2) = levels(RWggclust2_eds$ggpop)
# set the shapes
PCH = c(15,16,17)
# we can wrap it around using with
plot_All = with(RWggclust2_eds,
plot(RW1,RW2, yaxt="n",
ylab="RW2 (17%)", xlab="RW1 (26%)",
pch= PCH[Morph.cluster],
col=myCol2[as.character(ggpop)]
)
)
#provide a legend
legend("topleft",names(myCol2),fill=myCol2,horiz=T,cex=0.7)
axis(2, las=2)
abline(h=0,v=0,col="grey", lty=2)
s.class(cbind(RWggclust2_eds$RW1, RWggclust2_eds$RW2),
fac=as.factor(RWggclust2_eds$Morph.cluster),
add.plot=T,
cpoint=2,
clabel=0,
pch=NA,
axesell=F,
addaxes=F,
cstar=0,
cellipse = 1.5)
You get the plot:
Below is the data for making the plot
dput(RWggclust2_eds)
structure(list(ggpop = structure(c(3L, 4L, 3L, 5L, 4L, 1L, 3L,
3L, 1L, 1L, 4L, 3L, 5L, 1L, 3L, 1L, 1L, 1L, 1L, 4L, 3L, 1L, 1L,
3L, 1L, 3L, 3L, 5L, 3L, 1L, 3L, 1L, 3L, 3L, 3L, 1L, 4L, 3L, 1L,
4L, 5L, 3L, 4L, 1L, 2L, 2L, 5L, 5L, 4L, 4L, 2L, 5L, 2L, 2L, 3L,
3L, 1L, 1L, 3L, 4L, 1L, 3L, 1L, 1L, 1L, 4L, 1L, 2L, 4L, 1L, 5L,
1L, 4L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 4L, 4L, 5L, 5L, 3L, 3L, 1L,
1L, 3L, 2L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 5L,
3L, 1L, 3L, 4L, 4L, 1L, 1L, 3L, 4L, 3L, 1L, 1L, 4L, 1L, 1L, 2L,
4L, 1L, 4L, 5L, 5L, 1L, 1L, 1L, 1L, 4L, 1L, 3L, 1L, 3L, 1L, 1L,
2L, 3L, 1L, 1L, 5L, 4L, 4L, 3L, 2L, 4L, 3L, 3L, 3L, 2L, 3L, 2L,
3L, 3L, 2L, 4L, 2L, 1L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 5L, 1L, 1L,
1L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L, 3L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 3L,
2L, 2L, 3L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 3L, 2L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 3L, 2L, 2L,
2L, 1L, 2L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 3L,
1L, 2L, 1L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,
2L, 3L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L,
3L, 1L, 3L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 2L), RW1 = c(0.00793,
-0.0156, 0.01585, 0.01299, 0.02727, 0.00344, 0.031, 0.01568,
-0.00411, -0.00076, 0.01339, 0.02017, 0.02123, -0.0061, 0.01376,
-0.02301, 0.00501, 0.02418, -0.00102, -0.00813, 0.0032, -0.0122,
-0.00317, 0.00018, 0.00051, -0.00184, -0.00323, 0.01701,
-0.00027, 0.00288, -0.00689, -0.01864, 0.003, 0.00463, 0.0119,
-0.01515, 0.00364, 0.02048, -0.01233, -0.00774, -0.00348,
-0.00219, 0.00676, -0.00978, 0.00767, -0.00819, -0.01892,
-0.00584, 0.012, 0.00617, -0.01743, -0.02724, -0.03363, 0.01004,
0.02196, 0.01074, 0.0053, 0.00197, -0.01368, 0.00802, -0.00659,
-0.00521, -0.00938, 0.00901, 0.00639, 0.02151, -0.01637,
-0.02571, -0.00354, -0.00396, 0.03228, 0.01599, -0.00646,
0.00993, 0.02582, -0.01889, 0.01162, 0.00487, 0.02127, -0.00994,
-0.01862, 0.01338, 0.00229, 0.01818, 0.00687, 0.00545, 0.01723,
0.00438, 0.00832, 0.00095, -0.00751, -0.00645, -0.01469,
-0.01141, 0.01079, -0.00567, 0.00348, 0.00904, 0.00055, 0.02002,
-0.00382, -0.00244, 0.03351, -0.01225, 0.00865, -0.00105,
-0.01337, 0.00853, -0.00584, -0.001, -0.00068, -0.01427,
0.00742, 0.00352, -0.01016, -0.00713, -0.00129, -0.01557,
-0.00792, -0.00067, 0.00432, -0.01509, 0.03477, 0.01061,
-0.00097, 0.00797, -0.01066, -0.01534, 0.0192, 0.00266, -0.00549,
-0.02314, 0.00139, -0.02194, -0.00406, 0.00287, -0.01561,
-0.00329, 0.01625, 0.00822, 0.01719, -0.00545, 0.0019, 0.00278,
0.00436, 0.01424, -0.01102, -0.01331, 0.00928, 0.01181, 0.00416,
0.01673, 0.00715, -0.0159, -0.00208, -0.03367, 9e-04, -0.00011,
-0.02768, -0.01608, -0.00345, 0.00062, 0.00048, -0.01542,
0.02665, -0.0181, 0.0098, -0.00995, 0.00842, 0.00123, 0.00896,
0.02197, -0.00163, -0.01072, -0.01137, -0.01126, -0.01656,
0.00518, 0.03913, -0.02236, -0.02881, -0.0117, 0.01644, -0.00465,
-0.00811, 0.00107, 0.02483, 0.00197, -0.01018, 0.01023, 0.01279,
-0.01052, -0.0069, -0.01409, 0.01462, 0.00435, 0.01912, -0.01392,
0.02166, 0.00621, 0.0112, -0.01114, 0.00728, -0.0086, 5e-05,
0.00352, -0.00939, -0.01502, 0.00394, 0.01684, -0.00077,
-0.00037, 0.00563, -0.03636, 0.00443, 0.01819, 0.00603, -0.00363,
-0.02126, 0.01857, 0.00014, -0.03048, 0.00621, -0.00452,
0.02451, 0.00715, -0.00705, 0.01619, -0.016, -0.00362, -0.00949,
0.02046, -0.016, -0.01655, 0.00079, 0.00188, -0.01497, 0.01269,
0.02089, -0.01006, -0.01574, -0.00349, -0.01533, 0.01438,
0.0055, -0.00257, 0.00254, -0.00647, -0.00356, 0.02158, -0.03029,
0.01802, -0.00391, -0.01141, -0.0355, 0.00911, 0.00136, 0.00631,
-0.00551, -0.01403, 0.00929, 0.0059, -0.00238, 0.00028, -0.01683,
0.00347, -0.0213, 0.00411, 0.00953, 0.00029, -0.0028, 0.01801,
0.00405, -0.0079, -0.01496, -0.00638, -0.01092, -0.00773,
-0.00368, 0.00526, -0.00739), RW2 = c(0.01137, -0.01056,
0.00077, 0.00383, 0.01845, 0.00058, 0.02422, 0.00129, -0.01173,
-0.0161, 0.01019, 0.00135, 0.00579, 0.00576, 0.00751, 0.01384,
0.00408, -0.00634, -0.00044, -0.00168, -0.00021, 0.00019,
0.00428, -0.00924, 0.00098, 0.00518, -0.00184, -0.00469,
-0.00921, -3e-05, 0.00688, -0.00778, 0.00165, -0.00664, 0.00856,
-0.00157, 0.00322, -7e-05, -0.00405, 6e-04, -0.00464, -0.0019,
-0.00849, 0.00363, -0.01107, 0.00175, -0.03478, -0.00816,
-0.00338, 0.02075, -0.00039, -0.02465, -0.01461, 0.00529,
-0.00144, -0.00259, -0.00803, -0.00693, 0.00761, 0.01159,
0.01422, -0.00876, -0.00469, -0.00052, 0.00823, 0.01019,
0.00032, 0.00842, 0.02197, 4e-04, -0.02939, 0.00432, 0.00774,
0.00848, -0.00342, -0.00953, -0.01048, -0.0014, -0.01465,
0.0027, 0.00879, -4e-05, 0.00985, -0.03324, 0.01427, 0.00036,
-0.01587, -0.0012, 0.00331, 0.0089, -0.00684, -0.0348, 0.00497,
0.01468, -0.00218, -0.01662, 0.00608, -0.01143, 0.00312,
-0.01165, 0.00966, 0.00179, -0.01137, 0.00335, 0.0152, -0.00442,
0.01031, -0.01745, -0.01658, 0.003, 0.011, -0.00583, 0.0171,
0.00062, 0.00842, 0.00759, -0.00401, 0.01916, -0.00491, 0.01416,
0.00117, -0.00164, -0.02815, 0.00648, 0.00067, -0.006, -0.00454,
0.00368, 0.00666, 0.00352, 0.00368, -0.00999, 0.01213, -0.0022,
-0.0015, -0.02383, 0.00748, -4e-05, -0.02224, -0.01355, 0.00525,
0.00142, 0.01344, 0.00639, 0.00822, 0.01765, 0.00431, -0.0019,
0.00204, 0.03139, 0.00251, -0.00123, -0.00833, -0.01289,
0.00716, 0.00198, 0.00263, 0.00632, 0.00179, -5e-05, 0.0088,
-0.0017, -0.00574, 0.00356, -0.02567, 0.00984, 0.00209, 0.0072,
0.00377, -0.0085, 0.01386, 0.00746, 0.00415, -0.01666, 0.00516,
0.00883, -0.01979, 0.01931, 0.00208, 0.00411, 0.01303, -0.00108,
0.00854, -0.00262, -0.02486, 0.0179, -0.00035, 0.01651, 0.00952,
0.00668, -0.00279, 0.00344, 0.00314, -0.00599, 0.00954, -0.02629,
-0.00226, 0.00282, 0.00765, 0.00624, -0.00497, -0.00269,
0.01129, -0.02122, 0.00841, -0.0011, -0.00747, 0.01913, -0.0024,
0.01528, -0.00184, -0.005, -0.01863, 0.02115, -0.00814, 0.01531,
0.00641, -0.00111, -0.01767, 0.01528, -0.00414, -0.01503,
-0.00082, 0.00429, 0.00032, 0.01733, -0.00656, 0.00176, -0.00597,
0.00423, -0.00541, 0.01085, -0.00327, -4e-04, 0.01826, -0.00852,
0.0098, -0.01052, 0.0188, 0.00234, -0.00113, -0.00335, -0.01121,
-0.00305, 0.0065, -0.0032, -0.00877, -0.01151, -0.01051,
-0.00295, 0.00751, -0.00828, -0.00164, -0.00109, 0.01665,
0.01285, -0.00875, -0.01182, -0.01427, 0.01631, -0.00241,
-0.0055, 0.00393, 0.00351, 0.00061, -0.00063, 0.01053, -0.00094,
0.00509, 0.00162, 0.00295, -0.00789, -0.03162, 0.00523, -0.00361,
-0.00171, -0.00459, 0.00237, -0.03454, -0.01103, 0.00944)), class = "data.frame", row.names = c(NA,
-281L))

interpret estimated marginal means (emmans aka lsmeans): negative response values

I am working on a a model with lmer where I would like to get estimated marginal means with the emmeanslibrary. This is my dataframe:
df <- structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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("CCF", "UN"), class = "factor"), level = structure(c(2L,
3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L,
4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L,
2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L,
3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L,
4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L,
2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L, 2L,
3L, 4L, 2L, 3L, 4L, 2L, 3L, 4L), .Label = c("A", "F", "H", "L"
), class = "factor"), random = structure(c(3L, 3L, 3L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 4L,
4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 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, 5L), .Label = c("1.6", "2", "3.2", "5", NA), class = "factor"),
continuous = c(72.7951770264767, 149.373765810534, 1.64153094886205,
54.6697408615215, 25.5801835808851, 1.45794117443253, 25.3660934894788,
91.2321704897132, 2.75353217433675, 44.1995276851725, 33.1854545470435,
5.36536076058866, 29.6807620242672, 80.6077496067764, 0.833434180091457,
13.6789475327185, 77.4930412025109, 3.65998714174906, 25.2848344605563,
136.632099849828, 2.56715261161435, 28.6733878840584, 66.800616194317,
1.37475468782539, 23.007491380183, 84.980285774607, 1.13569710795522,
33.8610875632139, 56.1234827517798, 1.32327007970416, 60.0843812879313,
43.4487832450889, 1.14942423621912, 53.6673704529947, 146.746167255051,
3.91593723271292, 27.0321687961004, 89.5925729244878, 1.47707078226047,
44.0523211310831, 115.087908243373, 1.94039630728038, 86.4074806697431,
43.3266206881612, 2.81456503996437, 66.868588961071, 229.797526052566,
1.07971524769264, 30.3390107111747, 116.680801084036, 1.67711446647817,
69.0961010697534, 78.5454363192614, 1.92137892126384, 53.5708546850303,
37.7175476710608, 1.96087397451467, 25.5166981770257, 37.3755071788757,
2.21602000526086, 10.3266195584378, 38.1458490762217, 2.7508022340832,
44.5864920143771, 8.45382647692274, 2.63204944520792, 87.5376946978593,
27.2354119098268, 3.38134648323956, 26.8815471706502, 14.5539972194568,
2.0556994322415, 27.4619977737491, 32.8546665896602, 2.66809379088059,
42.3815445857533, 21.3359802201685, 2.19167325121191, 53.3189825439001,
13.5708790223439, 2.22274607227071, 88.297423835906, 8.50554349658773,
3.5764241495006, 29.284865737912, 21.1213079519954, 2.3070166819956,
10.7659615128225, 33.4813413290485, 2.49896565066211, 59.0935696616465,
13.2863515051715, 4.36424795471221, 72.1627847396763, 9.09326343200557,
2.13701784901259, 27.5824079679471, 8.84486812842272, 1.98293342019671,
17.5321126287485, 19.1806349705231, 5.03952187899644, 58.3473975730234,
9.17287686145614, 2.99575072457674)), class = "data.frame", row.names = c(NA,
105L))
This is my model:
library(lme4)
model <- lmer((continuous) ~ treatment + level + (1|random), data= df, REML = TRUE)
The data as it is does not meet the model assumptions, but still I am wondering why I get a negative estimated marginal mean (response) on treatment "UN" level "L" (see lettering table) when I don't have any negative numbers in df$continuous?
library(multcompView)
library(emmeans)
lsm.mixed_C <- emmeans::emmeans(my_model,pairwise ~ treatment * level, type="response")
lettering <- CLD(lsm.mixed_C,alpha=0.05,Letters=letters,
adjust= "tukey")
The short answer is because you badly need to include the interaction in your model. Compare:
model2 <- lmer((continuous) ~ treatment * level + (1|random),
data= df, REML = TRUE)
emmip(model2, treatment ~ level)
with:
emmip(model, treatment ~ level)
In model2, both EMMs at level L are close to zero. If you remove the interaction from the model, you force those two profiles to be parallel, while maintaining a sizeable positive difference between treatments CCF and UN, forcing the estimate for UN to go negative. In actual fact, though, all six estimates for treatment x level combinations are seriously distorted.
I can't repeat it enough. emmeans() summarizes a model. If you give it a bad model, you get dumb results. Thanks for the great illustration of this point.

Sort ggplot boxplots by median with facets

I'm trying to get ggplot to order my boxplots based on median value after splittin the data into several different facets.
This is part of a larger Shiny app I've written. Under default parameters, I can generate three faceted boxplots that order correctly:
boxData <- structure(list(Classification = structure(c(4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L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"Endoderm", "Mesoderm", "Ectoderm"), class = c("ordered", "factor"
)), value = c(0.000255214868214152, 0.000108050996652777, 0.00751505823956855,
8.71801689770664, 5.71059263813113e-05, 4.90291746067526e-05,
0.000129388767504551, 2.52712436532327e-07, 5345.09546573398,
0.0020991194782334, 4.33360773005175e-06, 1.8200776481618, 3.44754305553851e-06,
4.38932775031697, 0.00720892572385782, 7.53517216121544e-05,
0.221288441144887, 0.00104230990042965, 0.00288742662358172,
4.20947546944294e-05, 9.62973878475845e-07, 0.00710967831313203,
26.9833955280036, 0.00265697432110539, 1.41814003567946, 0.261340025051291,
0.00159083508412152, 9.55044905589291e-06, 0.0122931632086495,
8.54789134364452e-06, 2.01899938950824e-05, 1.55354988683742e-06,
0.000441285511108929, 0.000353500530366103, 0.125347054487635,
109.440278770173, 2.03304264082645e-05, 2.01899938950824e-05,
0.000148628664387571, 2.89902659683517e-06, 207.073625180606,
3.52469070261441e-07, 3.15047327017105e-06, 0.639049681601525,
2.11937734339159e-05, 0.484309094613314, 0.0126387710681522,
0.000124981311087457, 0.010701820155981, 0.00520458916051572,
0.002548740132205, 6.70653961877279e-06, 1.1372650836283e-06,
0.0028674817110041, 6.38196191847228, 0.00104230990042965, 2.77791027153022,
0.385285554179204, 3.23552539344696, 0.00129215960928528, 3313.17800288969,
0.42454812322342, 0.427501088945987, 0.0252775421363044, 1.3790172222154e-05,
0.000499925244349826, 0.575943821174679, 3.66456124110476e-05,
0.000979273863184647, 1.71186456807568e-06, 0.000506903940694852,
3.95489796579998e-05, 7.60789146241221e-07, 5.53083255055159e-07,
0.000283178626588241, 5.68632541814152e-07, 89.5114292952616,
2.15183665744117e-06, 9.48447928546097e-06, 1.10616651011032e-06,
6.83831307491562e-05, 0.000231612381626088, 0.361984543094889,
5.91197625260395e-05, 0.000979273863184647, 2.83936549218472e-06,
0.000979273863184647, 5.11112358098405e-05, 1.714153924998e-07,
5.19634300333657e-07, 0.000285939985649123, 0.000340041865397713,
0.11809338012465, 60.884369685235, 2.29364239206782e-05, 1.59952159960469e-05,
0.000213718586351138, 2.65657707341963e-06, 3635.65603745587,
1.08786283557826e-07, 3.36257994807117e-06, 0.482299092292068,
1.40214978558205e-05, 0.506277403675245, 0.00847835446782661,
5.84677257215999e-05, 0.00674484030136259, 0.00483589957358377,
0.0017456741452281, 6.45120458509457e-06, 6.32689066217975e-07,
0.00245170310797391, 9.30496033238278, 0.000922604532223834,
1.94261499108326, 0.348202870167258, 0.000995700862302919, 9.18683915124066e-06,
0.00490340621594781, 9.51081233425213e-06, 1.64449027258861e-05,
1.32828853670982e-06, 0.000283964853893518, 0.000480891817820092,
0.103521332666818, 96.202334596196, 1.57750051307367e-05, 2.09600255345096e-05,
0.000200793473806753, 1.29196641682183e-06, 179.519904082227,
2.39744324779145e-07, 2.44454941589392e-06, 0.492433221447773,
1.07746460295468e-05, 0.437695664847132, 0.00947275639891981,
9.69768554804815e-05, 0.0056325346541415, 0.00470366164543522,
0.00172164093341244, 6.91422987569681e-06, 8.82439067876674e-07,
0.00253816223135828, 5.84822979360013, 0.000929021754230271,
2.31017156910716, 0.278934830581241, 2.84415482117455, 0.00100262650949219,
2661.45599990874, 0.357992185300285, 0.37579036951639, 0.0210213626331535,
1.87597483406766e-05, 4.9165300967331e-05, 0.353063601096188,
2.84344613435294e-05, 0.00277749494255326, 1.32828853670982e-06,
0.00108958918195797, 9.25073867082013e-06, 1.4059026149049e-07,
4.29154362580066e-07, 0.000537294242854559, 8.10925044524043e-06,
0.020165038913309, 9.91469621624329e-06, 1.63313094852695e-05,
8.58308725160133e-07, 2.34183669433728e-05, 0.000352033415883844,
0.28087497575791, 4.58728478413563e-05, 0.0007598488052299, 1.48407969771465e-06,
0.0223745115812679, 1.15479796826903e-05, 1.33006491938229e-07,
4.03200286568411e-07, 83.9815202938853, 211.131788444181, 1.73147313103931,
0.162893393670412, 6347.61978641754, 1.56049096034741, 0.532923368033971,
0.651573574681646, 22.0392007421302, 0.05154584678813, 85997.0767809387,
2.10234581817541, 1994.76074197656, 17462.8329237372, 1.76785506212734,
49735.9012814537, 1.57134503333516, 340.615434516655, 3.73730938753272,
2.07340220203944, 0.974004268543241, 53.8920290309386, 28.8800232787977,
0.0604547706008708, 6.41744933081988, 1.9615580079771, 0.384751805040216,
1.53900722016086, 1.68412590721683, 2.31658561238929, 1.62675839626425,
2.23767420207142, 1.67249279982813, 1.53900722016086, 1.51781925297405,
0.717972255311719, 1.08072540203935, 1.6958399292663, 1.74351647907412,
1.6958399292663, 0.98077900398855, 0.000159075579756261, 1.32133840565826,
1.57134503333516, 1.79253339913881, 2.00277451142267, 1.74351647907412,
2.66105808216138, 0.90250072746243, 2.059080166868, 1.50733490955838,
1.3966785324674, 1.61552155521922, 1.42602571736414, 1.90791910109511,
1.38703096913138, 1.38703096913138, 1.49692298679269, 1.69583992926629,
2.16145080407871, 2.67956720485568, 1.3966785324674, 1.53900722016086,
1.70763542878249, 0.921464186198703, 3.32188009636358, 10.5707072452661,
6.5522935828786, 1.68412590721683, 7.57896056479413, 1.43594451062343,
0.312515575646302, 34.1070955541741, 2339.52511354582, 11.0962477530511,
8.17942824487938, 1.68412590721683, 0.418123199957032, 804.528657067602,
0.679243142274472, 1.47631440568283, 1.75564359521904, 2.81278639982623,
4.14680440407889, 1.68412590721683, 2.33269873957693, 1.68412590721683,
1.70763542878249, 1.37745004638314, 1.68412590721683), listElement = structure(c(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, 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,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Endoderm",
"Mesoderm", "Ectoderm"), class = "factor")), .Names = c("Classification",
"value", "listElement"), row.names = c(NA, -270L), class = "data.frame")
To generate the boxplot:
boxData$temp <- paste(substr(boxData$Classification,1,6),
as.character(boxData$listElement))
ggplot(boxData, aes(reorder(boxData$temp, value, median),value, fill=Classification))+
geom_boxplot()+
scale_y_log10()+
ylab("Fold Expression Change")+
xlab("Gene Classification")+
theme(axis.text.x=element_text(angle=90, hjust=1, size=6))+
facet_wrap(~listElement, scales='free', ncol=1)+
scale_x_discrete(labels=setNames(as.character(boxData$Classification), boxData$temp))
But if a parameter is changed and we only have two samples rather than three (In this case, the same data, but with twice as many 'endoderm' samples and no 'mesoderm' samples), the boxplots look really weird:
boxData <- structure(list(Classification = structure(c(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, 2L, 2L, 2L, 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, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 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, 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,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Pluripotent/ Undifferentiated",
"Endoderm", "Mesoderm", "Ectoderm"), class = c("ordered", "factor"
)), value = c(0.000255214868214152, 0.000108050996652777, 0.00751505823956855,
8.71801689770664, 5.71059263813113e-05, 4.90291746067526e-05,
0.000129388767504551, 2.52712436532327e-07, 5345.09546573398,
0.0020991194782334, 4.33360773005175e-06, 1.8200776481618, 3.44754305553851e-06,
4.38932775031697, 0.00720892572385782, 7.53517216121544e-05,
0.221288441144887, 0.00104230990042965, 0.00288742662358172,
4.20947546944294e-05, 9.62973878475845e-07, 0.00710967831313203,
26.9833955280036, 0.00265697432110539, 1.41814003567946, 0.261340025051291,
0.00159083508412152, 9.55044905589291e-06, 0.0122931632086495,
8.54789134364452e-06, 2.01899938950824e-05, 1.55354988683742e-06,
0.000441285511108929, 0.000353500530366103, 0.125347054487635,
109.440278770173, 2.03304264082645e-05, 2.01899938950824e-05,
0.000148628664387571, 2.89902659683517e-06, 207.073625180606,
3.52469070261441e-07, 3.15047327017105e-06, 0.639049681601525,
2.11937734339159e-05, 0.484309094613314, 0.0126387710681522,
0.000124981311087457, 0.010701820155981, 0.00520458916051572,
0.002548740132205, 6.70653961877279e-06, 1.1372650836283e-06,
0.0028674817110041, 6.38196191847228, 0.00104230990042965, 2.77791027153022,
0.385285554179204, 3.23552539344696, 0.00129215960928528, 3313.17800288969,
0.42454812322342, 0.427501088945987, 0.0252775421363044, 1.3790172222154e-05,
0.000499925244349826, 0.575943821174679, 3.66456124110476e-05,
0.000979273863184647, 1.71186456807568e-06, 0.000506903940694852,
3.95489796579998e-05, 7.60789146241221e-07, 5.53083255055159e-07,
0.000283178626588241, 5.68632541814152e-07, 89.5114292952616,
2.15183665744117e-06, 9.48447928546097e-06, 1.10616651011032e-06,
6.83831307491562e-05, 0.000231612381626088, 0.361984543094889,
5.91197625260395e-05, 0.000979273863184647, 2.83936549218472e-06,
0.000979273863184647, 5.11112358098405e-05, 1.714153924998e-07,
5.19634300333657e-07, 3.36257994807117e-06, 0.482299092292068,
1.40214978558205e-05, 0.00847835446782661, 5.84677257215999e-05,
0.00674484030136259, 0.00483589957358377, 0.0017456741452281,
6.45120458509457e-06, 6.32689066217975e-07, 0.00245170310797391,
9.30496033238278, 0.000922604532223834, 1.94261499108326, 0.348202870167258,
0.506277403675245, 0.000285939985649123, 0.000340041865397713,
0.11809338012465, 60.884369685235, 2.29364239206782e-05, 1.59952159960469e-05,
0.000213718586351138, 2.65657707341963e-06, 3635.65603745587,
1.08786283557826e-07, 83.9815202938853, 211.131788444181, 1.73147313103931,
0.162893393670412, 6347.61978641754, 1.56049096034741, 0.532923368033971,
0.651573574681646, 22.0392007421302, 0.05154584678813, 85997.0767809387,
2.10234581817541, 1994.76074197656, 17462.8329237372, 1.76785506212734,
49735.9012814537, 1.57134503333516, 340.615434516655, 3.73730938753272,
2.07340220203944, 0.974004268543241, 53.8920290309386, 28.8800232787977,
0.0604547706008708, 6.41744933081988, 1.9615580079771, 1.57750051307367e-05,
2.09600255345096e-05, 0.000200793473806753, 1.29196641682183e-06,
179.519904082227, 2.39744324779145e-07, 2.44454941589392e-06,
0.492433221447773, 1.07746460295468e-05, 0.437695664847132, 0.00947275639891981,
9.69768554804815e-05, 0.0056325346541415, 0.00470366164543522,
0.00172164093341244, 6.91422987569681e-06, 8.82439067876674e-07,
1.57134503333516, 1.79253339913881, 2.00277451142267, 1.74351647907412,
2.66105808216138, 0.90250072746243, 2.059080166868, 1.50733490955838,
1.3966785324674, 1.61552155521922, 0.384751805040216, 1.53900722016086,
1.68412590721683, 0.000995700862302919, 9.18683915124066e-06,
0.00490340621594781, 9.51081233425213e-06, 1.64449027258861e-05,
1.32828853670982e-06, 0.000283964853893518, 0.000480891817820092,
0.103521332666818, 96.202334596196, 1.6958399292663, 0.98077900398855,
0.000159075579756261, 2.31658561238929, 1.62675839626425, 2.23767420207142,
1.67249279982813, 1.53900722016086, 1.51781925297405, 0.717972255311719,
1.08072540203935, 1.6958399292663, 1.74351647907412, 1.32133840565826,
0.0210213626331535, 1.87597483406766e-05, 4.9165300967331e-05,
0.00253816223135828, 5.84822979360013, 0.000929021754230271,
2.31017156910716, 0.278934830581241, 2.84415482117455, 0.00100262650949219,
2661.45599990874, 0.357992185300285, 0.37579036951639, 1.42602571736414,
1.90791910109511, 1.38703096913138, 0.353063601096188, 2.84344613435294e-05,
0.00277749494255326, 1.32828853670982e-06, 0.00108958918195797,
9.25073867082013e-06, 1.4059026149049e-07, 4.29154362580066e-07,
0.000537294242854559, 8.10925044524043e-06, 0.020165038913309,
9.91469621624329e-06, 1.63313094852695e-05, 8.58308725160133e-07,
1.43594451062343, 0.312515575646302, 34.1070955541741, 2339.52511354582,
11.0962477530511, 8.17942824487938, 1.68412590721683, 0.418123199957032,
804.528657067602, 0.679243142274472, 10.5707072452661, 6.5522935828786,
1.68412590721683, 1.38703096913138, 1.49692298679269, 1.69583992926629,
2.16145080407871, 2.67956720485568, 1.3966785324674, 1.53900722016086,
1.70763542878249, 0.921464186198703, 3.32188009636358, 7.57896056479413,
2.34183669433728e-05, 0.000352033415883844, 0.28087497575791,
4.58728478413563e-05, 0.0007598488052299, 1.48407969771465e-06,
0.0223745115812679, 1.15479796826903e-05, 1.33006491938229e-07,
4.03200286568411e-07, 1.47631440568283, 1.75564359521904, 2.81278639982623,
4.14680440407889, 1.68412590721683, 2.33269873957693, 1.68412590721683,
1.70763542878249, 1.37745004638314, 1.68412590721683), listElement = 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,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= c(NA, -270L), class = "data.frame")
Running the same code as above:
boxData$temp <- paste(substr(boxData$Classification,1,6),
as.character(boxData$listElement))
ggplot(boxData, aes(reorder(boxData$temp, value, median),value, fill=Classification))+
geom_boxplot()+
scale_y_log10()+
ylab("Fold Expression Change")+
xlab("Gene Classification")+
theme(axis.text.x=element_text(angle=90, hjust=1, size=6))+
facet_wrap(~listElement, scales='free', ncol=1)+
scale_x_discrete(labels=setNames(as.character(boxData$Classification), boxData$temp))
gives a strange-looking graph:
This graph should look the same as the first graph, just with two facets rather than three. If I don't try to reorder the values by median, this graph plots fine. I've fiddled with a number of things, but can't seem to fix this issue. I'm sure I've made a stupid mistake somewhere, but can't seem to find it.
Any help would be greatly appreciated!
It looks to me that you are reordering the factor "temp" without releveling the dataset. What about bringing the ordering operation outside the ggplot call?
boxData$temp <- paste(substr(boxData$Classification,1,6),
as.character(boxData$listElement))
fac <- with(boxData, reorder(temp, value, median, order = TRUE))
boxData$temp <- factor(boxData$temp, levels = levels(fac))
ggplot(boxData, aes(temp,value, fill=Classification))+
geom_boxplot()+
scale_y_log10()+
ylab("Fold Expression Change")+
xlab("Gene Classification")+
theme(axis.text.x=element_text(angle=90, hjust=1, size=6))+
facet_wrap(~listElement, scales='free', ncol=1)+
scale_x_discrete(labels=setNames(as.character(boxData$Classification), boxData$temp))
This is what you would expect, right?

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

How to get the median line in bwplot thicker?

The following code is a minimal (for some value of minimal....) example that uses lattice to produce boxplots. But the median line on those boxplot is a) coloured and b) very thin. How to get them to be black and tick?
a71<-structure(list(n = structure(c(1L, 2L, 2L, 4L, 4L, 1L, 1L, 4L,
2L, 1L, 1L, 2L, 2L, 4L, 2L, 2L, 3L, 4L, 1L, 2L, 2L, 3L, 2L, 2L,
2L, 4L, 3L, 3L, 4L, 2L, 4L, 2L, 1L, 3L, 2L, 3L, 4L, 1L, 4L, 1L,
3L, 3L, 2L, 1L, 1L, 3L, 3L, 1L, 2L, 4L, 3L, 2L, 3L, 1L, 4L, 1L,
4L, 2L, 3L, 4L, 4L, 4L, 1L, 3L, 3L, 3L, 4L, 2L, 2L, 2L, 4L, 4L,
4L, 1L, 4L, 3L, 2L, 2L, 4L, 4L, 3L, 2L, 2L, 2L, 1L, 2L, 3L, 3L,
3L, 1L, 3L, 3L, 4L, 1L, 3L, 2L, 1L, 3L, 1L, 2L), .Label = c("100",
"200", "400", "800"), class = "factor"), g = structure(c(3L,
3L, 1L, 3L, 1L, 3L, 2L, 1L, 1L, 3L, 1L, 2L, 3L, 1L, 2L, 2L, 1L,
3L, 1L, 2L, 3L, 2L, 2L, 2L, 3L, 1L, 1L, 3L, 3L, 2L, 1L, 1L, 3L,
1L, 3L, 3L, 1L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 1L,
1L, 1L, 2L, 3L, 1L, 3L, 1L, 3L, 3L, 1L, 2L, 1L, 2L, 2L, 1L, 3L,
3L, 1L, 3L, 2L, 3L, 1L, 3L, 1L, 1L, 3L, 3L, 2L, 3L, 3L, 3L, 1L,
3L, 3L, 2L, 3L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 1L, 2L, 3L, 3L,
3L, 1L, 3L), .Label = c("0", "0.5", "1"), class = "factor"),
cr = structure(c(1L, 2L, 3L, 1L, 3L, 3L, 2L, 1L, 2L, 3L,
3L, 2L, 2L, 3L, 2L, 2L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 1L,
3L, 3L, 3L, 1L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 3L, 2L, 3L, 1L,
3L, 2L, 2L, 2L, 3L, 2L, 3L, 1L, 1L, 2L, 1L, 2L, 2L, 3L, 1L,
1L, 1L, 1L, 3L, 3L, 2L, 3L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
1L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 2L, 1L, 2L,
2L, 2L, 2L, 3L, 2L, 2L, 1L, 3L, 2L, 1L, 3L, 1L, 1L, 2L, 2L
), .Label = c("-0.4", "0", "0.4"), class = "factor"), bias = c(0.0162558992812201,
0.138354243932496, 0.0205686041691062, 0.269714433604472,
0.381044037439145, 0.0869422119950729, 0.331379037601084,
0.686894150152472, 0.0140922903231885, 0.225078933454863,
0.554444988164574, 0.076032683077827, 0.335284040888653,
0.0630810396519646, 0.358402154233125, 0.260940142571834,
0.141353291599136, 0.0220267076189838, 0.242149484071382,
0.278319984858078, 0.193105829691662, 0.0259815643559331,
0.318504899459259, 0.00277002060524357, 0.212681621053374,
0.418358846098857, 0.358916156777489, 0.438248724241505,
0.194398889511096, 0.2266870834128, 0.144338808446284, 0.149227951210927,
0.268111328952192, 0.123265441389974, 0.0376832357983068,
0.0353605481767078, 0.021227873083535, 0.0385614926552725,
0.130640111978654, 0.161865326447675, 0.174151298764213,
0.292085797406362, 0.198391364913347, 0.0779507859721407,
0.0045571464157577, 0.114734038438965, 0.0469613758623325,
0.64238405800387, 0.74508519247034, 0.0251182457091362, 0.217835062247358,
0.131159910126724, 0.130034859007596, 0.222418419987533,
0.0861715693619894, 0.185660520258661, 0.0940670543815277,
0.105680179626893, 0.215966730684923, 0.109008340760604,
0.0474735195202623, 0.192326789813641, 0.022147195644035,
0.277372858009381, 0.237574293593955, 0.123383946121193,
0.46406480500022, 0.123698482002945, 0.671442441453945, 0.0406004813894845,
0.260472754754191, 0.0151116521560003, 0.0422855023583402,
0.0405517218780402, 0.0441583998205882, 0.0958995639409343,
0.37588506579263, 0.098494760958735, 0.0928763466294421,
0.111205748449328, 0.413083543393392, 0.0138839674143682,
0.22407421093074, 0.72309883706409, 0.423231501875638, 0.141932050342199,
0.133808548118004, 0.331500621801688, 0.127652280721512,
0.132083126730013, 0.261864564503826, 0.208243130464985,
0.18657049493156, 0.333701537602998, 0.404884075502013, 0.470789398932934,
0.115008599462104, 0.177984001517338, 0.331717679106776,
0.0862418839846533), group = structure(c(3L, 2L, 2L, 3L,
1L, 3L, 3L, 2L, 2L, 1L, 3L, 2L, 2L, 2L, 1L, 3L, 2L, 3L, 1L,
3L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 3L, 1L, 1L,
3L, 2L, 3L, 1L, 3L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 3L, 1L, 2L,
3L, 3L, 3L, 1L, 1L, 3L, 2L, 3L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 1L, 3L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 2L, 1L, 3L,
3L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 1L, 1L, 2L, 3L,
1L, 1L, 3L, 2L, 1L, 3L), .Label = c("1", "2", "3"), class = "factor")), .Names = c("n",
"g", "cr", "bias", "group"), row.names = c(8721L, 6970L, 6686L,
9624L, 352L, 10545L, 7505L, 4216L, 6170L, 3309L, 10429L, 4302L,
5602L, 5680L, 1530L, 9234L, 5007L, 8004L, 721L, 10038L, 502L,
4891L, 2946L, 8502L, 622L, 1972L, 2403L, 3383L, 5880L, 1038L,
4756L, 9506L, 2169L, 1023L, 8506L, 6239L, 7768L, 3221L, 9536L,
5981L, 1507L, 4883L, 414L, 3117L, 3993L, 1923L, 9143L, 2673L,
4430L, 9520L, 9363L, 10602L, 95L, 1141L, 9660L, 4285L, 10704L,
154L, 531L, 6440L, 4876L, 7052L, 4397L, 3375L, 5075L, 1295L,
2620L, 334L, 9510L, 4690L, 4288L, 3576L, 2248L, 7693L, 8820L,
8135L, 4026L, 1906L, 10164L, 8616L, 423L, 5290L, 418L, 6486L,
4485L, 7042L, 955L, 2215L, 9031L, 8049L, 2323L, 1627L, 4212L,
8689L, 439L, 2590L, 8649L, 5447L, 1957L, 10570L), class = "data.frame")
library(lattice)
cl<-c('red','green','blue')
mypanel<-function(...){
panel.bwplot(...,pch="|",col="black",cex=4,fill=cl)
}
o1<-bwplot(a71$bias~a71$group|a71$cr*a71$g,type=c("l","g"),ylim=c(0,1),panel=mypanel)
plot(o1)
By changing some of the parameters of box.rectangle (a lattice-specific graphical parameter), you can manipulate the lines (including the median line) surrounding each of the box plots. This will change all the lines around the boxes, however, not just the median line.
myPars <- list(box.rectangle = list(lwd = 2, col = "black"))
lwd changes the line width (thickness). colchanges the color of the lines. Then pass this list to the par.settings argument in bwplot.
o1 <- bwplot(a71$bias ~ a71$group | a71$cr * a71$g,
type = c("l", "g"), ylim = c(0, 1), panel = mypanel,
par.settings = myPars)
plot(o1)
To see all of the parameters associated with box.rectangle, use
trellis.par.get("box.rectangle")
OP is happy with all lines thicker by #BenBarnes, but for completeness, it is possible to just thicken the median line. Using the fact that box.width defaults to 1/2:
bwplot(a71$bias~a71$group|a71$cr*a71$g,type=c("l","g"),ylim=c(0,1),
panel=function(x,y,...){
panel.bwplot(x,y,...,pch="|",col="black",cex=4,fill=cl)
medy <- by(y,list(x),median)
xx <- sort(unique(as.numeric(x)))
panel.segments(xx-.25,medy,xx+.25,medy,lwd=2)
}
)

Resources