Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display only integer values on an axis using ggplot2

Tags:

r

ggplot2

I have the following plot:

library(reshape) library(ggplot2) library(gridExtra) require(ggplot2)    data2<-structure(list(IR = structure(c(4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L ), .Label = c("0.13-0.16", "0.17-0.23", "0.24-0.27", "0.28-1" ), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L,  2L, 2L, 2L, 2L), .Label = c("Real queens", "Simulated individuals" ), class = "factor"), value = c(15L, 11L, 29L, 42L, 0L, 5L, 21L,  22L), Legend = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Real queens",  "Simulated individuals"), class = "factor")), .Names = c("IR",  "variable", "value", "Legend"), row.names = c(NA, -8L), class = "data.frame") p <- ggplot(data2, aes(x =factor(IR), y = value, fill = Legend, width=.15))   data3<-structure(list(IR = structure(c(4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L ), .Label = c("0.13-0.16", "0.17-0.23", "0.24-0.27", "0.28-1" ), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L,  2L, 2L, 2L, 2L), .Label = c("Real queens", "Simulated individuals" ), class = "factor"), value = c(2L, 2L, 6L, 10L, 0L, 1L, 4L,  4L), Legend = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Real queens",  "Simulated individuals"), class = "factor")), .Names = c("IR",  "variable", "value", "Legend"), row.names = c(NA, -8L), class = "data.frame") q<- ggplot(data3, aes(x =factor(IR), y = value, fill = Legend, width=.15))   ##the plot## q + geom_bar(position='dodge', colour='black') + ylab('Frequency') + xlab('IR')+scale_fill_grey() +theme(axis.text.x=element_text(colour="black"), axis.text.y=element_text(colour="Black"))+ opts(title='', panel.grid.major = theme_blank(),panel.grid.minor = theme_blank(),panel.border = theme_blank(),panel.background = theme_blank(), axis.ticks.x = theme_blank()) 

I want the y-axis to display only integers. Whether this is accomplished through rounding or through a more elegant method isn't really important to me.

like image 760
Atticus29 Avatar asked Mar 25 '13 18:03

Atticus29


People also ask

How do I show more values on Y axis in R?

Method 1: Change Axis Scales in Base R To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

How do I customize axis labels in R?

Key ggplot2 R functionsp + xlab(“New X axis label”): Change the X axis label. p + ylab(“New Y axis label”): Change the Y axis label. p + labs(x = “New X axis label”, y = “New Y axis label”): Change both x and y axis labels.

How do I get rid of scientific notation in ggplot2?

The scale_x_continuous() and scale_y_continuous() methods can be used to disable scientific notation and convert scientific labels to discrete form.


2 Answers

If you have the scales package, you can use pretty_breaks() without having to manually specify the breaks.

q + geom_bar(position='dodge', colour='black') +  scale_y_continuous(breaks= pretty_breaks()) 
like image 122
Sealander Avatar answered Oct 13 '22 08:10

Sealander


This is what I use:

ggplot(data3, aes(x = factor(IR), y = value, fill = Legend, width = .15)) +   geom_col(position = 'dodge', colour = 'black') +    scale_y_continuous(breaks = function(x) unique(floor(pretty(seq(0, (max(x) + 1) * 1.1))))) 
like image 33
Daniel Gardiner Avatar answered Oct 13 '22 08:10

Daniel Gardiner