Plot linear regression analysis with error bar for variability - r

I wanted to make plots that look like figure 1 (source: link)
In figure 1, they have plotted the regression analysis with one-year yield variability. In my case, I would like to plot variability between two locations and 4 blocks for each treatment group. So the plot I wanted would have three facets for factors B.glucosidase, Protein, POX.C of variable and four colors for treatments factors. Also, in my current plot I have legend for block and treatment. I should only have treatment because the block should be used for making error bar for variability.
I tried with this code, which obviously doesn't work for what I want. (Data for df.melted included below.)
ggplot(df.melted, aes(x = value, y = yield, color = as.factor(treatment))) +
geom_point(aes(shape= as.factor(block))) +
stat_smooth(method = "lm", formula = y ~ x, col = "darkslategrey", se=F) +
stat_poly_eq(formula = y~x,
# aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
aes(label = ..rr.label..),
parse = TRUE) +
theme_classic() +
geom_errorbar(aes(ymax = df.melted$yield+sd(df.melted$yield), ymin = df.melted$yield-sd(df.melted$yield)), width = 0.05)+
facet_wrap(~variable)
Data:
df.melted <- structure(list(Location = structure(c(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, 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, 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), .Label = c("M", "U"), class = "factor"),
treatment = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("CC",
"CCS", "CS", "SCS"), class = "factor"), block = c(1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L), yield = c(5156L, 5157L, 5551L, 5156L, 4804L,
4720L, 4757L, 5021L, 4826L, 4807L, 4475L, 4596L, 4669L, 4588L,
4542L, 4592L, 5583L, 5442L, 5693L, 5739L, 5045L, 4902L, 5006L,
5086L, 4639L, 4781L, 4934L, 4857L, 4537L, 4890L, 4842L, 4608L,
5156L, 5157L, 5551L, 5156L, 4804L, 4720L, 4757L, 5021L, 4826L,
4807L, 4475L, 4596L, 4669L, 4588L, 4542L, 4592L, 5583L, 5442L,
5693L, 5739L, 5045L, 4902L, 5006L, 5086L, 4639L, 4781L, 4934L,
4857L, 4537L, 4890L, 4842L, 4608L, 5156L, 5157L, 5551L, 5156L,
4804L, 4720L, 4757L, 5021L, 4826L, 4807L, 4475L, 4596L, 4669L,
4588L, 4542L, 4592L, 5583L, 5442L, 5693L, 5739L, 5045L, 4902L,
5006L, 5086L, 4639L, 4781L, 4934L, 4857L, 4537L, 4890L, 4842L,
4608L), 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, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 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), .Label = c("B.glucosidase",
"Protein", "POX.C"), class = "factor"), value = c(1.600946,
1.474084, 1.433078, 1.532492, 1.198667, 1.193193, 1.214941,
1.360981, 1.853056, 1.690117, 1.544357, 1.825132, 1.695409,
1.764123, 1.903743, 1.538684, 0.845077, 1.011463, 0.857032,
0.989803, 0.859022, 0.919467, 1.01717, 0.861689, 0.972332,
0.952922, 0.804431, 0.742634, 1.195837, 1.267285, 1.08571,
1.20097, 6212.631579, 5641.403509, 4392.280702, 7120.701754,
5305.964912, 4936.842105, 5383.157895, 6077.894737, 5769.122807,
5016.842105, 5060.350877, 5967.017544, 5576.842105, 5174.035088,
5655.438596, 5468.77193, 7933.333333, 7000, 6352.982456,
8153.684211, 6077.894737, 4939.649123, 5002.807018, 6489.122807,
4694.035088, 5901.052632, 4303.859649, 6768.421053, 6159.298246,
6090.526316, 4939.649123, 5262.45614, 810.3024, 835.5242,
856.206, 759.8589, 726.2298, 792.6472, 724.7165, 699.3266,
500.9153, 634.8698, 637.9536, 648.8814, 641.0357, 623.3822,
555.2834, 520.8119, 683.3528, 595.9173, 635.4315, 672.4234,
847.2944, 745.5665, 778.3548, 735.8141, 395.2647, 570.4148,
458.0383, 535.3851, 678.0293, 670.7419, 335.2923, 562.5674
)), row.names = c(NA, -96L), class = "data.frame")

