Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include a comma separator for data labels

Tags:

r

ggplot2

I have a box plot using ggplot which lists the data labels, but am not able to bring a comma separator for 1000s in the data label. sep ="," in aes doesn't seem to do the trick.

ggplot(based,aes(x=Cust=Claim.USD)) +
  geom_boxplot() +
  geom_text(data=subset(based,USD>10000), aes(label=USD, sep=","),
            hjust=1, vjust=1)+
  scale_y_continuous(labels=comma)
like image 591
Murali Avatar asked Sep 06 '15 19:09

Murali


People also ask

How do I separate data labels in Excel?

On the Layout tab, in the Labels group, click Data Labels, and then click the option that you want. For additional data label options, click More Data Label Options, click Label Options if it's not selected, and then select the options that you want.

How do I create a custom data label in Excel?

Press with mouse on Add Data Labels". Double press with left mouse button on any data label to expand the "Format Data Series" pane. Enable checkbox "Value from cells". A small dialog box prompts for a cell range containing the values you want to use a s data labels.

What are data labels?

In machine learning, data labeling is the process of identifying raw data (images, text files, videos, etc.) and adding one or more meaningful and informative labels to provide context so that a machine learning model can learn from it.


1 Answers

The comma function is in the scales package, which you'll need to load. Also get rid of sep, that's not an aesthetic mapping. This should work:

library(scales)
ggplot(based,aes(x=Cust=Claim.USD)) +
  geom_boxplot() +
  geom_text(data=subset(based,USD>10000), aes(label = comma(USD)),
            hjust=1, vjust=1)+
  scale_y_continuous(labels = comma)

Judging by your argument names, you might prefer scales::dollar instead of scales::comma.

like image 122
Gregor Thomas Avatar answered Sep 23 '22 05:09

Gregor Thomas