在geom_bar ggplot2中操作scale_y_log [英] 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的网站.
which gives as results the following figure, which is quite helpful for my data analysis since I want to highlight sites with values above 1.
但是,在另一个假设下,我需要使用facet_grid突出显示位于1和0.1上方的站点,最后得到类似这样的内容(我将此图编辑为期望输出):
However, under another assumption, I need to highlight sites above 1 and 0.1 using facet_grid, ending up with something like this (I edited this figure as desire output):
您知道scale_y_log10中的任何选项以便将第二个数字显示在facet_grid下吗?
Do you know any option in scale_y_log10 in order to get the second figure under facet_grid?
推荐答案
一种选择是将条形图重新参数化为矩形,然后绘制它们.
One option is to reparameterise the bars as rectangles and plot that instead.
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()
由 reprex软件包(v1.0.0)创建于2021-02-26 sup>
Created on 2021-02-26 by the reprex package (v1.0.0)
这篇关于在geom_bar ggplot2中操作scale_y_log的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!