101

我有以下情节:

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())

我希望 y 轴只显示整数。这是通过四舍五入还是通过更优雅的方法完成对我来说并不重要。

4

10 回答 10

96

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())
于 2014-12-16T00:57:08.483 回答
61

这就是我使用的:

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)))))
于 2016-10-05T14:41:48.670 回答
46

使用scale_y_continuous()and 参数breaks=,您可以将 y 轴的断点设置为要显示的整数。

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))
于 2013-03-25T18:39:36.077 回答
23

您可以使用自定义贴标机。例如,这个函数保证只产生整数中断:

int_breaks <- function(x, n = 5) {
  l <- pretty(x, n)
  l[abs(l %% 1) < .Machine$double.eps ^ 0.5] 
}

用于

+ scale_y_continuous(breaks = int_breaks)

它通过采用默认中断来工作,并且只保留那些整数。如果它显示的数据中断太少,请增加n,例如:

+ scale_y_continuous(breaks = function(x) int_breaks(x, n = 10))
于 2017-07-03T13:37:10.677 回答
19

这些解决方案对我不起作用,也没有解释解决方案。

函数的breaks参数scale_*_continuous可以与将限制作为输入并返回中断作为输出的自定义函数一起使用。默认情况下,对于连续数据(相对于数据范围),轴范围将在每侧扩展 5%。由于这种扩展,轴限制可能不是整数值。

我正在寻找的解决方案是简单地将下限四舍五入到最接近的整数,将上限四舍五入到最接近的整数,然后在这些端点之间的整数值处中断。因此,我使用了休息功能:

brk <- function(x) seq(ceiling(x[1]), floor(x[2]), by = 1)

所需的代码片段是:

scale_y_continuous(breaks = function(x) seq(ceiling(x[1]), floor(x[2]), by = 1))

原始问题的可重现示例是:

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"
      )
    ),
    row.names = c(NA,-8L),
    class = "data.frame"
  )

ggplot(data3, aes(
  x = factor(IR),
  y = value,
  fill = Legend,
  width = .15
)) +
  geom_col(position = 'dodge', colour = 'black') + ylab('Frequency') + xlab('IR') +
  scale_fill_grey() +
  scale_y_continuous(
    breaks = function(x) seq(ceiling(x[1]), floor(x[2]), by = 1),
    expand = expand_scale(mult = c(0, 0.05))
    ) +
  theme(axis.text.x=element_text(colour="black", angle = 45, hjust = 1), 
        axis.text.y=element_text(colour="Black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(), 
        axis.ticks.x = element_blank())
于 2019-02-13T20:27:02.460 回答
7

您可以为此使用or的accuracy参数:scales::label_number()scales::label_comma()

fakedata <- data.frame(
  x = 1:5,
  y = c(0.1, 1.2, 2.4, 2.9, 2.2)
)

library(ggplot2)

# without the accuracy argument, you see .0 decimals
ggplot(fakedata, aes(x = x, y = y)) +
  geom_point() +
  scale_y_continuous(label = scales::comma)

# with the accuracy argument, all displayed numbers are integers
ggplot(fakedata, aes(x = x, y = y)) +
  geom_point() +
  scale_y_continuous(label = ~ scales::comma(.x, accuracy = 1))

# equivalent
ggplot(fakedata, aes(x = x, y = y)) +
  geom_point() +
  scale_y_continuous(label = scales::label_comma(accuracy = 1))

# this works with scales::label_number() as well
ggplot(fakedata, aes(x = x, y = y)) +
  geom_point() +
  scale_y_continuous(label = scales::label_number(accuracy = 1))

reprex 包于 2021-08-27 创建(v2.0.0.9000)

于 2021-08-27T11:15:54.327 回答
6

我从 Joshua Cook 那里找到了这个解决方案,并且效果很好。

integer_breaks <- function(n = 5, ...) {
fxn <- function(x) {
breaks <- floor(pretty(x, n, ...))
names(breaks) <- attr(breaks, "labels")
breaks
}
return(fxn)
}

q + geom_bar(position='dodge', colour='black') + 
scale_y_continuous(breaks = integer_breaks())

来源是: https ://joshuacook.netlify.app/post/integer-values-ggplot-axis/

于 2020-06-11T09:25:51.187 回答
5

所有现有的答案似乎都需要自定义功能或在某些情况下失败。

此行进行整数中断:

bad_scale_plot +
  scale_y_continuous(breaks = scales::breaks_extended(Q = c(1, 5, 2, 4, 3)))

有关更多信息,请参阅文档?labeling::extended(这是一个由 调用的函数scales::breaks_extended)。

基本上,参数Q是一组很好的数字,算法试图将其用于比例中断。原始图产生非整数中断(0、2.5、5 和 7.5),因为 的默认值Q包括 2.5: Q = c(1,5,2,2.5,4,3)

编辑:正如评论中指出的那样,当 y 轴的范围较小时,可能会发生非整数中断。默认情况下,breaks_extended()尝试进行n = 5中断,当范围太小时,这是不可能的。快速测试表明,大于 0 < y < 2.5 的范围会给出整数中断(n也可以手动减少)。

于 2020-05-20T16:23:38.637 回答
4

谷歌让我想到了这个问题。我正在尝试以任意比例使用实数。y 刻度数字以百万为单位。

scalescomma方法在我的大数中引入了一个逗号R-Bloggers上的这篇文章解释了一种使用该方法的简单comma方法:

library(scales)

big_numbers <- data.frame(x = 1:5, y = c(1000000:1000004))

big_numbers_plot <- ggplot(big_numbers, aes(x = x, y = y))+
geom_point()

big_numbers_plot + scale_y_continuous(labels = comma)

享受R :)

于 2019-09-13T11:22:44.033 回答
4

这个答案建立在@Axeman's answer to address the comment by kory that if the data only go from 0 to 1, no break is shown at 1. 这似乎是因为pretty输出不准确,似乎是 1 与1(见最后的例子)。

因此,如果您使用

int_breaks_rounded <- function(x, n = 5)  pretty(x, n)[round(pretty(x, n),1) %% 1 == 0]

+ scale_y_continuous(breaks = int_breaks_rounded)

0 和 1 都显示为中断。

示例说明与 Axeman 的区别

testdata <- data.frame(x = 1:5, y = c(0,1,0,1,1))

p1 <- ggplot(testdata, aes(x = x, y = y))+
  geom_point()


p1 + scale_y_continuous(breaks = int_breaks)
p1 + scale_y_continuous(breaks =  int_breaks_rounded)

两者都将使用初始问题中提供的数据。

说明为什么需要舍入

pretty(c(0,1.05),5)
#> [1] 0.0 0.2 0.4 0.6 0.8 1.0 1.2
identical(pretty(c(0,1.05),5)[6],1)
#> [1] FALSE
于 2019-07-18T02:24:45.803 回答