【问题标题】:Fine tuning vjust ggplot2微调 vjust ggplot2
【发布时间】:2021-08-20 00:38:26
【问题描述】:

我正在尝试制作与此类似的情节:

现在,我正在研究 IL-b1 图(因为它首先出现在数据集中),并将在所有图上应用设置。

我目前正在处理 x 轴标签,但无论我做得多么小,vjust 都会遇到问题。这是我当前代码的图像:

关于如何使标签更靠近 x 轴的任何建议?不管我把vjust 做得多么小,都不会再靠近了……

这是我当前的代码:

il1b <- ggplot(data = mouse) +
  geom_violin(aes(x = Treatment, y = `IL-1b_fold`)) +
  geom_jitter(aes(x = Treatment, y = `IL-1b_fold`)) +
  theme_classic() +
  ggtitle(label = "IL-1\u03b2") +
  ylab("fold mock control") +
  theme(plot.title = element_text(face = "bold"), 
        axis.title.x = element_text(face = "bold", size = 12),
        axis.title.y = element_text(face = "bold", size = 12),
        axis.text.x = element_text(face = "bold", angle = 45, size = 11, color = "black", vjust = -0.00001),
        axis.text.y = element_text(size = 11, color = "black")) 
  

        
setwd(output)
ggsave("il1b.png", il1b, width = 5, height = 5, units = "in")

output 是输出目录的存储文件路径。这一切都在一个 R Markdown 代码块中。

数据集中前 10 个 obs 的代表:

wrapr::build_frame(\n   \"GBM#\"  , \"Treatment\", \"IL-1b\", \"IL-6\"    , \"TNF-a\", \"IP-10\"  , \"IL-29\", \"IFN-a2\", \"IFN-b\", \"IL-10\", \"IFN-y\", \"IL-1b_fold\", \"IL-6_fold\", \"TNF-a_fold\", \"IP-10_fold\", \"IL-29_fold\", \"IFN-a2_fold\", \"IFN-b_fold\", \"IL-10_fold\", \"IFN-y_fold\" |\n     1     , \"Mock\"     , 484.4  , \"2290.62\" , 2055   , 951.4    , 433.4  , 567.8   , 400.4  , 2595   , 60.64  , 1           , 1          , 1           , 1           , 1           , 1            , 1           , 1           , 1            |\n     1     , \"PVSRIPO\"  , 383.6  , \"23233.55\", 1555   , 9865     , 298.8  , 546.6   , 240.9  , 4816   , 136.4  , 0.792       , 10.14      , 0.7564      , 10.37       , 0.6894      , 0.9628       , 0.6017      , 1.856       , 2.249        |\n     1     , \"Poly(I:C)\", 849.5  , \"37969.47\", 3451   , 1377     , 265    , 457.2   , 255.3  , 8435   , 53.69  , 1.754       , 16.58      , 1.679       , 1.448       , 0.6115      , 0.8052       , 0.6376      , 3.251       , 0.8854       |\n     1     , \"cGAMP\"    , 472.2  , \"12495.37\", 4002   , 3.078e+04, 304.4  , 741.4   , 324.7  , 3411   , 42.48  , 0.9748      , 5.455      , 1.947       , 32.35       , 0.7024      , 1.306        , 0.8109      , 1.315       , 0.7005       |\n     2     , \"Mock\"     , 23     , \"2851.31\" , 5.25   , 35.06    , 4.54   , 8.69    , 21.84  , 4.11   , 8.55   , 1           , 1          , 1           , 1           , 1           , 1            , 1           , 1           , 1            |\n     2     , \"PVSRIPO\"  , 11.57  , \"142.84\"  , 16.69  , 6116     , 13.57  , 23.46   , 21.84  , 5.63   , 5.14   , 0.503       , 0.0501     , 3.179       , 174.5       , 2.989       , 2.7          , 1           , 1.37        , 0.6012       |\n     2     , \"Poly(I:C)\", 28.41  , \"158.04\"  , 38.85  , 822.7    , 7.18   , 3.11    , 6.61   , 5.48   , 4.47   , 1.235       , 0.05543    , 7.4         , 23.47       , 1.581       , 0.3579       , 0.3027      , 1.333       , 0.5228       |\n     2     , \"cGAMP\"    , 71.07  , \"1166.8\"  , 162.7  , 1794     , 8.08   , 7.72    , 22.55  , 25.62  , 9.94   , 3.09        , 0.4092     , 30.98       , 51.18       , 1.78        , 0.8884       , 1.033       , 6.234       , 1.163        |\n     4     , \"Mock\"     , 8.67   , \"1148.81\" , 203.6  , 1.86     , 1.88   , 35.52   , 39.1   , 8.21   , 74.05  , 1           , 1          , 1           , 1           , 1           , 1            , 1           , 1           , 1            |\n     4     , \"PVSRIPO\"  , 88.52  , \"1997.02\" , 551    , 211.4    , 1.88   , 35.52   , 39.1   , 4.71   , 74.05  , 10.21       , 1.738      , 2.706       , 113.6       , 1           , 1            , 1           , 0.5737      , 1            )\n

【问题讨论】:

  • 与普遍看法相反,hjustvjust 参数用于水平和垂直对齐。它们用于文本方向和正交方向对齐。当且仅当文本角度为 0 时,它们才与水平/垂直对齐方式一致。通过设置 hjust = 1,您可能会获得更好的成功。

标签: r ggplot2


【解决方案1】:

非常感谢@Daniel Molitor 和@teunbrand!我对vjust 的功能有一个普遍的误解。以下设置获得了所需的输出:vjust = 0.65

这是修改后的输出代码,附后:

il1b <- ggplot(data = mouse) +
  geom_violin(aes(x = Treatment, y = `IL-1b_fold`)) +
  geom_jitter(aes(x = Treatment, y = `IL-1b_fold`)) +
  theme_classic() +
  ggtitle(label = "IL-1\u03b2") +
  ylab("fold mock control") +
  theme(plot.title = element_text(face = "bold"), 
        axis.title.x = element_text(face = "bold", size = 12),
        axis.title.y = element_text(face = "bold", size = 12),
        axis.text.x = element_text(face = "bold", angle = 45, size = 11, color = "black", vjust = 0.65),
        axis.text.y = element_text(size = 11, color = "black")) 
  

        
setwd(output)
ggsave("il1b.png", il1b, width = 5, height = 5, units = "in")

修改后的输出:

【讨论】:

    猜你喜欢
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 2021-11-04
    • 2021-12-01
    相关资源
    最近更新 更多