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.
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.
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.
The scale_x_continuous() and scale_y_continuous() methods can be used to disable scientific notation and convert scientific labels to discrete form.
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())
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)))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With