如何在 R 中的 ggplot 中使 log10 仅成为第一个 y 轴(而不是第二个 y 轴)

How to make log10 ONLY first y-axis (not secondary y-axis) in ggplot in R

我想将 ONLY y 轴 1 数据(左轴,Var1,虚线)绘制为 log10 刻度。因此,虚线在 y 轴上看起来更高,1 和 2 之间的差异会很明显。

我尝试了几种方法,但都不起作用(我相信我用错了 order/place)例如:

+coord_trans(y='log10')--> 空地块

scale_y_continuous(trans = log10_trans(),... --> 使 Var1 和 Var 2 都为 log10

scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),labels = trans_format("log10", math_format(10^.x)))--> 使 y 轴均为 log10 并删除 y 轴 2 (Var2)

data<- data.frame(
      Day=c(1,2,3,1,2,3,1,2,3),
      Name=rep(c(rep("a",3),rep("b",3),rep("c",3))),
      Var1=c(1090,484,64010,1090,484,64010,1090,484,64010),
      Var2= c(4,16,39,2,22,39,41,10,3))


ggplot(data)  + 
  geom_bar(aes(fill=Name, y=Var2*1000, x=Day),stat="identity", colour="black", position= position_stack(reverse = TRUE))+
  geom_line(aes(x=Day, y=Var1),stat="identity",color="black", linetype="dotted", size=0.8)+
  geom_point(aes(Day, Var1), shape=8)+
  labs(title= "",
       x="",y=expression('Var1'))+
  scale_y_continuous(
    sec.axis=sec_axis(~./1000, name= expression(paste("Var2"))))+
  theme_classic()+
  scale_fill_grey(start = 1, end=0.1,name = "", labels = c("a", "b", "c"))

这是一个自定义休息功能:

br <- function(limits) {
  10^(seq(ifelse(limits[1] <= 0,
                 0,
                 trunc(log10(limits[1]))),
          trunc(log10(limits[2])),
          by = 1))}
ggplot(data)  + 
  geom_bar(aes(fill = Name, y = Var2 * 1000, x = Day),
           stat = "identity",
           colour = "black",
           position = position_stack(reverse = TRUE))+
  geom_line(aes(x=Day, y=Var1),
            stat = "identity",
            color = "black",
            linetype = "dotted",
            size = 0.8)+
  geom_point(aes(Day, Var1),
             shape = 8)+
  labs(title = "",
       x = "",
       y = expression('Var1'))+
  scale_y_continuous(
    breaks = br,
    sec.axis = sec_axis(~./1000, name= expression(paste("Var2"))))+
  theme_classic()+
  scale_fill_grey(start = 1,
                  end = 0.1,
                  name = "",
                  labels = c("a", "b", "c"))

结果不是很好,但您可以根据需要自定义休息时间。

你绝对应该阅读@teunbrand 在你的问题的评论中链接到的答案。但是对于左边显示log值,右边显示原始值的问题,可以使用:

tibble(Day = 1:10,
       Val1 =10*Day) %>% 
    ggplot(aes(x = Day, y = log10(Val1))) +
    geom_col() +
    scale_y_log10(name = "log(Val1)",
                  sec.axis = sec_axis(~ 10^., name = "Val1"))

我认为最简单的方法是让主轴为线性轴,但将其放在绘图的右侧。然后,您可以让次要轴成为您的 log-transformed 轴。

library(ggplot2)

data<- data.frame(
  Day=c(1,2,3,1,2,3,1,2,3),
  Name=rep(c(rep("a",3),rep("b",3),rep("c",3))),
  Var1=c(1090,484,64010,1090,484,64010,1090,484,64010),
  Var2= c(4,16,39,2,22,39,41,10,3))

# Max of secondary divided by max of primary
upper <- log10(3e6) / 80

breakfun <- function(x) {
  10^scales::extended_breaks()(log10(x))
}

ggplot(data)  + 
  geom_bar(aes(fill=Name, y=Var2, x=Day),
           stat="identity", colour="black", position= position_stack(reverse = TRUE))+
  geom_line(aes(x=Day, y=log10(Var1) / upper),
            stat="identity",color="black", linetype="dotted", size=0.8)+
  geom_point(aes(Day, log10(Var1) / upper), shape=8)+
  labs(title= "",
       x="",y=expression('Var1'))+
  scale_y_continuous(
    position = "right",
    name = "Var2",
    sec.axis = sec_axis(~10^ (. * upper), name= expression(paste("Var1")),
                        breaks = breakfun)
  )+
  theme_classic() +
  scale_fill_grey(start = 1, end=0.1,name = "", labels = c("a", "b", "c"))

reprex package (v2.0.1)

于 2022-02-09 创建