在ggplot/facet_wrap()中,如何让Y轴有不同的格式
In ggplot/facet_wrap(), how to marke axis Y have different format
在ggplot/facet_wrap()中,如何让Y轴有不同的格式?谢谢!
library(tidyverse)
test_data <- diamonds
plot_data <- test_data %>% mutate(x_to_price=x/price,
price=price*1000) %>% head(12) %>%
mutate(mseq=1:NROW(.)) %>%
select(price,x_to_price,mseq) %>% gather(key='type',value='amount',- mseq)
plot_data %>% ggplot(aes(x=mseq,y=amount))+geom_line()+geom_point()+
facet_wrap(.~type,scales='free_y')
一个选项是 ggh4x
包,它通过 facetted_pos_scales
允许为每个方面单独设置比例:
library(ggplot2)
library(ggh4x)
ggplot(plot_data, aes(x=mseq,y=amount))+geom_line()+geom_point()+
facet_wrap(.~type,scales='free_y') +
facetted_pos_scales(
y = list(
type == "price" ~ scale_y_continuous(labels = scales::comma_format()),
type == "x_to_price" ~ scale_y_continuous(labels = scales::percent_format())
)
)
在ggplot/facet_wrap()中,如何让Y轴有不同的格式?谢谢!
library(tidyverse)
test_data <- diamonds
plot_data <- test_data %>% mutate(x_to_price=x/price,
price=price*1000) %>% head(12) %>%
mutate(mseq=1:NROW(.)) %>%
select(price,x_to_price,mseq) %>% gather(key='type',value='amount',- mseq)
plot_data %>% ggplot(aes(x=mseq,y=amount))+geom_line()+geom_point()+
facet_wrap(.~type,scales='free_y')
一个选项是 ggh4x
包,它通过 facetted_pos_scales
允许为每个方面单独设置比例:
library(ggplot2)
library(ggh4x)
ggplot(plot_data, aes(x=mseq,y=amount))+geom_line()+geom_point()+
facet_wrap(.~type,scales='free_y') +
facetted_pos_scales(
y = list(
type == "price" ~ scale_y_continuous(labels = scales::comma_format()),
type == "x_to_price" ~ scale_y_continuous(labels = scales::percent_format())
)
)