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

六月ゝ 毕业季﹏ 提交于 2019-12-10 20:43:21

问题


I am trying to modify the axes in ggplot2 so that it is one decimal point and has a label for every integer. However, I want to do it without an upper limit so that it will automatically adjust to data of different counts.

The difference between my question and the question posed here (that I was flagged as being a duplicate of) is that I need to make this work automatically for many different data sets, not just for a single one. It must automatically choose the upper limit instead of creating a fixed y-axis with breaks=(0,2,4,...). This question has been answered extremely well by @DidzisElferts below.

Here is my work:

library(data.table)
library(scales)
library(ggplot2)

mtcars <- data.table(mtcars)
mtcars$Cylinders <- as.factor(mtcars$cyl)
mtcars$Gears <- as.factor(mtcars$gear)
setkey(mtcars, Cylinders, Gears)
mtcars <- mtcars[CJ(unique(Cylinders), unique(Gears)), .N, allow.cartesian = TRUE]

ggplot(mtcars, aes(x=Cylinders, y = N, fill = Gears)) + 
               geom_bar(position="dodge", stat="identity") + 
               ylab("Count") + theme(legend.position="top") + 
               scale_x_discrete(drop = FALSE)

As you can see, ggplot2 is plotting the axes with a decimal point and doing it every 2.5 automatically. I'd like to change that. Any way to do so?


回答1:


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)




回答2:


integer_breaks <- function(x)
  seq(floor(min(x)), ceiling(max(x)))

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=integer_breaks) +
  scale_x_discrete(drop = FALSE)


来源:https://stackoverflow.com/questions/23937193/modifying-ggplot2-y-axis-to-use-integers-without-enforcing-an-upper-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!