I am trying to edit the Y-axis scale to represent the amount the rows of Id. The numbers needed to be shown on the scale are in the millions. I need to abbreviate the number in order for it to be seen on the visualization.
ggplot(sleeptocalories1, aes(Id,TotalCalories)
geom_col(fill="steelblue")
theme(axis.text.x = element_text(angle = 90))
theme(axis.text.y = element_text(angle = 45))
CodePudding user response:
One option would be to convert the labels to millions via the labels
argument of scale_y_continuous
.
Using some fake random example data:
set.seed(123)
sleeptocalories1 <- data.frame(
Id = 1:10,
TotalCalories = runif(10, 86, 12e6)
)
library(ggplot2)
ggplot(sleeptocalories1, aes(Id, TotalCalories))
geom_col(fill = "steelblue")
scale_y_continuous(labels = ~ .x / 1e6)
theme(axis.text.x = element_text(angle = 90))
theme(axis.text.y = element_text(angle = 45))
CodePudding user response:
I like scales::label_number_si()
:
ggplot(data.frame(x = 1:5, y = 10^(2:6)), aes(x, y))
geom_col()
scale_y_continuous(labels = scales::label_number_si())
CodePudding user response:
The scales
library can help with formatting the scales of plots. Scientific notation is an abbreviation.
library(scales)
library(ggplot2)
demo_continuous(c(0, 1e9), labels = label_number_auto())
May not be appropriate for this application, but log breaks are also possible
demo_log10(c(1, 1e5), labels = label_log())