如何仅使用ggplot2在轴上显示整数值 [英] How to display only integer values on an axis using ggplot2

查看:730
本文介绍了如何仅使用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(真正的皇后,模拟个体
) =因子),值= c(15L,11L,29L,42L,0L,5L,21L,
22L),图例=结构(c(1L,1L,1L,1L,2L,2L,2L ,2L),.Label = c(真实皇后,
模拟个体),class =factor)),.Names = c(IR,
variable (数据2,aes(x =因子(IR),y = 0)),row.names = c(NA,-8L),class =data.frame)
p < - ggplot值,填充=图例,宽度= .15))


data3 <-structure(list(IR = structure(c(4L,3L,2L,1L,4L,3L,2L ,1L
),。标签= 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 (1L,2L,6L,10L,0L,1L,4L,
4L),图例=模拟个人
),等级=因子 1L,1L,1L,2L,2L,2L,2L),。标签= c(真正的皇后,
模拟个体),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))


## plot ##
q + geom_bar(position = 'dodge',color ='black')+ ylab('Frequency')+ xlab('IR')+ scale_fill_grey()+ theme(axis.text.x = element_text(color =black),axis.text。 (),panel.grid.minor = theme_blank(),panel.border = theme_blank(),panel.background = theme_blank(),axis.ticks.x = theme_blank())

我想要Y轴仅显示整数。这是通过四舍五入还是通过一种更优雅的方法来实现对我来说并不重要。

解决方案使用 scale_y_continuous()和参数 breaks = 您可以将y轴的断点设置为要显示的整数。

  ggplot(data2,aes(x = factor(IR),y = value,fill = Legend,width = .15))+ 
geom_bar(position ='dodge',color ='black')+
scale_y_continuous(breaks = c(1,3,7,10))


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.

解决方案

With scale_y_continuous() and argument breaks= you can set the breaking points for y axis to integers you want to display.

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

这篇关于如何仅使用ggplot2在轴上显示整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