Home > Mobile >  r ggplot2 round off geom_text values for geom_bar
r ggplot2 round off geom_text values for geom_bar

Time:07-26

Based on the data and code, How can I round off the geom_text to two or a decimal that looks visually good inside the geom_bar.

I did the base round function but I get an error:

Error in labs(fill = "Legend", x = NULL, y = "Avg. Min. Temperature °C")    : 
  non-numeric argument to binary operator

How can I fix this?

Sample Data (df):

df = structure(list(CITYNAME = c("A", "B", "C", 
"D", "E", "F", "G", 
"H", "I", "J", "K", 
"L", "M", "N", "O", "P", 
"Q", "R", "S", "T", 
"U", "V", "W", "X"), AvgTMin = c(20.2816084328988, 
20.3840825075794, 20.0835783555714, 20.347418425369, 20.3811359868631, 
20.7554449391855, 20.9974032162639, 21.2099738161653, 20.4519932648135, 
20.2125743740635, 21.1833765506329, 20.2896719963552, 20.6081700987288, 
20.435186095623, 20.9495391505466, 19.7528992240298, 20.5827896792107, 
20.3185165984173, 21.0522389837351, 20.2764728930218, 20.0887610057421, 
20.1485958052192, 20.7300726136944, 20.1160170580025)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -24L))

Code:

library(tidyverse)

df %>% 
  round(digits = 2) %>% 
  ggplot(aes(x = reorder(CITYNAME,AvgTMin), y = AvgTMin, fill = CITYNAME))  
  geom_bar(stat="identity")  
  labs(fill = "Legend", x = NULL, y = "Avg. Min. Tempreture \u00B0C")  
  theme(axis.text.x = element_text(angle = 90))  
  ggtitle("Temperature Trend By City")  
  coord_cartesian(ylim = c(15,23))

CodePudding user response:

You could use sprintf for the label aes in geom_text like this:

df = structure(list(CITYNAME = c("A", "B", "C", 
                                 "D", "E", "F", "G", 
                                 "H", "I", "J", "K", 
                                 "L", "M", "N", "O", "P", 
                                 "Q", "R", "S", "T", 
                                 "U", "V", "W", "X"), AvgTMin = c(20.2816084328988, 
                                                                  20.3840825075794, 20.0835783555714, 20.347418425369, 20.3811359868631, 
                                                                  20.7554449391855, 20.9974032162639, 21.2099738161653, 20.4519932648135, 
                                                                  20.2125743740635, 21.1833765506329, 20.2896719963552, 20.6081700987288, 
                                                                  20.435186095623, 20.9495391505466, 19.7528992240298, 20.5827896792107, 
                                                                  20.3185165984173, 21.0522389837351, 20.2764728930218, 20.0887610057421, 
                                                                  20.1485958052192, 20.7300726136944, 20.1160170580025)), class = c("tbl_df", 
                                                                                                                                    "tbl", "data.frame"), row.names = c(NA, -24L))
library(tidyverse)

df %>% 
  mutate(AvgTMin = round(AvgTMin, digits = 2)) %>% 
  ggplot(aes(x = reorder(CITYNAME,AvgTMin), y = AvgTMin, fill = CITYNAME))  
  geom_bar(stat="identity")  
  geom_text(aes(label=sprintf("%0.1f", AvgTMin)), vjust=-0.2, size = 2)  
  labs(fill = "Legend", x = NULL, y = "Avg. Min. Tempreture \u00B0C")  
  theme(axis.text.x = element_text(angle = 90))  
  ggtitle("Temperature Trend By City")  
  coord_cartesian(ylim = c(15,23)) 

Created on 2022-07-25 by the reprex package (v2.0.1)

  • Related