library(dplyr)
library(ggplot2)
library(ggpmisc)
Summarize data frame (this could also be done with stat_summary(), but it's often clearer/more transparent to do it explicitly up front). (I think that because your data set is balanced you could collapse/average over the block structure first, and then do your whole plot with the reduced data set - it shouldn't change the outcome of the linear regressions at all, at least not the mean values ... and any statistical comparisons should probably done on block-level summaries anyway ...)
df.sum <- (df.melted
%>% group_by(Location,treatment,variable)
%>% summarise(value=mean(value),yield_sd=sd(yield),
## collapse yield to mean *after* computing sd!
yield=mean(yield))
)
Plot:
(ggplot(df.melted,
aes(x = value, y = yield, color = treatment))
+ stat_smooth(method = "lm", col = "darkslategrey", se=FALSE)
+ stat_poly_eq(
formula = y ~ x,
## aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
aes(group=1, label = ..rr.label..),
parse = TRUE)
+ theme_classic()
+ scale_shape(guide=FALSE)
+ geom_point(data=df.sum)
+ geom_errorbar(data=df.sum,
aes(ymax = yield+yield_sd, ymin = yield-yield_sd),
width = 0.05)
+ facet_wrap(~variable,scale="free_x")
)
(adding group=1 to the stat_poly_eq() aesthetics means we only plot a single R^2 value per facet)
Since you're no longer using the shape aesthetic for anything, you could consider using it to show the Location variable ...

Related

R - boxplot log scale problem with 0 values

I have a dataset that consists of 0 values. I want to use log scale but because of the 0 values, it is returning an error. I tried to replace 0s with 1s and it returned something that did not seem right.
As you can see in the figure, I have very small values for the 16k case but to show it clearly, I want to use log scale. Also, I want the order to be 8k_B, 8k_S, 16k_B, 16k_S. I tried factor and levels but still it didn't change the order.
Can someone please help? I can post the data if necessary. Thank you.
Here is the code I used.
data_freq <- data.frame(name=c( rep("8K_B",24), rep("8K_S",24), rep("16_B",24), rep("16K_S",24)),sines=c(rep("B",24),rep("S",24),rep("B",24),rep("S",24)),
value_freq=c( r1B$Frequency, r1S$Frequency, r2B$Frequency, r2S$Frequency)
)
p <- ggplot(data_freq, aes(x=name, y=value_freq, fill=name)) +
geom_boxplot()
Here is the data:
data_freq <- structure(list(name = 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, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("16K_S", "16_B",
"8K_B", "8K_S"), class = "factor"), sines = 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, 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, 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("B",
"S"), class = "factor"), value_freq = c(6.269822e-05, 5.494403e-05,
5.84888e-05, 5.727028e-05, 7.300023e-05, 6.502448e-05, 6.568913e-05,
5.771338e-05, 5.638409e-05, 5.693796e-05, 5.527635e-05, 6.103661e-05,
5.660564e-05, 6.269822e-05, 5.594099e-05, 6.978778e-05, 5.571945e-05,
6.258745e-05, 6.779384e-05, 6.668609e-05, 6.048274e-05, 5.826725e-05,
5.671641e-05, 6.070429e-05, 9.433902e-05, 8.037108e-05, 8.203393e-05,
8.591391e-05, 9.633444e-05, 9.123503e-05, 8.946133e-05, 8.447278e-05,
7.638024e-05, 8.103622e-05, 8.15905e-05, 8.480535e-05, 7.527167e-05,
8.779847e-05, 8.192307e-05, 9.7443e-05, 7.649109e-05, 8.425106e-05,
9.134589e-05, 9.555844e-05, 8.724419e-05, 7.881908e-05, 7.771052e-05,
8.358592e-05, 1.1077e-07, 1.1077e-07, 0, 0, 1.1077e-07, 0, 0,
1.1077e-07, 1.1077e-07, 0, 0, 0, 0, 0, 3.3232e-07, 0, 2.2155e-07,
4.431e-07, 1.1077e-07, 1.1077e-07, 1.1077e-07, 0, 2.2155e-07,
0, 5.5428e-07, 5.5428e-07, 6.6514e-07, 6.6514e-07, 7.64911e-06,
6.6514e-07, 6.6514e-07, 1.1086e-07, 5.5428e-07, 6.6514e-07, 6.6514e-07,
6.6514e-07, 3.3257e-07, 6.6514e-07, 0, 6.6514e-07, 3.87998e-06,
6.6514e-06, 1.1086e-07, 1.1086e-07, 1.1086e-07, 3.3257e-07, 3.3257e-07,
1.10857e-06)), class = "data.frame", row.names = c(NA, -96L))
You could try to do log(x+n) transformation instead.
p <- data_freq %>%
mutate(value_freq = log(value_freq + 0.000001)) %>% # your numbers are really small so I am adding a small number
ggplot(aes(x=name, y=value_freq, fill=name)) +
geom_boxplot()
Alternatively, you can try square root transformation.
p <- data_freq %>%
mutate(value_freq = value_freq^(1/2)) %>%
ggplot(aes(x=name, y=value_freq, fill=name)) +
geom_boxplot()
Or do the transformation using ggplot:
p <- data_freq %>%
ggplot(aes(x=name, y=value_freq, fill=name)) +
geom_boxplot() +
scale_y_log10()

