加粗标题和轴刻度

Posted

技术标签:

【中文标题】加粗标题和轴刻度【英文标题】:Bolden the title and axis-ticks 【发布时间】:2021-12-23 13:57:27 【问题描述】:

所以我得到了帮助来更改图表上不同方面的比例。建议使用下面的代码,但现在我无法将标签和轴刻度的字体设置为粗体。在我使用的旧代码中

theme(
     axis.text.x = element_text(size = 12,face="bold"),#, angle = 10, hjust = .5, vjust = .5),axis.text.y = element_text(size = 12, face = "bold"),
     axis.title.y = element_text(vjust= 1.8, size = 16),
     axis.title.x = element_text(vjust= -0.5, size = 16),
     axis.title = element_text(face = "bold"))

但它不起作用。

tgc <- structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("Visible", "Remembered"), class = "factor"), 
    Condition = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
    3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
    ), .Label = c("CEN", "IPS", "CTL"), class = "factor"), test = structure(c(1L, 
    1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 
    1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Pre-test", "Post-test"
    ), class = "factor"), Session = structure(c(1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L), .Label = c("Adaptation", "Post-adaptation"
    ), class = "factor"), N = c(12, 12, 12, 12, 12, 12, 12, 12, 
    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
    12), EE = c(2.134379625, 0.333942625, 1.742841125, 0.317361916666667, 
    2.84197270833333, 0.307057416666667, 2.403112375, 0.281202, 
    3.49590529166667, 0.305657666666667, 2.85211466666667, 0.3131155, 
    1.44857545833333, 0.269328166666667, 1.740270875, 0.243361833333333, 
    2.10702266666667, 0.286209125, 2.145855125, 0.305474083333333, 
    1.60016616666667, 0.281528625, 1.94182179166667, 0.294655916666667
    ), sd = c(0.727246182828044, 0.0816168443914292, 0.549168068103643, 
    0.0894916121701392, 1.14554677132408, 0.0958562360654162, 
    1.06827971273128, 0.0953131237162305, 1.18204258551111, 0.0896670491921828, 
    1.32864473484909, 0.109865886496798, 0.605344957514288, 0.0815454655757737, 
    0.833908172662699, 0.0798994165789182, 1.11582277105041, 
    0.0976064300150272, 0.667812406644538, 0.142929179817685, 
    0.686043669971901, 0.109794818975944, 1.39509308576833, 0.161854932615856
    ), se = c(0.209937889711449, 0.0235607535398997, 0.158531165974993, 
    0.0258340031883217, 0.330690868396632, 0.0276713118479362, 
    0.308385789857611, 0.0275145288174349, 0.341226302469221, 
    0.0258846474942731, 0.383546697661249, 0.0317155495718416, 
    0.174748037086728, 0.0235401482506832, 0.240728553983119, 
    0.0230649748349663, 0.322110288616933, 0.0281765493219072, 
    0.192780836372198, 0.0412601002213964, 0.198043748767058, 
    0.0316950341456936, 0.402728684306467, 0.0467234944577166
    ), ci = c(0.462070179795855, 0.0518568689018959, 0.348924743722983, 
    0.0568602576432562, 0.727845693918804, 0.0609041467375754, 
    0.678752547059741, 0.0605590696140879, 0.751034027967696, 
    0.0569717250090983, 0.844180589754564, 0.069805453951774, 
    0.384617836383033, 0.0518115169661108, 0.529839974927164, 
    0.0507656673296478, 0.708959965158704, 0.0620161669201078, 
    0.424307760005262, 0.0908128682911871, 0.435891352085212, 
    0.0697602998032695, 0.886399857701764, 0.102837717929058)), row.names = c(NA, 
-24L), class = "data.frame")
ls_p <- 
   tgc %>%
   split(., .$Session) %>%
   map(function(x)
     ggplot(x, aes(x = Condition, y = EE, fill = test)) +
       geom_errorbar(aes(ymin = EE - se, ymax = EE + se, group = test), position = position_dodge(0.5), width = .1) +
       geom_col(width = 0.5, color = "black", position = "dodge") +
       labs(x= "Workspace", y = "EE (cm)") +
       scale_fill_manual("Test:", values = c("grey80", "grey20")) +
       facet_nested(. ~ Session + Group, scales = "free_y") +
       cowplot::theme_half_open() 
   )
 
 wrap_plots(ls_p) + plot_layout(guides = "collect") & 
   theme(legend.position = "bottom", legend.margin=margin(t=0, r=3, b=5, l=3, unit="cm")) + theme(aspect.ratio = 3/2) 
 

