R语言 如何确定此双轴条形图中哪个y轴在左侧和右侧?

58wvjzkj  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(219)

我目前正试图绘制一些条形图,其中有两个值为每一组。由于价值是远离对方,我做了两个轴。
然而,我希望"累积RFU"的y轴在顶部,而不是"累积OD600",但到目前为止,它在底部。

My script looks like this for the plot:
ggplot(mean_sum_long, aes(x = Nitrogen_Source, y = value, fill = name)) +
  geom_col(position="dodge",alpha=0.8) +
  scale_y_continuous(
    name = "Accumulated RFU",
    sec.axis = sec_axis(~./coeff, name="Accumulated OD600"))+
  scale_fill_manual(values = c("green", "tomato"))+
  theme(axis.text.x = element_text(angle = 30, vjust = 0.5))+
  coord_flip()

系数为5000。
数据:

structure(list(Nitrogen_Source = structure(c(1L, 1L, 2L, 2L, 
3L, 3L), levels = c("Negative Control", "N-Phthaloyl-LGlutamic Acid", 
"D,L-α-AminoCaprylic Acid", "α-Amino-NValeric Acid", "L-Tryptophan", 
"Methylamine", "L-Cysteine", "Gly-Met", "Ethylenediamine", "Ala-His", 
"L-Tyrosine", "Ala-Leu", "Formamide", "D-Lysine", "D-Galactosamine", 
"Nitrate", "L-Histidine", "N-Acetyl-DMannosamine", "Ethylamine", 
"D-Mannosamine", "Histamine", "N-Acetyl-DGalactosamine", "Biuret", 
"D,L-Lactamide", "N-Acetyl-LGlutamic Acid", "Parabanic Acid", 
"Met-Ala", "L-Pyroglutamic Acid", "ß-Phenylethylamine", "Glucuronamide", 
"D-Glucosamine", "L-Glutamic Acid", "L-Glutamine", "Thymidine", 
"L-Isoleucine", "Xanthine", "Nitrite", "Ala-Asp", "Uridine", 
"ε-Amino-NCaproic Acid", "L-Citrulline", "D-Alanine", "Thymine", 
"L-Phenylalanine", "Ala-Glu", "Uric Acid", "γ-Amino-NButyric Acid", 
"Ammonia", "L-Aspartic Acid", "D-Aspartic Acid", "δ-Amino-NValeric Acid", 
"Ethanolamine", "L-Threonine", "Putrescine", "L-Methionine", 
"Allantoin", "L-Ornithine", "Ala-Thr", "Ala-Gln", "Cytosine", 
"Ala-Gly", "Agmatine", "L-Asparagine", "Inosine", "Guanosine", 
"Glycine", "N-Acetyl-DGlucosamine", "Adenosine", "Xanthosine", 
"L-Leucine", "Uracil", "L-Serine", "L-Homoserine", "Gly-Gln", 
"L-Proline", "L-Alanine", "Gly-Asn", "Urea", "Cytidine", "D-Glutamic Acid", 
"L-Arginine", "Gly-Glu", "Adenine"), class = "factor"), name = c("gfp_mean", 
"od_mean", "gfp_mean", "od_mean", "gfp_mean", "od_mean"), value = c(74851.5, 
41.29, 75282.5, 29.6650000000001, 76050, 58.755)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))
xdnvmnnf

xdnvmnnf1#

为了达到你想要的结果,你必须使用逆变换,变换你的gfp_mean值,当然切换次轴的变换以及名称。
注:至少对于所提供的示例数据,coeff为1250似乎更合适。

library(ggplot2)

coeff <- 1250

ggplot(mean_sum_long, aes(x = Nitrogen_Source, y = ifelse(name == "gfp_mean", value / coeff, value), fill = name)) +
  geom_col(position = "dodge", alpha = 0.8) +
  scale_y_continuous(
    name = "Accumulated OD600",
    sec.axis = sec_axis(~ . * coeff, name = "Accumulated RFU")
  ) +
  scale_fill_manual(values = c("green", "tomato")) +
  theme(axis.text.x.top = element_text(angle = 30, vjust = 0, hjust = 0)) +
  coord_flip()

w8f9ii69

w8f9ii692#

我还建议交换坐标轴,自从2020年更新到ggplot(我想是v2.2)后,你就可以这样做了。

YOUR_DATA |>
  dplyr:: mutate(val_adj = value / if_else(name == "gfp_mean", 2000, 1)) |>

  ggplot( aes(x = val_adj, y = Nitrogen_Source, fill = name)) +
  geom_col(position="dodge",alpha=0.8) +
  scale_x_continuous(
    name="Accumulated OD600",
    sec.axis = sec_axis(~.*2000, 
                        name = "Accumulated RFU",))+
  scale_fill_manual(values = c("green", "tomato"))+
  theme(axis.text.x = element_text(angle = 30, vjust = 0.5))

相关问题