ggplot2 Adding data labels to grouped histograms chart - r

I have a table called "year" looking like this:
# Year Stars n pct
1 2015 1 778 26.5
2 2015 2 247 8.4
3 2015 3 286 9.7
4 2015 4 439 15
5 2015 5 1186 40.4
6 2016 1 678 22.7
7 2016 2 233 7.8
8 2016 3 256 8.6
9 2016 4 451 15.1
10 2016 5 1372 45.9
11 2017 1 501 24.3
12 2017 2 180 8.7
13 2017 3 215 10.4
14 2017 4 274 13.3
15 2017 5 894 43.3
16 2018 1 391 25.1
17 2018 2 125 8
18 2018 3 144 9.3
19 2018 4 196 12.6
20 2018 5 699 45
21 2019 1 474 22.4
22 2019 2 124 5.9
23 2019 3 168 8
24 2019 4 277 13.1
25 2019 5 1070 50.6
26 2020 1 148 25.3
27 2020 2 50 8.5
28 2020 3 64 10.9
29 2020 4 77 13.1
30 2020 5 247 42.2
Data represents have users have rated app from google play store through years. They rate it by giving 1 (bad) to 5 (great) stars.
I'm trying to make a chart which shows share of stars by star level and year using this code:
ggplot(year, aes(as.factor(Stars), pct)) +
geom_bar(aes(fill = as.factor(Year)), position = "dodge", stat="identity") +
scale_fill_manual(values=c("#05668D", "#028090", "#00A896", "#02C39A", "#4ecdc4", "#F0F3BD")) +
ylab("Share of stars (in %)") +
xlab("Stars") +
geom_text(label=round(year$pct, digits = 1),
position=position_dodge(0.9),
size = 4, fontface = "bold") +
ylim(0,50) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"),
plot.title = element_text(hjust = 0.5)) +
ggtitle("Share of stars")
Unfortunately, I get two problems:
1) For some reason I get the following warning message:
Warning messages:
1: Removed 1 rows containing missing values (geom_bar).
2: Removed 1 rows containing missing values (geom_text).
Meaning that data for 2020, 4 stars is missing though it should 13.1
2) My labels are all over the place while I would like them to be positioned above the corresponding column on the chart.
Could someone please help me with this problems?

You need to set the grouping correctly for the dodging to work. Instead of using ylim, which cuts off one of your bars, we can turn off axis expansion which looks better for bars going down to 0. (You may need to use ylim with a higher value to make sure all labels are printed.)
ggplot(year, aes(as.factor(Stars), pct)) +
geom_col(aes(fill = as.factor(Year)), position = "dodge") +
geom_text(
aes(label = round(pct, digits = 1), group = interaction(Stars, Year)),
position = position_dodge(0.9), size = 3, fontface = "bold", vjust = 0
) +
scale_fill_manual(values=c("#05668D", "#028090", "#00A896", "#02C39A", "#4ecdc4", "#F0F3BD")) +
scale_y_continuous(expand = c(0, 0)) +
theme(
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"),
plot.title = element_text(hjust = 0.5)
) +
labs(title = "Share of stars", x = "Share of stars (in %)", y = "Stars", fill = "Year")

Problem #1: You're getting that message because row #25 has a pct value of 50.6, and you're setting the maximum limit on the y-axis to 50.
Problem #2: Using position_dodge2 in your geom_text will make the labels line up, and I think adding a little space above the bar makes it more readable so I also added vjust = -1
ggplot(year, aes(as.factor(Stars), pct)) +
geom_bar(aes(fill = as.factor(Year)), position = "dodge", stat="identity") +
scale_fill_manual(values=c("#05668D", "#028090", "#00A896", "#02C39A", "#4ecdc4", "#F0F3BD")) +
ylab("Share of stars (in %)") +
xlab("Stars") +
geom_text(label=round(year$pct, digits = 1),
position=position_dodge2(width = .9),
vjust = -1,
fontface = "bold",
size = 4) +
ylim(0,50) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"),
plot.title = element_text(hjust = 0.5)) +
ggtitle("Share of stars")