【问题讨论】:

我建议遵循约定并尝试链接到上一个问题***.com/q/69893914/7941188 【参考方案1】:

使用拼凑而成的主题时,您有两种选择。您可以在绘图级别(使用+)或拼凑级别(使用&amp;)执行此操作。下面只是使用两种选项的一种方式,用于演示目的。

为什么你应该只给theme()打电话一次?

多次调用主题在技术上并没有错,但它会使代码混乱。为什么这么糟糕?您很容易产生错误/错误,因为很容易创建相互矛盾的主题调用。

library(tidyverse)
library(ggh4x)
library(patchwork)

ls_p <- 
  tgc %>%
  split(., .$Session) %>%
  map(function(x)
    ggplot(x, aes(x = Condition, y = EE, fill = test)) +
      geom_errorbar(aes(ymin = EE - se, ymax = EE + se, group = test), position = position_dodge(0.5), width = .1) +
      geom_col(width = 0.5, color = "black", position = "dodge") +
      labs(x= "Workspace", y = "EE (cm)") +
      scale_fill_manual("Test:", values = c("grey80", "grey20")) +
      facet_nested(. ~ Session + Group, scales = "free_y") +
      cowplot::theme_half_open() +
      theme(axis.text.x = element_text(size = 12,face="bold"),#, angle = 10, hjust = .5, vjust = .5),axis.text.y = element_text(size = 12, face = "bold"),
            axis.title.y = element_text(vjust= 1.8, size = 16),
            axis.title.x = element_text(vjust= -0.5, size = 16),
            axis.title = element_text(face = "bold"))
  )

wrap_plots(ls_p) + plot_layout(guides = "collect") & 
  theme(legend.position = "bottom",
        aspect.ratio = 3/2)

【讨论】:

【参考方案2】:

它有效。我认为您错过了添加 theme 的订单/位置。

wrap_plots(ls_p) + plot_layout(guides = "collect") & 
  theme(legend.position = "bottom", legend.margin=margin(t=0, r=3, b=5, l=3, unit="cm")) + theme(aspect.ratio = 3/2) +
  theme(
    axis.text.x = element_text(size = 12,face="bold"),#, angle = 10, hjust = .5, vjust = .5),axis.text.y = element_text(size = 12, face = "bold"),
    axis.title.y = element_text(vjust= 1.8, size = 16),
    axis.title.x = element_text(vjust= -0.5, size = 16),
    axis.title = element_text(face = "bold"))

没有theme 的情节

【讨论】:

我建议只调用一次主题...

以上是关于加粗标题和轴刻度的主要内容,如果未能解决你的问题,请参考以下文章

如何明确设置 QChart 轴刻度?

R语言ggplot2可视化:默认情况下ggplot2在x轴和y轴的刻度线和轴之间保留了一些空间设置ggplot2可视化去除可视化结果与坐标轴之间的空间可视化结果与坐标轴紧紧贴合,没有空白区域

Pyqtgraph plotwidget:无法将轴刻度的字体粗细更改为 BOLD

matplotlib 中轴标签文本顶部的位置是啥?

在 matplotlib 中优雅地更改图框的颜色

整数刻度标签 matplotlib,没有原点刻度标签