I am practicing with the "diamonds" dataset in ggplot 2. I am trying to make a bar chart plot, to demonstrate the average price per color category:
This is the code I wrote:
ggplot(data=diamonds)+
geom_bar(mapping=aes(x=color,y=price),
fun="mean",stat="summary",fill="yellow")+
geom_text(
aes(label=scales::comma(
round(mean(price),digits=3)
),
x=color,y=price),
stat="summary",size=3,
vjust=-1)
The visualization being generated by this code is shown as follow:
The problems that I have encountered are regarding the data labels:
I have searched online for possible answers, but could not find a good solution
The first issue is that using mean(price)
you compute the overall mean of price
. To get the mean per color
which is computed by stat_summary
and which you display as bars you have to use label = after_stat(y)
. Here y
is the variable which contains the values computed by stat_summary
under the hood. This value is automatically mapped on the y
aesthetic. However, to use it for the label
we have to wrap in after_stat
to access the computed value.
Second, the issue is that you wrapped round
in scales::comma
which will round the already round
ed value a second time with a default accuracy=1
. To fix that I would suggest to use the accuracy
argument of scales::comma
, e.g. set it to .001
to round to three decimals.
library(ggplot2)
ggplot(data = diamonds, aes(x = color, y = price)) +
geom_bar(
fun = "mean", stat = "summary", fill = "yellow"
) +
geom_text(
aes(
label = after_stat(scales::comma(y, accuracy = .001))
),
fun = "mean",
stat = "summary",
size = 3,
vjust = -1
)