Simply use position_dodge2 to fix the labels and get rid of the ylim to show all data:
library(ggplot2)
ggplot(year, aes(as.factor(Stars), pct)) +
geom_bar(aes(fill = as.factor(Year)), position = "dodge", stat="identity") +
scale_fill_manual(values=c("#05668D", "#028090", "#00A896", "#02C39A", "#4ecdc4", "#F0F3BD")) +
ylab("Share of stars (in %)") +
xlab("Stars") +
geom_text(aes(label=round(pct, digits = 1)),
position=position_dodge2(0.9),
size = 4, fontface = "bold") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"),
plot.title = element_text(hjust = 0.5)) +
ggtitle("Share of stars")

Related

Addition of exponential regression models to scatterplot

I have data set consisting of 7 species wet weights and size measurements. Number of observations range between 3 and 18 per species.
Species ID Wet weight(g) Size(mm)
These measurements are visualised in a ggplot scatter plot. I used the following code.
library("ggplot2")
library("reshape2")
library("tidyverse")
p<-ggplot(Wet,aes(x=Size,y=WW,colour=Species))+
geom_point(size=3)+
labs(x="\nDiameter or Length (mm)",y="Wet weight (g)\n")+
theme(axis.title.x=element_text(size=18),
axis.text.x=element_text(size=14,colour="black"),
axis.title.y=element_text(size=18),
axis.text.y=element_text(size=14,colour="black"),
axis.ticks=element_blank(),
legend.position="right",
legend.text=element_text(colour="black",size=14),
legend.title=element_blank())
p
I would like to add exponential regression models for every species and am interested in their equations, correlations coefficients and p-values.
I would appreciate any suggestions on how to add these to my scatter plot.
Thanks so much!!!
Species ID Wet Size (mm) weight(g)
Aequorea 1 195 390
Aequorea 2 225 579
Aequorea 3 224 303
Aurelia 4 235 647
Aurelia 5 170 335
Aurelia 6 155 269
Cyanea 7 370 1499
Cyanea 8 460 5000
Cyanea 9 430 2011
...
Your example data frame contains only three data points for each species, which is a very small sample size to fit an exponential regression. As a result, I decided to add more data points to your example as follows.
Wet <- read.table(text = "Species Size WW
Aequorea 195 390
Aequorea 225 579
Aequorea 224 303
Aequorea 280 1600
Aequorea 320 4000
Aurelia 235 647
Aurelia 170 335
Aurelia 155 269
Aurelia 300 2000
Aurelia 350 4500
Cyanea 370 1499
Cyanea 460 5000
Cyanea 430 2011
Cyanea 100 500
Cyanea 120 550
Cyanea 200 1000",
header = TRUE, stringsAsFactors = FALSE)
We can use geom_smooth to fit non-linear regression to each species as follows. The geom_smooth code is only for plotting. You will need to use the nls function to find out the coefficients for each species.
library(ggplot2)
p <- ggplot(Wet, aes(x = Size, y = WW, colour = Species))+
geom_point(size=3)+
labs(x = "\nDiameter or Length (mm)", y="Wet weight (g)\n")+
geom_smooth(method = "nls",
formula = y ~ a + x^b,
method.args = list(start = c(a = 1, b = 1)),
se = FALSE) +
theme(axis.title.x = element_text(size = 18),
axis.text.x = element_text(size = 14, colour = "black"),
axis.title.y = element_text(size = 18),
axis.text.y = element_text(size = 14, colour = "black"),
axis.ticks = element_blank(),
legend.position = "right",
legend.text = element_text(colour = "black", size = 14),
legend.title = element_blank())
p

ggplot2: geom_bar(); how to alternate order of fill so bars are not lost inside a bar with a higher value?

