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())
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))