다음 플롯이 있습니다.
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) {
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))
답변
이 솔루션은 저에게 효과적이지 않았고 솔루션을 설명하지 않았습니다.
함수에 대한 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())
답변
Google은 저에게이 질문을 가져 왔습니다. 저는 실제 숫자 를 y 척도 로 사용하려고합니다 . y 스케일 숫자는 백만 단위입니다.
저울 패키지 comma
방법은 소개 쉼표 내 많은 수를. 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을 즐기십시오 🙂
답변
기존의 모든 답변에는 사용자 지정 기능이 필요하거나 경우에 따라 실패하는 것 같습니다.
이 줄은 정수 나누기를 만듭니다.
bad_scale_plot +
scale_y_continuous(breaks = scales::breaks_extended(Q = c(1, 5, 2, 4, 3)))
자세한 내용은 설명서 ?labeling::extended
(에서 호출하는 함수 scales::breaks_extended
)를 참조하세요.
기본적으로 인수 Q
는 알고리즘이 스케일 브레이크에 사용하려고하는 멋진 숫자 세트입니다. 의 기본값에 Q
2.5 가 포함 되므로 원래 플롯은 정수가 아닌 구분 (0, 2.5, 5 및 7.5)을 생성합니다 Q = c(1,5,2,2.5,4,3)
.
편집 : 주석에서 지적했듯이 y 축의 범위가 작을 때 정수가 아닌 구분이 발생할 수 있습니다. 기본적으로 범위가 너무 작 으면 불가능한 휴식 시간에 breaks_extended()
대해 시도합니다 n = 5
. 빠른 테스트에 따르면 0 <y <2.5보다 넓은 범위는 정수 중단을 제공합니다 ( n
수동으로 줄일 수도 있음).
