ggplot同步y轴刻度
ggplot to sync y-axis scale
我想使用 gridExtra
在相同比例的 y 轴上生成两个直方图,这是我的尝试:
library(rvest)
library(tidyverse)
library(gridExtra)
dat_url <- 'https://gender-pay-gap.service.gov.uk/viewing/download-data/2019'
dat <- read_csv(dat_url)
p1 <- dat %>%
filter(between(DiffMedianHourlyPercent,-100,100)) %>%
ggplot(aes(x = DiffMedianHourlyPercent)) +
geom_histogram(aes(fill = DiffMedianHourlyPercent<=0),
breaks=seq(-100,100, by=2)) +
guides(fill=FALSE)
p1 <- p1 + scale_fill_brewer(palette="Set2") +
theme_bw() +
theme(legend.position="none")
p2 <- dat %>%
filter(between(DiffMeanHourlyPercent,-100,100)) %>%
ggplot(aes(x = DiffMeanHourlyPercent)) +
geom_histogram(aes(fill = DiffMeanHourlyPercent<=0),
breaks=seq(-100,100, by=2)) +
guides(fill=FALSE)
p2 <- p2 + scale_fill_brewer(palette="Set2") +
theme_bw() +
theme(legend.position="none")
p2
grid.arrange(p1, p2, nrow = 1)
从图中可以看出 y 轴的比例不同...
我建议您尝试 facet_wrap()
方法。您的代码正在创建额外的数据以创建不同的图。您可以设置最佳管道,以便根据您想要的变量重塑数据,并使用常见的 y-axis 获得所需的图。这里的代码:
library(rvest)
library(tidyverse)
library(gridExtra)
#Data
dat_url <- 'https://gender-pay-gap.service.gov.uk/viewing/download-data/2019'
dat <- read_csv(dat_url)
#Plot
G1 <- dat %>%
select(c(DiffMedianHourlyPercent,DiffMeanHourlyPercent)) %>%
pivot_longer(everything()) %>%
#Filter
group_by(name) %>%
filter(between(value,-100,100)) %>%
ggplot(aes(x = value)) +
geom_histogram(aes(fill = value<=0),
breaks=seq(-100,100, by=2)) +
guides(fill=FALSE)+
scale_fill_brewer(palette="Set2") +
theme_bw() +
theme(legend.position="none")+
facet_wrap(.~name,scales = 'free_x')
输出:
我想使用 gridExtra
在相同比例的 y 轴上生成两个直方图,这是我的尝试:
library(rvest)
library(tidyverse)
library(gridExtra)
dat_url <- 'https://gender-pay-gap.service.gov.uk/viewing/download-data/2019'
dat <- read_csv(dat_url)
p1 <- dat %>%
filter(between(DiffMedianHourlyPercent,-100,100)) %>%
ggplot(aes(x = DiffMedianHourlyPercent)) +
geom_histogram(aes(fill = DiffMedianHourlyPercent<=0),
breaks=seq(-100,100, by=2)) +
guides(fill=FALSE)
p1 <- p1 + scale_fill_brewer(palette="Set2") +
theme_bw() +
theme(legend.position="none")
p2 <- dat %>%
filter(between(DiffMeanHourlyPercent,-100,100)) %>%
ggplot(aes(x = DiffMeanHourlyPercent)) +
geom_histogram(aes(fill = DiffMeanHourlyPercent<=0),
breaks=seq(-100,100, by=2)) +
guides(fill=FALSE)
p2 <- p2 + scale_fill_brewer(palette="Set2") +
theme_bw() +
theme(legend.position="none")
p2
grid.arrange(p1, p2, nrow = 1)
从图中可以看出 y 轴的比例不同...
我建议您尝试 facet_wrap()
方法。您的代码正在创建额外的数据以创建不同的图。您可以设置最佳管道,以便根据您想要的变量重塑数据,并使用常见的 y-axis 获得所需的图。这里的代码:
library(rvest)
library(tidyverse)
library(gridExtra)
#Data
dat_url <- 'https://gender-pay-gap.service.gov.uk/viewing/download-data/2019'
dat <- read_csv(dat_url)
#Plot
G1 <- dat %>%
select(c(DiffMedianHourlyPercent,DiffMeanHourlyPercent)) %>%
pivot_longer(everything()) %>%
#Filter
group_by(name) %>%
filter(between(value,-100,100)) %>%
ggplot(aes(x = value)) +
geom_histogram(aes(fill = value<=0),
breaks=seq(-100,100, by=2)) +
guides(fill=FALSE)+
scale_fill_brewer(palette="Set2") +
theme_bw() +
theme(legend.position="none")+
facet_wrap(.~name,scales = 'free_x')
输出: