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

uj5u.com熱心網友回復:
一種選擇是允許為每個方面單獨設定比例的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())
)
)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/485239.html
上一篇:R中的熱圖具有圖例和意義
