[英]Log10 Y-Axis starting from 0
I created a bar plot to show differences in water accumulation on different sites and layers.我创建了一个条形 plot 来显示不同地点和层的积水差异。 Because one value is way higher than the other ones I want to set the y-axis on log10 scale.因为一个值远高于其他值,所以我想将 y 轴设置为 log10 刻度。 It all works but the result looks rather unintuitive.这一切都有效,但结果看起来相当不直观。 is it possible to set the limit of the y-axis to 0 so the bar with the value 0.2 is not going downwards?是否可以将 y 轴的限制设置为 0,因此值为 0.2 的条不会向下?
Here is the code I used:这是我使用的代码:
p2 <- ggplot(data_summary2, aes(x= Site, y= small_mean, fill= Depth, Color= Depth))+
geom_bar(stat = "identity", position = "dodge", alpha=1)+
geom_errorbar(aes(ymin= small_mean - sd, ymax= small_mean + sd),
position = position_dodge(0.9),width=0.25, alpha= 0.6)+
scale_fill_brewer(palette = "Greens")+
geom_text(aes(label=small_mean),position=position_dodge(width=0.9), vjust=-0.25, hjust= -0.1, size= 3)+
#geom_text(aes(label= Tukey), position= position_dodge(0.9), size=3, vjust=-0.8, hjust= -0.5, color= "gray25")+
theme_bw()+
theme(legend.position = c(0.2, 0.9),legend.direction = "horizontal")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
labs(fill="Depth [cm]")+
theme(axis.text.x = element_text(angle = 25, size =9, vjust = 1, hjust=1))+
scale_x_discrete(labels= c( "Site 1\n(Hibiscus tillaceus)","Site 2 \n(Ceiba pentandra)","Site 3 \n(Clitoria fairchildiana)","Site 4 \n(Pachira aquatica)"))+
#theme(legend.position = c(0.85, 0.7))+
labs(x= "Sites\n(Type of Tree)", y= "µg Deep-Water/ g rhizosphere soil", title = "Average microbial Deep-water incorporation per Site", subtitle = "Changes over Time and Depth")+
facet_grid(.~Time, scale = "free")
p2 + scale_y_continuous(trans = "log10")
This is what the plot looks like:这是 plot 的样子:
On a log scale there is no 0
, therefore the only sensible place for bars to start from is y = log10(0)
or 1
.在对数刻度上没有0
,因此条形开始的唯一合理位置是y = log10(0)
或1
。
However you can create a pseudolog scale using scales::pseudo_log_trans
to get 0
included on the axis so all the bars go the same direciton.但是,您可以使用scales::pseudo_log_trans
创建一个伪对数刻度,以使轴上包含0
,因此所有条形 go 具有相同的方向。 I'm borrowing from this answer.我从这个答案中借用。 Compare the two plots below:比较下面的两个图:
library(tidyverse)
library(scales)
# make up data with distribution positive values above and below 1
d <- tibble(grp = LETTERS[1:5],
val = 10^(-2:2))
# normal plot with true log scale doesn't contain 0
d %>%
ggplot(aes(x = grp, y = val, fill = grp)) +
geom_col() +
ggtitle("On True Log Scale Bars Start at y = 1") +
scale_y_log10() # or if you prefer: scale_y_continuous(trans = "log10")
# set range of 'linear' portion of pseudolog scale
sigma <- min(d$val)
# plot on pseudolog to get all bars to extend to 0
d %>%
ggplot(aes(x = grp, y = val, fill = grp)) +
geom_col() +
ggtitle("On Pseudolog Scale Bars Start at y = 0") +
scale_y_continuous(
trans = pseudo_log_trans(base = 10, sigma = sigma),
breaks = 10^(-2:2)
)
Created on 2021-12-30 by the reprex package (v2.0.1)由reprex package (v2.0.1) 于 2021 年 12 月 30 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.