2

我只想将y- axis1 DATA(左轴,Var1,虚线)绘制为 log10 比例。因此,虚线在 y 轴上看起来更高,并且 1 和 2 之间的差异会很明显。

我尝试了几件事,但不起作用(我相信我以错误的顺序/位置使用它们),例如:

+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-axis2 (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"))

在此处输入图像描述

4

3 回答 3

2

我认为最简单的方法是将主轴设为线性轴,但将其放在图的右侧。然后,您可以将第二个作为您的对数转换轴。

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 包于 2022-02-09 创建(v2.0.1)

于 2022-02-09T14:07:49.497 回答
1

这是一个自定义中断函数:

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"))

结果不是那么漂亮,但您可以根据需要自定义休息时间。

阴谋

于 2022-02-09T13:25:59.773 回答
0

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

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"))
于 2022-02-09T14:03:51.580 回答