如何使用ggplot2仅在轴上显示整数值

时间:2013-03-25 18:23:30

标签: r ggplot2

我有以下情节:

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轴只显示整数。无论这是通过四舍五入还是通过更优雅的方法来实现,对我来说并不重要。

9 个答案:

答案 0 :(得分:55)

如果您拥有scales包,则可以使用pretty_breaks()而无需手动指定中断。

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

答案 1 :(得分:32)

这就是我使用的:

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

答案 2 :(得分:31)

使用scale_y_continuous()和参数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))

答案 3 :(得分:8)

您可以使用自定义贴标机。例如,此函数保证仅生成整数中断:

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

用作

+ scale_y_continuous(breaks = int_breaks)

答案 4 :(得分: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 n也可以手动减小)。

答案 5 :(得分:2)

我从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/

答案 6 :(得分:1)

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

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

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

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

答案 7 :(得分:1)

Google提出了我这个问题。我正在尝试以y比例使用实数。 y刻度数字以百万为单位。

scalesUIBarButtonItem方法为我的大量用户引入了逗号。这篇关于R-Bloggers的帖子解释了一种使用comma方法的简单方法:

comma

享受 R :)

答案 8 :(得分:0)

此答案基于@Axeman的答案,以解决kory的评论,即如果数据仅从0到1,则不会在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