0

Overview:

I have a summarised dataframe called "leaf.aggregate.1 (see below)" and I want to label the values (i.e. to 3 significant figures) for Mean_Canopy_Index per Urbanisation_index category (x-axis) onto the bars in the barplot using ggplot().

As you can see (barplot below - not using the sprintf() function),the bars entitled 1, 3 and 4 for the Urbanisation_index are labelled to 7 decimal places (e.g. 61.66667, 76.42857, and 72.05882).

How do you show the Mean_Canopy_Index values on the individual bars to 3 significant figures (i.e. 61.7, 75.0, 76.4, and 72.1) instead?

From browsing different answers on Stackoverflow, I tried to incorporate these suggestions, but I was unsuccessful.

The solution suggests the function sprintf() is the most appropriate method; however, the Mean_Canopy_Index column is numeric, not a character (see the error message for 'Attempt 2' in R-code section).

If anyone can help, I would be deeply appreciative.

R-code

#Attempt 1

Canopy_barplot<-ggplot(data=leaf.aggregate.1, aes(x=Urbanisation_Index, y=Mean_Canopy_Index)) +
                                                      geom_bar(stat="identity", color="steelblue", fill="steelblue", width=0.5) +
                                                      geom_text(aes(label=Mean_Canopy_Index), vjust=1.6, color="white", size=3.5, round(aes(label=Mean_Canopy_Index, digits = 2)))+
                                                      theme_minimal() +
                                                      theme(panel.background = element_blank(), 
                                                      panel.grid.major = element_blank(), 
                                                      panel.grid.minor = element_blank())

#Error message
Error in round(aes(label = Mean_Canopy_Index, digits = 2)) : 
 non-numeric argument to mathematical function

#Attempt 2


                  Canopy_barplot<-ggplot(data=leaf.aggregate.1, aes(x=Urbanisation_Index, y=Mean_Canopy_Index)) +
                                         geom_bar(stat="identity", color="steelblue", fill="steelblue", width=0.5) +
                                         geom_text(aes(label=Mean_Canopy_Index), vjust=1.6, color="white", size=3.5)+
                                         sprintf(aes(label=Mean_Canopy_Index("%0.2f", round(Mean_Canopy_Index, digits = 2))))+
                                         theme_minimal() +
                                         theme(panel.background = element_blank(), 
                                         panel.grid.major = element_blank(), 
                                         panel.grid.minor = element_blank())

#Error message
    Error in sprintf(aes(label = Mean_Canopy_Index("%0.2f", round(Mean_Canopy_Index,  : 
  'fmt' is not a character vector

Barplot without using the function sprintf()

#R-code

Canopy_barplot<-ggplot(data=leaf.aggregate.1, aes(x=Urbanisation_Index, y=Mean_Canopy_Index)) +
                                             geom_bar(stat="identity", color="steelblue", fill="steelblue", width=0.5) +
                                             geom_text(aes(label=Mean_Canopy_Index), vjust=1.6, color="white", size=3.5)+
                                             theme_minimal() +
                                             theme(panel.background = element_blank(), 
                                             panel.grid.major = element_blank(), 
                                             panel.grid.minor = element_blank())

enter image description here

Data Structure

str(leaf.aggregate.1)
'data.frame':   4 obs. of  5 variables:
 $ Species           : Factor w/ 1 level "Quercus petraea": 1 1 1 1
 $ Urbanisation_Index: num  1 2 3 4
 $ Counts            : num  6 17 14 17
 $ Mean_Canopy_Index : num  61.7 75 76.4 72.1
 $ SD_Canopy_Index   : num  17.5 20 10.3 16.5

Data

structure(list(Species = structure(c(1L, 1L, 1L, 1L), .Label = "Quercus petraea", class = "factor"), 
    Urbanisation_Index = c(1, 2, 3, 4), Counts = c(6, 17, 14, 
    17), Mean_Canopy_Index = c(61.6666666666667, 75, 76.4285714285714, 
    72.0588235294118), SD_Canopy_Index = c(17.5119007154183, 
    20, 10.2710518202619, 16.494205756247)), class = "data.frame", row.names = c(NA, 
-4L))
Alice Hobbs
  • 1,021
  • 1
  • 15
  • 31
  • According to linked answer `format(round(Mean_Canopy_Index, 1))` should work – pogibas Jan 25 '19 at 08:24
  • Just using `aes(label= round(Mean_Canopy_Index, digits = 2))` didn't work? – Jaap Jan 25 '19 at 08:25
  • @Jaap OP wants `75.0` too – pogibas Jan 25 '19 at 08:25
  • @PoGibas ok, then this can be closed indeed – Jaap Jan 25 '19 at 08:26
  • 1
    In this instance, my question relates to changing values to significant figures on individual bar plots using ggplot() notation. Although, I can run the suggested code in the possible duplicate solution above, it’s not really a duplicate question. I am unsure how to incorporate the suggested code into geom_text() in ggplot() format. If anyone can help, then many thanks. – Alice Hobbs Jan 25 '19 at 08:33
  • Doesn't `aes(label = format(round(Mean_Canopy_Index, 1)))` work for you? – pogibas Jan 25 '19 at 08:44
  • 2
    I disagree, this question should not be closed. Beginners and /intermediate R users (like me) will find this type of question really useful. I successfully figured out how to change numerical values into 1, 2, or 3 decimal places separately, but I was unable to integrate these solutions into the correct geom_text() format due to on my current knowledge base. There are people (like me) who frequently use 'Stackoverflow' as a source of help but are not as advanced yet! It takes practice and continued learning from kind people like yourselves who are willing to help, and pass on your knowledge – Alice Hobbs Jan 25 '19 at 09:05

1 Answers1

1

You were close, sprintf("%.1f", ...) will give you 3 sig. fig. (1 decimal precision):

    df %>%
  ggplot(aes(x=Urbanisation_Index, y=Mean_Canopy_Index)) +
  geom_bar(stat="identity", color="steelblue", fill="steelblue", width=0.5) +
  geom_text(aes(label = sprintf("%.1f", Mean_Canopy_Index)),
            vjust=1.6,
            color="white",
            size=3.5)+
  theme_minimal() +
  theme(panel.background = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())
Paul
  • 2,877
  • 1
  • 12
  • 28
  • Doesn't work as OP wants `75.0` – pogibas Jan 25 '19 at 08:27
  • 1
    Thanks, Paul! Thank you so much! I was trying to get my code right for a long time using different combinations of code. Your right, I was so close. Still learning! Take care and have a good day – Alice Hobbs Jan 25 '19 at 08:56