I am trying to position two bars at the same position on the x-axis and seperated out by colour (almost as if stacking).
However, instead of stacking I want the bar simply inside the other bar - with the smallest Y-value being visable inside the bar with the highest Y-value.
I can get this to work to some extent - but the issue is that one Y-value is not consistently higher across one of the two factors. This leads to bars being 'lost' within a bar with a higher Y-value.
Here is a subset of my dataset and the current ggplot code:
condition hours expression freq_genes
1 tofde 9 up 27
2 tofde 12 up 92
3 tofde 15 up 628
17 tofde 9 down 0
18 tofde 12 down 1
19 tofde 15 down 0
33 tofp 9 up 2462
34 tofp 12 up 786
35 tofp 15 up 298
49 tofp 9 down 651
50 tofp 12 down 982
51 tofp 15 down 1034
65 tos 0 up 27
66 tos 3 up 123
67 tos 6 up 752
81 tos 0 down 1
82 tos 3 down 98
83 tos 6 down 594
sf_plot <- ggplot(data = gene_freq,
aes(x = hours,
y = freq_genes,
group = condition,
fill = factor(expression,
labels=c("Down",
"Up"))))
sf_plot <- sf_plot + labs(fill="Expression")
sf_plot <- sf_plot + geom_bar(stat = "identity",
width = 2.5,
position = "dodge")
sf_plot <- sf_plot + scale_fill_manual(values=c("#9ecae1",
"#3182bd"))
sf_plot <- sf_plot + xlab("Time (Hours)")
sf_plot <- sf_plot + scale_x_continuous(breaks =
seq(min(gene_freq$freq_genes),
max(gene_freq$freq_genes),
by = 3))
sf_plot <- sf_plot + ylab("Gene Frequency")
sf_plot <- sf_plot + facet_grid(. ~ condition, scales = "free")
sf_plot <- sf_plot + theme_bw()
sf_plot <- sf_plot + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
sf_plot <- sf_plot + theme(axis.text.x = element_text(angle = 90))
# Print plot
sf_plot
You can add alpha = 0.5 to your geom_bar() statement to make the bars transparent. This will allow both bars to be seen. Adding that alpha statement and nothing else will produce what you're looking for, to make both overlaid bars visible. The colors, however, make seeing the two different bars challenging.
Another (and maybe better) option is to change the order in which the plot is created. If I recall correctly, ggplot will plot the bars in alphabetical or numeric or factor-level order. Here, your expression values are c("Down", "Up") and "Down" is being plotted first. If you force "Up" to be plotted first, you could resolve this, too.
library(dplyr)
library(ggplot2)
dat <-
read.table(text = "condition hours expression freq_genes
1 tofde 9 up 27
2 tofde 12 up 92
3 tofde 15 up 628
17 tofde 9 down 0
18 tofde 12 down 1
19 tofde 15 down 0
33 tofp 9 up 2462
34 tofp 12 up 786
35 tofp 15 up 298
49 tofp 9 down 651
50 tofp 12 down 982
51 tofp 15 down 1034
65 tos 0 up 27
66 tos 3 up 123
67 tos 6 up 752
81 tos 0 down 1
82 tos 3 down 98
83 tos 6 down 594") %>%
mutate(expression2 = ifelse(expression == "up", 1, 2))
dat %>%
ggplot(aes(x = hours, y = freq_genes, group = condition,
fill = factor(expression2, labels=c("Up", "Down")))) +
labs(fill="Expression") +
geom_bar(stat = "identity", position = "dodge", width = 2.5, alpha = 0.5) +
scale_fill_manual(values=c("#9ecae1", "#3182bd")) +
xlab("Time (Hours)") +
scale_x_continuous(breaks = seq(min(dat$freq_genes),
max(dat$freq_genes),
by = 3)) +
ylab("Gene Frequency") +
facet_grid(. ~ condition, scales = "free") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "bottom",
axis.text.x = element_text(angle = 90))
Here, I've created a new column called expression2 that is just a numeric version of expression. I changed the fill variable in aes() to match with those new labels. I left the colors in scale_fill_manual() the same as in your original statement and kept the alpha value. "Down" is being plotted on top of "Up" but in keeping the same colors with the alpha value, both bars are easier to see. You can play with the legend to display "Down" before "Up" if that's necessary.
Note that providing machine readable data goes a long way in allowing others to help you out. Consider using dput() to output your data next time rather than pasting it in. Also note that you can "chain" together ggplot() statements with a +. This makes code much more compact and easier to read.

Grouped boxplot combined with dotplot in ggplot2