Geom Bar Plot, sum of count not visible

I have a dataframe tag, with 51X5 structure
structure(list(Tagging = 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, 2L, 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("CIRCLE CAMPIAGN",
"NATIONAL CAMPIAGN"), class = "factor"), Status = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("Negative", "Positive"), class = "factor"),
Month = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L), .Label = c("JUL",
"JUN", "MAY"), class = "factor"), Category = structure(c(1L,
4L, 6L, 1L, 2L, 4L, 6L, 1L, 2L, 4L, 5L, 6L, 1L, 2L, 4L, 5L,
6L, 1L, 2L, 4L, 5L, 6L, 1L, 2L, 4L, 6L, 1L, 4L, 6L, 2L, 3L,
4L, 6L, 1L, 2L, 3L, 4L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L,
3L, 4L, 5L, 6L, 6L), .Label = c("Data", "Other", "Roaming",
"Unlimited", "VAS", "Voice"), class = "factor"), count = c(3L,
2L, 1L, 4L, 5L, 2L, 1L, 2L, 6L, 7L, 2L, 3L, 4L, 9L, 6L, 2L,
3L, 3L, 3L, 10L, 2L, 5L, 5L, 5L, 4L, 3L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 4L, 1L, 1L, 3L, 3L, 2L,
1L, 1L, 1L, 3L, 4L, 2L)), class = "data.frame", row.names = c(NA,
-51L))
I want to create a bar plot (ggplot) to show bar graph with label on bar as sum of count of category month wise I am using below code
ggplot(data = tag, aes(x = Tagging, y = count, fill = Status)) +
geom_col() +
labs(x = "Tagging", y = "Count", title = "FlyTxt ROI", subtitle = "Statistics") +
geom_text(aes(label = count), color = "white", size = 3, position = position_stack(vjust = 0.5)) +
theme_minimal()+facet_wrap(~Month)
But I am getting split count values:
Help as I want only sum of count for each status
The problem is, that the information you show in the bar is accumulated by geom_col over all categories but the geom_text doesn't do that.
On option is to pre-summarize the data (to get rid of the category split) and then plot the graph.
library(tidyverse)
tag_sum <- tag %>%
group_by(Tagging, Status, Month) %>%
summarise(count_sm = sum(count))
ggplot(data = tag_sum, aes(x = Tagging, y = count_sm, fill = Status)) +
geom_col() +
geom_text(aes(label = count_sm), color = "white", size = 3,
position = position_stack(vjust = 0.5)) +
facet_wrap(~Month) +
labs(x = "Tagging", y = "Count", title = "FlyTxt ROI", subtitle = "Statistics") +
theme_minimal()

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, 4L,
4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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751505823956855,
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, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Endoderm",
"Ectoderm"), class = "factor")), .Names = c("Classification",
"value", "listElement"), row.names = 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?

