I have the dataframe below
d1_1<-structure(list(conm = c("Amazon.com Inc", "Apple Inc", "Facebook Inc",
"Intl Business Machines Corp", "Microsoft Corp", "Netflix Inc",
"Paypal Holdings Inc", "Qualcomm Inc", "Tesla Inc", "Twitter Inc"
), avg_profit = c(12613.1818181818, 62428.7272727273, 14952.8181818182,
20280.8181818182, 40535.5833333333, 1145.86854545455, 2810.25,
6653.63636363636, 705.462090909091, 189.0503)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -10L))
and I want to round the text labels of the bars to the closes thousand like the image below.
I do
roundUp <- function(x) 10^ceiling(log10(x))
and
library(ggplot2)
p<-ggplot(data=d1_1, aes(x=avg_profit, y=reorder(conm,avg_profit)))
geom_bar(stat="identity")
ylab("Company") xlab("Average profit in $millions")
scale_x_continuous(name="Average profit in $millions", labels=scales::dollar_format())
geom_text(aes(label=roundUp(avg_profit)), position=position_dodge(width=0.9), hjust=-0.2,size=3)
p
CodePudding user response:
label=round(avg_profit, -3)/1000
library(ggplot2)
ggplot(data = d1_1, aes(x = avg_profit, y = reorder(conm, avg_profit)))
geom_bar(stat =
"identity")
ylab("Company") xlab("Average profit in $millions")
scale_x_continuous(name =
"Average profit in $millions", labels = scales::dollar_format())
geom_text(
aes(label = round(avg_profit, -3)/1000),
position = position_dodge(width = 0.9),
hjust = -0.2,
size = 3
)