jkinstack
jkinstack

Reputation: 79

geom_text labels position and issue with theme()

plotdi <- read_excel("DI_graph_3.xlsx")
plotdi <- as.data.frame(plotdi)
plotdi$Tr <- factor(plotdi$Tr, levels = c("Control", "BC", "Pa", "BC+Pa"))

colours <- c(Control = "#00BA38", BC = "red", Pa = "#619CFF", `BC+Pa` = "black")

plot_t<- ggplot(plotdi, aes(x=Z, y=Taxamean, fill=Tr)) +
  geom_bar(stat="identity", colour="black",position="dodge") +
  scale_fill_manual(values = colours, name = "Treatment") +
  xlab("")+ ylab(bquote('Richness'))+
  facet_grid(~Tm) +
  scale_y_continuous(limits = c(700, 1100), oob = rescale_none) +
  geom_errorbar(aes(ymin=Taxamean, ymax=Taxamean+TaxaSE), size=0.5,   
                width=.25,position=position_dodge(.9)) +
  theme(axis.title.y = element_text(size=12, face = "bold"),
        legend.text = element_text(size=10), 
        legend.key.size = unit(0.4, "cm"),
        legend.position = c(0.91, 0.89), 
        legend.title = element_blank(), 
        legend.background=element_blank(),
        axis.text.x = element_text(size=10, face = "bold"), 
        strip.text.x = element_text(size = 10, face = "bold"))
plot_t

plot_t + geom_text(aes(label=Taxa_label, size = 1, vjust = -1, hjust = 1.55))

I need to make a graph in this format, but with the labels of Taxa_label

enter image description here

but my graph ends up looking like this, although this code worked previously

enter image description here

> dput(plotdi)