I am new to stackoverflow so please be forgiving, if the way of asking my question can be improved (in that case I'm happy if you let me know how).
I am using the following code in ggplot2 to produce a grouped boxplot combined with a dotplot (unfortunately I cannot post an image yet (no reputation)):
ggplot(data24, aes(x=intensity, y=percacc, fill=group)) +
geom_boxplot(position=position_dodge(1), notch=T,
outlier.colour = NA, width = .7, alpha=0.2) +
geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 3,
position=position_dodge(1), dotsize=0.5, alpha=1) +
stat_summary(fun.y=mean, geom="point", shape=23, size=5,
position=position_dodge(0.3)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1,
position=position_dodge(0.3), size=1.2)+
scale_y_continuous(limits=c(0,100)) +
scale_fill_discrete(name="Group")
My questions are:
How can use different colors for different elements? I tried to add color/fill commands within geom_boxplot() and geom_dotplot, but this doesn't work: e.g., if I add fill="green" to geom_dotplot(), all points become green and centered between the boxplots. How can I rewrite the code to get
white fill for all boxplots
blue fill and black line for dots of group 1
green fill and black line for dots of group 2
black fill for all mean diamonds
How can pull the categories of "intensity" (i.e. the three pairs of boxplots) further apart from each other?
How can I display the dotplots next to the boxplots and the mean+CI in the boxplot?
Why does my coordination system still go from <0 to >100, even if I defined the y-axis to go from 0 to 100?
Thank you!
Edit 180722
Thank you for your comments. The data head is:
id intensity AQ_sum group acc percacc
1 54 40 11 COMP 5 20.83333
2 54 60 11 COMP 18 75.00000
3 54 80 11 COMP 24 100.00000
4 55 40 12 COMP 9 37.50000
5 55 60 12 COMP 22 91.66667
6 55 80 12 COMP 24 100.00000
7 58 40 10 COMP 8 33.33333
8 58 60 10 COMP 22 91.66667
9 58 80 10 COMP 23 95.83333
10 59 40 6 COMP 19 79.16667
11 59 60 6 COMP 24 100.00000
12 59 80 6 COMP 24 100.00000
13 60 40 9 COMP 10 41.66667
14 60 60 9 COMP 23 95.83333
15 60 80 9 COMP 22 91.66667
16 61 40 13 COMP 4 16.66667
17 61 60 13 COMP 19 79.16667
18 61 80 13 COMP 24 100.00000
19 62 40 12 COMP 16 66.66667
20 62 60 12 COMP 23 95.83333
My updated code is
ggplot(data24, aes(x=intensity, y=percacc, fill=group)) +
geom_boxplot(position=position_dodge(0.8), notch=T,
outlier.colour = NA, width = .4, alpha=0.3) +
geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 3,
position=position_dodge(0.8), dotsize=0.4, alpha=1)+
scale_fill_manual(values=c("#999999", "#E69F00"), name="Group") +
stat_summary(fun.y=mean, geom="point", shape=23, size=3,
position=position_dodge(0.2)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1,
position=position_dodge(0.2), size=0.5)+
scale_y_continuous(limits=c(0,103), expand = c(0, 0),
breaks=c(0,20,40,60,80,100), name="Percentage accuracy") +
scale_x_discrete(expand = c(0, 0.6), name="Degree of intensity (in percent)") +
labs(title="Accuracy by intensity and group\n") +
theme_light()+
theme(plot.title = element_text(face='bold', size=12, hjust = 0.5),
axis.title.x = element_text(size=10,hjust=0.5),
axis.title.y = element_text(size=10,vjust=1),
axis.text.x = element_text(size=10,color='black'),
axis.text.y = element_text(size=10, color='black'),
panel.grid.major.y = element_line(size = 0.3, linetype = "dotted", color="darkgrey"),
panel.grid.minor.y = element_line(size = 0.3, linetype = "dotted", color="darkgrey"),
panel.grid.major.x = element_blank(),
panel.border = element_blank(),
axis.line = element_line(size = 0.5, linetype = "solid", colour = "black")) +
ggsave("plotintensity.png", width = 10, height = 5)

Add a percent to y axis labels [duplicate]

