当需要在原生中进行对数转换时,绘制一个次要的Y轴。

问题描述 投票:1回答:1

我试图绘制一个两轴线图(见图)。我尝试了许多方法,从这里的例子,但没有工作。左轴必须是对数10,而二次(右)正常。此外,我确实需要设置它们的限制。所有的数据都来自一个单一的数据集(dfwc),我采取了两个子集(这里绘制为 "aa "和 "bb",Y-左和Y-右,分别)。最后,我想按株数来显示图例颜色(如左图所示)。

  aa <- ggplot(dfwc, aes(x=time, y=numbers, color=strain, group=strain)) + theme_dp1() + 
              geom_point(aes(color=strain, group=strain), subset(dfwc,strain %in% c("mc", "mut1", "mut2", "mut3")), alpha=.5, size=2) +
              geom_errorbar(aes(ymax=numbers-sd, ymin=numbers+sd), subset(dfwc,strain %in% c("mc", "mut1", "mut2", "mut3")), size=.4, width=.1, show.legend=FALSE) +
              geom_line(aes(color=strain), subset(dfwc,strain %in% c("mc", "mut1", "mut2", "mut3")), size=.6, linetype="dashed") +
            scale_y_log10(breaks=c(0.01, 0.1, 1, 10),
                    labels=scales::trans_format("log10", scales::math_format(10^.x))) +
            annotation_logticks(colour="#8d96a3", sides='l', size = .5, short=unit(1,"mm"), mid=unit(2,"mm"), long=unit(2.5,"mm")) + 
            expand_limits(y=c(0.05,8)) +  #set Y1 limits
            scale_color_manual(name = "Strain", values=colorsg, labels=c("WT", "RsmB", "RsmB:IGR", "RsmB,vfmE"))+
           labs(x=bold("Time")~(hours), y=bold("Bacterial growth")~(OD[600]))  # axes labels
    aa
    #second subset
  bb <- ggplot(dfwc, aes(x=time, y=numbers, color=strain, group=strain)) + theme_dp1() + 
            geom_point(aes(), subset(dfwc,strain %in% c("vmc", "vmut1", "vmut2", "vmut3")), alpha=.4, size=1.5, show.legend=FALSE) +
            geom_errorbar(aes(ymax=numbers-sd, ymin=numbers+sd), subset(dfwc,strain %in% c("vmc", "vmut1", "vmut2", "vmut3")), size=.4, width=.05, show.legend=FALSE) +
            geom_line(aes(), subset(dfwc,strain %in% c("vmc", "vmut1", "vmut2", "vmut3")), size=.6, linetype=1, show.legend=FALSE) +
            scale_y_continuous(sec.axis = sec_axis(~.*1, name = "This is my Right Axis"))
    bb}

我想把这2张图放在1张图中。

enter image description here

数据转换(原始数据在此。https:/docs.google.comspreadsheetsd1zuNzjTjf_0MRyRoTCEfiP1WqRl3viWHueNXepiA7m9oedit#gid=0。)

data_long <- gather(curve, time, numbers, X0:X12, factor_key=TRUE) #change according to hours numbers
data_long
plot(data_long)
names(data_long)
str(data_long)  #To view variable structure

# Rename factor names (time) from "h0" to "h12" to "0" to "14"  #useful! 
levels(data_long$time)[levels(data_long$time)=="X0"] <- "0"
levels(data_long$time)[levels(data_long$time)=="X2"] <- "2"
levels(data_long$time)[levels(data_long$time)=="X4"] <- "4"
levels(data_long$time)[levels(data_long$time)=="X6"] <- "6"
levels(data_long$time)[levels(data_long$time)=="X8"] <- "8"
levels(data_long$time)[levels(data_long$time)=="X10"] <- "10"
levels(data_long$time)[levels(data_long$time)=="X12"] <- "12"
levels(data_long$time)[levels(data_long$time)=="X14"] <- "14"

#Statistics ! 
dfwc<-summarySE(data_long, measurevar="numbers", groupvars=c("strain", "time"))
dfwc  
r scale axes multiple-axes
1个回答
0
投票

我不能帮助你添加一个次要的对数轴在左边,而有一个正常的轴在右边。通常情况下,你会把它反过来。另外,在这种情况下,日志轴在很多数值低于1的情况下也不能很好地工作,请看这个例子。

library(tidyverse)

del <- read.csv("~/Path/del.csv", stringsAsFactors=FALSE)

del <- del %>%  
  pivot_longer(3:9,names_to = "time") %>% 
  mutate(time =  stringr::str_replace_all(time, pattern = 'X',replacement = 'T'),
         time = fct_relevel(time,'T0','T2','T4','T6','T8','T10','T12'))

del <- del %>% 
  mutate(subject = as.factor(subject),
         strain = as.factor(strain),
         time = as.factor(time))

del %>% 
  filter(!is.na(value)) %>%  
  ggplot(aes(x=time, y=value, color=strain, group=strain)) + 
  geom_point(alpha=.5, size=2) +
  geom_line() +
  scale_y_continuous(sec.axis = sec_axis(~log10(.)))

enter image description here

注:你提供数据是很好的! 请注意 最小 部分。最有效的问题针对某一个问题,省略每一个不必要的细节。另外,其最好的 可复制的例子 是独立的。在这里,您使用的数据是您没有在 ggplot 例如,代码。参见 再制

© www.soinside.com 2019 - 2024. All rights reserved.