structure(list(Z = c("A", "A", "A", "A", "A", "A", "A", "A", 
"B", "B", "B", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", 
"C", "C", "C"), Tm = c("T1", "T1", "T1", "T1", "T2", "T2", "T2", 
"T2", "T1", "T1", "T1", "T1", "T2", "T2", "T2", "T2", "T1", "T1", 
"T1", "T1", "T2", "T2", "T2", "T2"), Tr = structure(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), levels = c("Control", "BC", "Pa", "BC+Pa"
), class = "factor"), S = c("T2-R5-049-B-Ex2", "T2-R6-054-B-Ex2", 
"T2-R7-057-B-Ex2", "T2-R8-061-B-Ex2", "T3-R5-085-B-Ex2", "T3-R6-089-B-Ex2", 
"T3-R7-093-B-Ex2", "T3-R8-097-B-Ex2", "T2-R5-049-D-Ex2", "T2-R6-053-D-Ex2", 
"T2-R7-057-D-Ex2", "T2-R8-061-D-Ex2", "T3-R5-085-D-Ex2", "T3-R6-089-D-Ex2", 
"T3-R7-093-D-Ex2", "T3-R8-097-D-Ex2", "T2-R5-049-E-Ex2", "T2-R6-053-E-Ex2", 
"T2-R7-057-E-Ex2", "T2-R8-061-E-Ex2", "T3-R5-085-E-Ex2", "T3-R6-089-E-Ex2", 
"T3-R7-093-E-Ex2", "T3-R8-097-E-Ex2"), Taxa_S = c(898, 823, 731, 
894, 964, 973, 936, 926, 843, 877, 823, 914, 920, 914, 870, 887, 
757, 722, 763, 847, 763, 832, 733, 861), Shannon_H = c(5.607, 
5.357, 4.916, 5.503, 5.562, 5.562, 5.475, 5.495, 5.326, 5.256, 
5.339, 5.532, 5.567, 5.539, 5.352, 5.542, 5.039, 4.98, 5.233, 
5.456, 5.234, 5.364, 5.016, 5.417), `Evenness_e^H/S` = c(0.3034, 
0.2576, 0.1868, 0.2747, 0.2701, 0.2674, 0.255, 0.263, 0.244, 
0.2187, 0.2531, 0.2765, 0.2845, 0.2784, 0.2427, 0.2878, 0.2038, 
0.2016, 0.2454, 0.2765, 0.2458, 0.2566, 0.2057, 0.2616), N_T = c(4, 
3, 2, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 
4, 4), Taxamean = c(905.25, 851, 820.5, 921.5, 924.75, 941.75, 
927.5, 914.75, 831.5, 883.666666666667, 823.25, 905.75, 869, 
906.75, 840.75, 921.25, 757.25, 760, 776, 828.75, 746, 834.5, 
761.25, 840.25), TaxaSD = c(19.2764969155014, 38.5746030439718, 
126.572113832392, 23.0723499742296, 29.567718883945, 21.4223403638974, 
8.42614977317636, 43.5995030552719, 24.6644143115812, 63.2639971337042, 
37.1427426738163, 13.8894444333338, 50.2991053598372, 7.97391580927046, 
32.3457879792717, 32.087120988542, 20.2381652659853, 53.7401153701776, 
15.790292376436, 26.2218102604174, 12.8840987267251, 32.3367695768558, 
24.8512239805876, 42.2640509180083), TaxaSE = c(9.63824845775068, 
22.2710574513201, 89.5, 11.5361749871148, 14.7838594419725, 10.7111701819487, 
4.21307488658818, 21.799751527636, 12.3322071557906, 36.5254857751558, 
18.5713713369081, 6.94472221666689, 25.1495526799186, 3.98695790463523, 
16.1728939896359, 16.043560494271, 10.1190826329926, 38, 7.89514618821801, 
13.1109051302087, 6.44204936336256, 16.1683847884279, 12.4256119902938, 
21.1320254590042), N_S = c(4, 3, 2, 4, 4, 4, 4, 4, 4, 3, 4, 4, 
4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4), Smean = c(5.57125, 5.46233333333333, 
5.243, 5.5575, 5.55125, 5.4945, 5.5305, 5.42825, 5.29625, 5.352, 
5.2985, 5.6115, 5.3375, 5.50125, 5.27075, 5.528, 5.08425, 5.0045, 
5.2555, 5.434, 5.184, 5.276, 5.1435, 5.32125), SSD = c(0.0472537476467916, 
0.105500394943968, 0.462447834896002, 0.0484458460551571, 0.0629358138635442, 
0.0453615108507937, 0.0621852072441671, 0.056435065931269, 0.0471336751519901, 
0.264410665442981, 0.11403362077329, 0.059607046563305, 0.16652227078282, 
0.037455529186846, 0.0727432700208984, 0.0264952825989836, 0.107738495132118, 
0.0346482322781405, 0.0337293146486358, 0.0492002710019638, 0.0398078719183365, 
0.158954920234218, 0.10429605297741, 0.104630699765094), SSE = c(0.0236268738233958, 
0.0609106814205119, 0.327, 0.0242229230275785, 0.0314679069317721, 
0.0226807554253968, 0.0310926036220836, 0.0282175329656345, 0.023566837575995, 
0.152657568870113, 0.0570168103866452, 0.0298035232816525, 0.0832611353914099, 
0.018727764593423, 0.0363716350104492, 0.0132476412994918, 0.0538692475660589, 
0.0244999999999997, 0.0168646573243179, 0.0246001355009819, 0.0199039359591682, 
0.079477460117109, 0.0521480264887049, 0.0523153498825471), order = c(1, 
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 24), Taxa_label = c("ab", "abc", "bc", "a", "abc", 
"ab", "bc", "ab", "c", "c", "c", "abc", "a", "a", "a", "ab", 
"abc", "abc", "bc", "a", "e", "cd", "de", "bc"), S_label = c("ab", 
"ab", "bc", "ab", "abc", "abc", "abc", "a", "c", "c", "bc", "ab", 
"a", "a", "a", "ab", "bc", "a", "cde", "a", "de", "cd", "e", 
"bc")), row.names = c(NA, -24L), class = "data.frame")

Upvotes: 1

Views: 248

Answers (1)

Shafee
Shafee

Reputation: 19867

Option 1

Specifying position = position_dodge(width = .9) along with angle and hjust we can get a clear plot.

plot_t + geom_text(
  aes(label = Taxa_label),
  position = position_dodge(width = .9),
  angle = 90,
  hjust = -1
)

Plot_with_label


Option 2

plot_t + geom_text(
  aes(label = Taxa_label),
  position = position_dodge(width = .9),
  angle = 90,
  hjust = 1.5,
  vjust = 0.3,
  color = "white",
  fontface = "bold"
)

second_plot_with_label

Upvotes: 1

Related Questions