How to Display Only Integer Values on an Axis Using Ggplot2

How to display only integer values on an axis using ggplot2

With scale_y_continuous() and argument breaks= you can set the breaking points for y axis to integers you want to display.

ggplot(data2, aes(x =factor(IR), y = value, fill = Legend, width=.15)) +
geom_bar(position='dodge', colour='black')+
scale_y_continuous(breaks=c(1,3,7,10))

forcing ggplot2 y-axis label to be of integer only, and give proper breaks

First, as your y data are continuous you should use scale_y_continuous(). In this function you can add argument breaks= pretty_breaks() (add library scales to use function pretty_breaks()). If you don't provide any number inside pretty_breaks() then in this case you will get integer numbers on y axis. You can set number of breaks to display, for example, pretty_breaks(4) but for the first facet where you have range 0-10 it will still display only integer values and the number of breaks will be larger to get "nice" numbers.

library(scales)
ggplot(data=chart.data,aes(x=x,y=y)) +
geom_bar(stat="identity") +
facet_wrap(facets=~group,nrow=1,scale="free_y")+
scale_y_continuous(breaks= pretty_breaks())

Modifying ggplot2 Y axis to use integers without enforcing an upper limit

Use scale_y_continuous(breaks=c(0,2,4,6,8,10)). So your plotting code will look like:

ggplot(mtcars, aes(x=Cylinders, y = N, fill = Gears)) + 
geom_bar(position="dodge", stat="identity") +
ylab("Count") + theme(legend.position="top") +
scale_y_continuous(breaks=c(0,2,4,6,8,10)) +
scale_x_discrete(drop = FALSE)

EDIT: Alternatively you can use scale_y_continuous(breaks=seq(round(max(mtcars$N),0))) in order to automatically adjust the scale to the maximum value of the y-variable. When you want the breaks more then 1 from each other, you can use for example seq(from=0,to=round(max(mtcars$N),0),by=2)

How to display all values on X and Y axes in R ggplot2

cycle_time <- 1:15
volume <- c(109.12,381.11,812.31,1109,4439.32, 12148.29,32514.32,82231.24,183348.44,329472.36,462381.96,541111.67,
576516.09, 590450.40,595642.83)

dfx <- data.frame(cycle_time,volume)

p1<- ggplot(dfx, aes(cycle_time,volume)) + geom_line()
p1 +
scale_x_continuous(breaks=seq(1,15,1))+
scale_y_continuous(labels=scales::comma)

Sample Image

Created on 2022-04-29 by the reprex package (v2.0.1)

Frequency count histogram displaying only integer values on the y-axis?

How about:


ggplot(data = sample, aes (x = binMidPts, y = counts)) + geom_col() +
scale_y_continuous( breaks=round(pretty( range(sample$counts) )) )

Sample Image

This answer suggests pretty_breaks from the scales package. The manual page of pretty_breaks mentions pretty from base. And from there you just have to round it to the nearest integer.

Only show maximum and minimum dates/values for x and y axis label in ggplot2 plot

You could achieve your desired result by setting the breaks of the scales equal to the range of the variables mapped on x and y:

Notes:

  1. If you want to start the y-scale at zero you could do breaks = c(0, max(df$value)).

  2. Instead of removing the axis titles via theme you could simply do it via labs.

library(ggplot2)
ggplot(df, aes(
x = date, y = value, group = type,
color = type, fill = type
)) +
geom_area(alpha = 0.4) +
scale_y_continuous(breaks = range(df$value)) +
scale_x_date(breaks = range(df$date)) +
scale_color_manual(
values = c("1" = "gray", "2" = "red", "3" = "blue"),
aesthetics = c("color", "fill"), name = "type"
) +
labs(x = NULL, y = NULL)

Sample Image



Related Topics



Leave a reply



Submit