This question already has answers here:
How can I change the Y-axis figures into percentages in a barplot?
(4 answers)
Closed 4 years ago.
I'm sure I missed an obvious solution tot his problem but I can't figure out how to add a percent sign to the y axis labels.
Data Sample:
Provider Month Total_Count Total_Visits Procedures RX State
Roberts 2 19 19 0 0 IL
Allen 2 85 81 4 4 IL
Dawson 2 34 34 0 0 CA
Engle 2 104 100 4 4 CA
Goldbloom 2 7 6 1 1 NM
Nathan 2 221 192 29 20 NM
Castro 2 6 6 0 0 AK
Sherwin 2 24 24 0 0 AK
Brown 2 282 270 12 12 UT
Jackson 2 114 96 18 16 UT
Corwin 2 22 22 0 0 CO
Dorris 2 124 102 22 22 CO
Ferris 2 427 318 109 108 OH
Jeffries 2 319 237 82 67 OH
The following code gives graphs with inaccurate values because R seems to be multiplying by 100.
procs <- read.csv(paste0(dirdata, "Procedure percents Feb.csv"))
procs$Percentage <- round(procs$Procedures/procs$Total.Visits*100, 2)
procs$Percentage[is.na(procs$Percentage)] <- 0
procsplit <- split(procs, procs$State)
plots <- function(procs) {
ggplot(data = procs, aes(x= Provider, y= Percentage, fill= Percentage)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(x = Provider, y = Percentage, label = sprintf("%.1f%%", Percentage)), position = position_dodge(width = 0.9), hjust = .5, vjust = 0 , angle = 0) +
theme(axis.text.x = element_text(angle = 45, vjust = .5)) +
ggtitle("Procedure Percentages- February 2018", procs$State) +
theme(plot.title = element_text(size = 22, hjust = .5, family = "serif")) +
theme(plot.subtitle = element_text(size = 18, hjust = .5, family = "serif")) +
scale_y_continuous(name = "Percentage", labels = percent)
}
lapply(procsplit, plots)
I'm not sure if there's a way to use sprintf to add it or if there's a way to paste it onto the labels.
adding + scale_y_continuous(labels = function(x) paste0(x, "%")) to the ggplot statement fixes this issue

How to add mean separation letters above bars in a vertical facet?