How to add significance letters (anova) to ggplot geom_bar with facet wrap

When I make a barplot with significance letters from anova above the bars, I use following code:
anova_NDW_geel<-aov(nodule_dry_weight~treatment,inoculatieproef_geel_variety2)
HSD_NDW_geel <- HSD.test(anova_NDW_geel,"treatment",alpha=0.05,group=TRUE)$groups
HSD_NDW_means_geel <- HSD.test(anova_NDW_geel,"treatment",alpha=0.05,group=TRUE)$means
HSD_NDW_means_geel <- HSD_NDW_means_geel[order(-HSD_NDW_means_geel$nodule_dry_weight),]
p_HSD_NDW_geel <- ggplot(aes(x=treatment, y=NDW_mean_geel, width=0.6), data=inoculatieproef_mean_geel)+
geom_bar(stat="identity", data=HSD_NDW_geel, aes(x=trt, y=means), fill="gray40")+
geom_text(data=HSD_NDW_geel, aes(x=trt, y=means, label=M), size=5, vjust=-1, hjust=1)+
ggtitle("Zand")+
ylab("Droog gewicht wortelknolletjes (g)")+
xlab("Behandeling")+
geom_errorbar(aes(ymin=NDW_mean_geel-NDW_sd_geel,ymax=NDW_mean_geel+NDW_sd_geel),
position=position_dodge(width=0.5),width=0.1,size=0.3)+
theme_bw() +
theme(axis.line = element_line(colour="black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())+
scale_y_continuous(expand = c(0, 0))+
theme(axis.text.x = element_text(angle = 0, hjust = 1, vjust = 0.5))+
theme(text = element_text(size=12))
which results in following graph: http://i.stack.imgur.com/bZidZ.png
This is probably not the best way to do this and when I want to add the letters to the barplots with facet wrap.
Here is a sample of the data I want to make a facet wrap with significance letters with:
structure(list(treatment = structure(c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L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, 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 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("2014",
"2015"), class = "factor"), nodule_dry_weight = c(0, 0.0467,
0.0328, 0.0885, 0.0081, 0.1086, 0.0788, 0.0267, 0, 0.0128, 0.0143,
0.0333, 0.006, 0.098, 0.0286, 0.011, 0, 0.0627, 0.0769, 0.0784,
0.023, 0.1504, 0.1026, 0.0254, 0, 0.0597, 0.0158, 0.0354, 0.0226,
0.3261, 0.0436, 0, 0, 0.0203, 0.0469, 0.0904, 0.1593, 0.0836,
0.056, 0.0037, 0, 0.0534, 0.0901, 0.0435, 0.0248, 0.0435, 0.0279,
0.0029, 0, 0.0545, 0.038, 0.0991, 0.0099, 0.1453, 0.1096, 0.0272,
0, 0.0319, 0.0624, 0.0508, 0.0415, 0.11, 0.0079, 0, 0, 0.1257,
0.1242, 0.2899, 0.024, 0.2175, 0.2979, 0.0396, 0, 0.1583, 0.2935,
0.2541, 0.1027, 0.4196, 0.2059, 0.0396, 0, 0.0891, 0.167, 0.0907,
0.2153, 0.3063, 0.2921, 0.0528, 0, 0.0928, 0.2109, 0.1514, 0.0821,
0.3607, 0.0996, 0.0069, 0, 0.0685, 0.3109, 0.1862, 0.0393, 0.286,
0.3418, 0.0459, 0, 0.0765, 0.3486, 0.3988, 0.1155, 0.6341, 0.3653,
0.039, 0, 0.0766, 0.3112, 0.1988, 0.05, 0.2856, 0.34, 0.0862,
0, 0.2621, 0.1146, 0.393, 0.1644, 0.3415, 0.1343, 0.019, 0, 0.0976,
0.1853, 0.0691, 0.0248, 0.1764, 0.1244, 0.1525, 0, 0.1529, 0.1069,
0.2833, 0.0204, 0.2966, 0.2371, 0.1464, 0, 0.0691, 0.2094, 0.1633,
0.0264, 0.1344, 0.0694, 0.1175, 0, 0.1783, 0.1434, 0.2136, 0.0873,
0.19, 0.1683, 0.1927, 0, 0.0571, 0.0599, 0.1061, 0.0244, 0.1256,
0.0894, 0.0123, 0, 0.1696, 0.1046, 0.2164, 0.0939, 0.1552, 0.2942,
0.1652, 0, 0.0844, 0.102, 0.0227, 0.025, 0.0654, 0.1234, 0.0702,
0, 0.0979, 0.1246, 0.0958, 0.0867, 0.1104, 0.1969, 0.227, 0,
0.3704, 0.4727, 0.2527, 0.2078, 0.3377, 0.308, 0.1293, 0, 0.2417,
0.3744, 0.2916, 0.1773, 0.433, 0.2446, 0.1382, 0, 0.4718, 0.4271,
0.4882, 0.1799, 0.4178, 0.518, 0.3915, 0, 0.3421, 0.3804, 0.2112,
0.4292, 0.3829, 0.1315, 0.2719, 0, 0.3197, 0.6867, 0.414, 0.3112,
0.2914, 0.4994, 0.369, 0.0256, 0.1494, 0.5577, 0.2538, 0.3854,
0.4151, 0.544, 0.4009, 0, 0.5208, 0.2962, 0.4175, 0.2689, 0.3374,
0.5075, 0.3601, 0, 0.704, 0.4631, 0.4573, 0.154, 0.5087, 0.4319,
0.4155)), .Names = c("treatment", "block", "position", "variety",
"location", "year", "nodule_dry_weight"), row.names = c(NA, -256L
), class = "data.frame")
I use following code for my graph with facet wrap:
inoculatieproef <- inoculatieproef %>%
group_by(treatment, location, variety, year) %>%
mutate(NDW_mean = mean(nodule_dry_weight),
NDW_sd = sd(nodule_dry_weight))
ggplot(data=inoculatieproef,aes(x=treatment, y=NDW_mean))+
facet_wrap(~location*variety*year,ncol=2)+
geom_bar(position="dodge", stat="identity")+
geom_errorbar(aes(ymin = NDW_mean - NDW_sd,
ymax = NDW_mean + NDW_sd),
width=0.1,size=0.3,
color = "darkgrey")+
theme_bw() +
theme(axis.line = element_line(colour="black"),
panel.grid.minor = element_blank(),
panel.background = element_blank())
How do I add on each barplot the significance letters (anova) in de the facet wrap graph?
No idea if the test fits your data distribution, but you can start with that:
library(tidyverse)
stat_pvalue <- dd %>%
group_by(location, variety, year) %>%
rstatix::t_test(nodule_dry_weight~treatment) %>%
filter(p < 0.05) %>%
group_by(location, variety, year) %>%
rstatix::add_significance("p") %>%
rstatix::add_y_position() %>%
mutate(y.position = seq(min(y.position), max(y.position),length.out = n())*1.1) %>%
ungroup()
ggplot(data=dd,aes(x=treatment, y=nodule_dry_weight))+
geom_boxplot() +
facet_wrap(~location + variety + year,ncol=2, scales = "free_y") +
ggpubr::stat_pvalue_manual(stat_pvalue, label = "p")

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