Cut Function in R- Labeling Without Scientific Notations for Use in Ggplot2

cut function in R- labeling without scientific notations for use in ggplot2

Use argument dig.lab in cut function:

a<-c(1,10,100,1000,100000,1000000)
b<-cut(a,
breaks=data.frame(
classIntervals(
a,n=3,method="quantile")[2])[,1],
include.lowest=T,dig.lab=10) ##Number of digits used
b
[1] [1,70] [1,70] (70,34000] (70,34000]
[5] (34000,1000000] (34000,1000000]
Levels: [1,70] (70,34000] (34000,1000000]

Show cut categories in R without e formatting

Try this:

#Code
num_seq <- 1:2000
break_m <- seq(0, 2000, by=30)
num_broken <- cut(num_seq, breaks = break_m,dig.lab = 10)
unique(num_broken)

Outout:

 [1] (0,30]      (30,60]     (60,90]     (90,120]    (120,150]   (150,180]   (180,210]  
[8] (210,240] (240,270] (270,300] (300,330] (330,360] (360,390] (390,420]
[15] (420,450] (450,480] (480,510] (510,540] (540,570] (570,600] (600,630]
[22] (630,660] (660,690] (690,720] (720,750] (750,780] (780,810] (810,840]
[29] (840,870] (870,900] (900,930] (930,960] (960,990] (990,1020] (1020,1050]
[36] (1050,1080] (1080,1110] (1110,1140] (1140,1170] (1170,1200] (1200,1230] (1230,1260]
[43] (1260,1290] (1290,1320] (1320,1350] (1350,1380] (1380,1410] (1410,1440] (1440,1470]
[50] (1470,1500] (1500,1530] (1530,1560] (1560,1590] (1590,1620] (1620,1650] (1650,1680]
[57] (1680,1710] (1710,1740] (1740,1770] (1770,1800] (1800,1830] (1830,1860] (1860,1890]
[64] (1890,1920] (1920,1950] (1950,1980] <NA>
66 Levels: (0,30] (30,60] (60,90] (90,120] (120,150] (150,180] (180,210] ... (1950,1980]

Generating compact scientific notation with ggplot2 on R

Starting with a similar plot:

ggplot(mtcars, aes(wt, mpg * 1E-8)) +
geom_point()

Sample Image

If we know the scale we want to use, we could define it, and then we could either scale the data on the way in, or change the labels on the y axis, will look the same, except for y axis label (which we could rename whatever we want):

divisor = 1E-8

ggplot(mtcars, aes(wt, mpg * 1E-8 / divisor)) +
geom_point() +
labs(title = formatC(divisor, format = "e", digits = 0))

Sample Image

ggplot(mtcars, aes(wt, mpg * 1E-8)) +
geom_point() +
scale_y_continuous(labels = function(x) x / divisor) +
labs(title = formatC(divisor, format = "e", digits = 0))

Sample Image


Edit:

If you also want a title, you could alternatively use annotate to write the text outside the plot area, and then scootch the title up:

ggplot(mtcars, aes(wt, mpg * 1E-8)) +
geom_point() +
scale_y_continuous(labels = function(x) x / divisor) +
annotate("text", x = -Inf, y = Inf, hjust = 0, vjust = -0.5,
label = formatC(divisor, format = "e", digits = 0)) +
coord_cartesian(clip = "off") +
labs(title = "Title here") +
theme(plot.title = element_text(margin = margin(0,0,20,0)))

Sample Image

Get rid of scientific numbers in boxplot labels

Add dig.lab=4 to your cut() function:

cvalue2<-cut(value2,breaks=c(1000,1100,1200,1300,1400,1500,1600), dig.lab=4)

The default is 3 so cut() uses scientific notation to format the ranges. Alternatively you could specify labels at the midpoint of each group:

cvalue2<-cut(value2, breaks=c(1000,1100,1200,1300,1400,1500,1600), 
labels=seq(1050, 1550, by=100))

How to remove E notation from label cut intervals and replace with shorthand labels?

Had to write up some custom functions to break up the interval:

library(scales)
library(tidyverse)

SI_format <- function(x) {
case_when(
x < 1e3 ~ as.character(x),
x < 1e6 ~ paste0(as.character(x/1e3), "K"),
x < 1e9 ~ paste0(as.character(x/1e6), "M"),
x < 1e12 ~ paste0(as.character(x/1e9), "B"),
x < 1e15 ~ paste0(as.character(x/1e12), "T"),
TRUE ~ "..."
)
}

pretty_labels <- function(x) {

first_bracket <- str_extract(x,"^.")
last_bracket <- str_extract(x,".$")
first_number <- SI_format(as.numeric(str_extract(x,"(\\d+)")))
last_number <- SI_format(as.numeric(str_extract(x,"(\\d+)(?!.*\\d)")))

new_label <- paste0(first_bracket,first_number,",",last_number,last_bracket)
return(new_label)
}

dat <- data.frame(test_x=c(0,1, 10, 1e2,1e3,1e4,1e5,1e6), test_y=1) %>%
mutate(label_cut=cut_width(test_x,1e3, boundary = 0, dig.lab=10))

levels(dat$label_cut) <- pretty_labels(levels(dat$label_cut))

ggplot(dat) +
geom_col(aes(test_x,label_cut)) +
scale_x_continuous(labels = label_number_si())

Sample Image

How to remove scientific notation from this legend ggplot

I could not get your data and try this to see if it works:

break_year_map_data_nona %>% 
mutate(
break_year = cut(value, breaks = c(1961, 1980, 1990, 2000, 2010, 2016),
labels = c("1960-", "1980-", "1990-", "2000-", "2010-2016"))
) %>%
ggplot(aes(long, lat, group = group, fill = break_year )) +
geom_polygon() +
coord_map(xlim=c(-180,180)) +
theme_void() +
labs(title = "", fill = "Break year")

Format bin labels as currency

Use labels:

xbin <- cut(x, 
breaks = c(0, 100, 1000, 10000),
labels = c("$0:100", "$100:1,000", "$1000:10,000"))
table(xbin)
# $0:100 $100:1,000 $1000:10,000
# 149 465 210

Or use dig.lab to avoid scientific notation:

xbin <- cut(x, 
breaks = c(0, 100, 1000, 10000),
dig.lab = 5)
table(xbin)
# xbin
# (0,100] (100,1000] (1000,10000]
# 149 465 210


Related Topics



Leave a reply



Submit