I'm trying to make a figure containing three bar plots vertically. I can do it successfully using Facets in ggplot2 but was wondering how I can add the mean separation letters above each bar for the respective plot. Data is separated by harvest in the facet (1-3). Please find sample data below, followed by code for the facet, and then final code I created for just one of the harvest in which I was successful in adding separation letters. Thank you:
Rootstock Harvest N Length.cm. sd se ci Lower Upper
1 A 1 8 76.51075 36.00585368 12.72999165 30.10164697 63.78075835 89.24074165
2 A 2 8 160.9014 59.41736724 21.00721165 49.67416212 139.8941884 181.9086116
3 A 3 8 325.2713625 117.812772 41.653105 98.49394223 283.6182575 366.9244675
4 B 1 8 71.09765 38.1718855 13.49579954 31.91249489 57.60185046 84.59344954
5 B 2 8 142.1246375 67.33487775 23.80647433 56.29336655 118.3181632 165.9311118
6 B 3 8 336.24105 115.3372241 40.77786664 96.42433239 295.4631834 377.0189166
7 C 1 8 46.3798 25.53625659 9.028430101 21.34884477 37.3513699 55.4082301
8 C 2 8 118.4134875 65.35360032 23.10598698 54.63697717 95.30750052 141.5194745
9 C 3 8 281.5792375 117.049729 41.38332856 97.85602232 240.1959089 322.9625661
10 D 1 7 86.62565714 37.50832694 14.17681503 34.6894167 72.44884212 100.8024722
11 D 2 6 120.86055 48.8806253 19.95543172 51.2970703 100.9051183 140.8159817
12 D 3 8 274.203525 86.86426453 30.71115524 72.62034249 243.4923698 304.9146802
13 E 1 8 57.2146875 26.41120188 9.337769974 22.08031734 47.87691753 66.55245747
14 E 2 8 120.2972875 45.39353788 16.04903923 37.94994738 104.2482483 136.3463267
15 E 3 8 333.678325 103.5042683 36.594285 86.53173379 297.08404 370.27261
16 F 1 8 81.166225 37.18034784 13.14523804 31.08354867 68.02098696 94.31146304
17 F 2 8 161.96095 71.60747702 25.31706629 59.86534893 136.6438837 187.2780163
18 F 3 7 331.6084286 195.9165314 74.04948854 181.1925711 257.55894 405.6579171
19 G 1 8 68.743 44.9696775 15.89918195 37.59559123 52.84381805 84.64218195
20 G 2 8 142.6372625 80.95226642 28.62094827 67.67778838 114.0163142 171.2582108
21 G 3 8 362.7983625 143.4288665 50.70976205 119.9095331 312.0886004 413.5081246
22 H 1 8 55.2445625 31.35474448 11.08557622 26.21322238 44.15898628 66.33013872
23 H 2 8 125.48165 97.10938809 34.33335342 81.18548013 91.14829658 159.8150034
24 H 3 8 402.004975 176.9811861 62.57229841 147.9599743 339.4326766 464.5772734
25 I 1 7 63.47242857 30.17893187 11.40656408 27.91085683 52.06586449 74.87899265
26 I 2 8 167.4317875 76.30008692 26.97615443 63.78846899 140.4556331 194.4079419
27 I 3 8 357.2169125 108.6952357 38.42956911 90.8714911 318.7873434 395.6464816
28 J 1 8 65.6122875 29.5111406 10.43376382 24.67193096 55.17852368 76.04605132
29 J 2 8 126.4079875 66.06781069 23.35849848 55.23407198 103.049489 149.766486
30 J 3 8 502.1878625 140.3556285 49.62320833 117.3402419 452.5646542 551.8110708
31 K 1 8 57.3303625 27.86618278 9.852183406 23.29671181 47.47817909 67.18254591
32 K 2 8 106.203025 52.69142229 18.629231 44.05113142 87.573794 124.832256
33 K 3 7 348.9141 166.4786335 62.92300897 153.9670564 285.991091 411.837109
34 L 1 8 54.4373375 32.89032536 11.62848605 27.49700012 42.80885145 66.06582355
35 L 2 8 173.801975 114.7929316 40.58543019 95.9692925 133.2165448 214.3874052
36 L 3 8 427.5621 141.5160672 50.03348538 118.3103929 377.5286146 477.5955854
37 N 1 8 53.19965 18.75340803 6.630330993 15.67824146 46.56931901 59.82998099
38 N 2 8 102.83935 49.66950295 17.56082118 41.52474363 85.27852882 120.4001712
39 N 3 8 361.586525 123.0931912 43.52001511 102.9084831 318.0665099 405.1065401
40 M 1 8 90.71195 45.58629605 16.11718953 38.11109723 74.59476047 106.8291395
41 M 2 8 201.219475 90.14563026 31.87129323 75.36363289 169.3481818 233.0907682
42 M 3 8 521.6052875 144.9878341 51.26094033 121.2128627 470.3443472 572.8662278
43 O 1 8 72.572875 37.76520057 13.35201471 31.57249778 59.22086029 85.92488971
44 O 2 8 181.783975 97.67988452 34.53505436 81.66242708 147.2489206 216.3190294
45 O 3 8 415.6914625 179.8261041 63.57812883 150.3383853 352.1133337 479.2695913
46 P 1 8 58.817375 22.82069356 8.068333583 19.07857726 50.74904142 66.88570858
47 P 2 8 131.3006875 67.37113752 23.8192941 56.32368048 107.4813934 155.1199816
48 P 3 8 286.8072625 49.36617664 17.45357913 41.27115649 269.3536834 304.2608416
49 Q 1 8 76.4929625 50.22446933 17.75703142 41.98870713 58.73593108 94.24999392
50 Q 2 8 174.730975 111.6447534 39.47238111 93.33734965 135.2585939 214.2033561
51 Q 3 8 363.4492125 169.6025304 59.96354967 141.7912638 303.4856628 423.4127622
52 R 1 8 72.9998875 24.48056866 8.655188055 20.46626758 64.34469945 81.65507555
53 R 2 8 172.1736375 97.8407617 34.59193304 81.79692377 137.5817045 206.7655705
54 R 3 8 453.68845 177.718465 62.83296587 148.5763549 390.8554841 516.5214159
Code with Facet:
library(ggplot2)
AllHarvest<-summarySE(CombinedwinrhizoSigmaPlot, measurevar="Length.cm.", groupvars=c("Rootstock","Harvest"))
AllHarvest$Lower <- AllHarvest$Length.cm. - AllHarvest$se
AllHarvest$Upper <- AllHarvest$Length.cm. + AllHarvest$se
AllHplot<-ggplot(AllHarvest,aes(x=Rootstock, y=Length.cm., fill=Rootstock)) + ylim(0,600) + geom_bar(colour = "black", stat="identity", width = 0.7, fill = "#4DAF4A") + geom_errorbar(aes(ymax=Upper, ymin=Lower, width = 0.2), size=0.5, color = "black")
AllHplot + facet_grid(Harvest ~ .)
Code for Harvest #3 including mean separation letters above bars:
Harvest3 <- AllHarvest[which(Harvest =="3"),]
Harvest3summary <- summarySE(Harvest3, measurevar="Length.cm.", groupvars=c("Rootstock"))
offset.v = -8 # offsets for mean letters
offset.h = 0.4
Harvest3summary$Lower <- Harvest3summary$Length.cm. - Harvest3summary$se
Harvest3summary$Upper <- Harvest3summary$Length.cm. + Harvest3summary$se
Harvest3plot <- ggplot(Harvest3summary,aes(x=Rootstock, y=Length.cm., fill=Rootstock)) + ylim(0,600) +
geom_bar(colour = "black", stat="identity", width = 0.7, fill = "#4DAF4A") + geom_errorbar(aes(ymax=Upper, ymin=Lower, width = 0.2), size=0.5, color = "black") +
geom_text(aes(label=c("CDE", "CDE", "DE", "DE","CDE","E","BCDE","ABCDE","ABCDE","AB","CDE", "ABC", "ABCDE","A","ABCD","CDE","BCDE","ABC"), size = 8, hjust=offset.h, vjust=offset.v)) +
labs(x = "", y = expression(Total~Root~Length~(cm))) + theme(legend.position = "none") +
## ggtitle("Main title") +
theme(
plot.title = element_text(size = rel(1.5),
face = "bold", vjust = 1.5),
axis.title = element_text(face = "bold"),
axis.title.y = element_text(vjust= 1.8, size=14),
axis.text.y=element_text(size=12),
axis.title.x = element_text(vjust= -0.5),
axis.text.x = element_text(angle=60, hjust=1.1,vjust=1.05, size=14, colour="black"))
panel.border = element_rect(colour="black")
After looking around a bit I was able to answer my own question through trial and error. I am adding code for those who may want to use it in the future - this is especially helpful for journal figures where mean separation letters are needed. A note - when using geom_text you add your separation letters as one long string for all graphs in the facet, so make sure you have them ordered correctly:
AllHplot<-ggplot(AllHarvest,aes(x=Rootstock, y=Length.cm., fill=Rootstock)) + coord_cartesian(ylim=c(0,650)) + geom_blank()+ theme_bw() + geom_bar(colour = "black", stat="identity", width = 0.7, fill = "black") + geom_errorbar(aes(ymax=Upper, ymin=Lower, width = 0.2), size=0.5, color = "black")+
labs(x = "", y = expression(paste(Total~Root~Length~(cm)))) + scale_y_continuous(expand = c(0,0)) +
theme(
axis.title = element_text(face = "bold"),
axis.title.y = element_text(face="bold", vjust= 1.8, size=14),
axis.text.y=element_text(size=12, colour="black"),
axis.title.x = element_text(vjust= -0.5),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
axis.text.x = element_text(angle=60, hjust=1.1, vjust=1.05, size=14, colour="black"),
strip.background = element_blank(),
strip.text = element_blank())+ geom_text(aes(label=c("CDE", "CDE", "DE", "DE","CDE","E","BCDE","ABCDE","ABCDE","AB","CDE", "ABC", "ABCDE","A","ABCD","CDE","BCDE","ABC", "ABC", "ABCDEF", "CDEF", "BCDEF","BCDEF","ABC","BCDEF","EF","ABC","CDEF","DEF", "ABCD", "F","A","AB","BCDEF","ABCDE","ABC", "ABC", "ABC", "D", "AB","BCD","AB","ABCD","CD","ABCD","ABCD","BCD", "CD", "BCD","A","ABC","ABCD","ABC","ABC"), hjust=offset.h, vjust=offset.v)) + facet_grid(Harvest ~ .,as.table=F)

Resources