[英]manipulate scale_y_log in geom_bar ggplot2
我有以下 df 作為示例:
sites <- c('s1','s1','s2', "s2", "s3", "s3")
conc <- c(15, 12, 0.5, 0.05, 3, 0.005)
trop <- c("pp", "pt")
df <- data.frame(sites, conc, trop)
df$trop<- factor(df$trop, levels = c("pp", "pt"))
ggplot(df, aes(x= sites, y= conc))+
geom_bar(stat = "identity", colour="black")+
scale_y_log10()+
facet_grid(.~trop)+
theme_bw()
結果如下圖,這對我的數據分析非常有幫助,因為我想突出顯示值高於 1 的站點。
但是,在另一個假設下,我需要使用 facet_grid 突出顯示 1 和 0.1 以上的站點,最終得到類似這樣的結果(我將此圖編輯為期望輸出):
您是否知道 scale_y_log10 中的任何選項以獲取 facet_grid 下的第二個數字?
一種選擇是將條形重新參數化為矩形,並改為 plot。
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.3
sites <- c('s1','s1','s2', "s2", "s3", "s3")
conc <- c(15, 12, 0.5, 0.05, 3, 0.005)
trop <- c("pp", "pt")
df <- data.frame(sites, conc, trop)
df$trop<- factor(df$trop, levels = c("pp", "pt"))
char2num <- function(x){match(x, sort(unique(x)))}
ggplot(df) +
geom_rect(
aes(
xmin = char2num(sites) - 0.4,
xmax = char2num(sites) + 0.4,
ymin = ifelse(trop == "pt", 0.1, 1),
ymax = conc
),
colour = 'black'
) +
scale_y_log10() +
# Fake discrete axis
scale_x_continuous(labels = sort(unique(df$sites)),
breaks = 1:3) +
facet_grid(. ~ trop) +
theme_bw()
由代表 package (v1.0.0) 於 2021 年 2 月 26 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.