0

I am not quite new to R, so apologies if this is simple. I have read all the previous questions relating to this and am still unable to solve it.

I need to order barplots in a specific order, which I can do. I then need to order the letters that denote significance in the same order. I import the data in Excel format.

my data

structure(list(Species = c("Lobaria pulmonaria", "Lobaria pulmonaria", "Lobaria pulmonaria", "Ramalina calicaris", "Ramalina calicaris", "Ramalina calicaris", "Sticta limbata", "Sticta limbata", "Sticta limbata", "Sticta sylvatica", "Sticta sylvatica", "Sticta sylvatica", "Lobaria virens", "Lobaria virens", "Lobaria virens", "Hypotrachyna laevigata", "Hypotrachyna laevigata", "Hypotrachyna laevigata", "Pectenia atlantica", "Pectenia atlantica", "Pectenia atlantica"), NP_WC_optimal = c(21.3618237198417, 15.5845298075442, 31.25636339425, 12.95027982548, 11.5492605388886, 13.8964065715251, 23.1619245358907, 53.7708744850057, 50.4182471232504, 18.7436272362963, 21.035699567449, 11.5011803500647, 1.59102057224244, 11.6565164302693, 9.06852286490795, 8.63444946787104, 8.41753904659996, 12.5181232114049, 4.79683342567531, 3.4023650553992, 11.2043763064991 )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -21L))

I can create the significance letters fine, and can plot the barplot in the desired order, without any letters However, when adding the letters by "geom_text", they do not follow the order of the ordered barplot.

I have tried to order the cld letters when I create them (using factor), yet this does not work.

I have been using numbers instead of species names when plotting bar plots, and then editing the species names after. However, this no longer works when trying to generate the significance letters, so I am using actual Species names in the raw data when generating cld letters.

So, I am able to plot the bar graphs in the desired order (using numbers instead of species names). I can also generate correct significance letters separately using Species names in the data. But I can not plot the graph in correct order with correctly ordered letters. Where I am going wrong?

code for cld letter creation

anovaDR <- aov(DR_WC_optimal ~ Species, data = Cardinal_points_TABLE222)
summary(anovaDR)

tukeyDR <- TukeyHSD(anovaDR)
print(tukeyDR)

cldDR <- multcompLetters4(anovaDR,tukeyDR)
print(cldDR)

dtDR <- group_by(Cardinal_points_TABLE222, factor(Species)) %>%
  summarise(DR=mean(DR_WC_optimal), sd = sd(DR_WC_optimal))
print(dtDR)

cldDR <- as.data.frame.list(cldDR$Species) 
dtDR$cldDR <- cldDR$Letters
print(dtDR)

basic code for ordered bar plot

ggplot(data = dtDR, aes(x = factor(Species), y = DR, colour = factor(Species), fill = factor(Species))) + 
  geom_bar(stat="identity") +
  geom_errorbar(aes(ymin = DR - sd, ymax = DR + sd), width = 0.2, color = "grey15", position = position_dodge(0.9))

code to add cld letters to barplot

geom_text(aes(label = cldDR, y = DR +sd), vjust = -20, colour ="grey16")
  • Hint: maybe at that point "dtDR$cldDR <- cldDR$Letters" you should rather join the dfs with merge(df1, df2, by=uniqueID) – Clem Snide May 26 '23 at 06:39
  • 1
    It would be easier to help you if you share your data via e.g. `dput()` so that other can easily run your code, e.g. run `dput(Cardinal_points_TABLE222)` and copy the output into your post. – stefan May 27 '23 at 07:42
  • Your analysis is on the variable *DR_WC_optimal*, but this variable isn't found in your data. It's also helpful to include which packages you're using, for example *multcompView* and the source for the %>% operator. – Sal Mangiafico Jun 01 '23 at 19:02
  • Probably you will have to add the order to the factor variable, e.g. `Data$A = factor(Data$A, levels=unique(Data$A))` or `Data$B = factor(Data$Tigger, levels = c("c", "a", "d", "b"))` – Sal Mangiafico Jun 01 '23 at 20:10

0 Answers0