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 축에 정수만 표시하고 싶습니다. 이것이 반올림을 통해 수행되는지 또는 더 우아한 방법을 통해 수행되는지 여부는 나에게별로 중요하지 않습니다.
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))
scales
패키지 가 있으면 pretty_breaks()
수동으로 구분을 지정하지 않고도 사용할 수 있습니다 .
q + geom_bar(position='dodge', colour='black') +
scale_y_continuous(breaks= pretty_breaks())
이것이 내가 사용하는 것입니다.
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)))))
사용자 지정 레이 블러를 사용할 수 있습니다. 예를 들어이 함수는 정수 나누기 만 생성하도록 보장합니다.
int_breaks <- function(x, n = 5) pretty(x, n)[pretty(x, n) %% 1 == 0]
로 사용
+ scale_y_continuous(breaks = int_breaks)
이 솔루션은 저에게 효과가 없었으며 솔루션을 설명하지 않았습니다.
함수에 대한 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())
This answer builds on @Axeman's answer to address the comment by kory that if the data only goes from 0 to 1, no break is shown at 1. This seems to be because of inaccuracy in pretty
with outputs which appear to be 1 not being identical to 1 (see example at the end).
Therefore if you use
int_breaks_rounded <- function(x, n = 5) pretty(x, n)[round(pretty(x, n),1) %% 1 == 0]
with
+ scale_y_continuous(breaks = int_breaks_rounded)
both 0 and 1 are shown as breaks.
Example to illustrate difference from Axeman's
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)
Both will work with the data provided in the initial question.
Illustration of why rounding is required
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
Google brought me to this question. I'm trying to use real numbers in a y scale. The y scale numbers are in Millions.
The scales package comma
method introduces a comma to my large numbers. This post on R-Bloggers explains a simple approach using the comma
method:
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)
Enjoy R :)
'programing tip' 카테고리의 다른 글
jQuery : 부모, 부모 ID를 얻습니까? (0) | 2020.11.17 |
---|---|
엄격 모드가 적용되었는지 확인할 수있는 방법이 있습니까? (0) | 2020.11.17 |
.net 코어에서 유효하지 않은 SSL 인증서 우회 (0) | 2020.11.17 |
여러 열에서 최소값을 선택하는 가장 좋은 방법은 무엇입니까? (0) | 2020.11.17 |
MySQL Workbench에서 전체 데이터베이스 스크립트를 생성하는 방법은 무엇입니까? (0) | 2020.11.17 |