R语言 使一个轴标签在y轴上为斜体

dm7nw8vv  于 2023-04-09  发布在  其他
关注(0)|答案(2)|浏览(240)

我有一个图,底部的勾有一个'平均'的标签。我想这是斜体,但它不工作。任何想法?
我得到错误:官方不支持element_text()的矢量化输入。结果可能是意外的,或者在ggplot2的未来版本中可能会更改。
但是,即使我将代码更改为:

....theme(axis.text.y=element_text(face='italic')....

什么也没有发生,所有的标签在y轴上保持'平原'。矢量形式的工作,所以我不明白为什么面对不工作。

示例代码
stacked <- ggplot(tally, aes(fill=Ellenberg, y=n, x=Quadrat)) +
    geom_bar(position='stack', stat='identity', show.legend=FALSE) +
    scale_x_discrete(labels=labels) +
    scale_fill_manual(values = colLegend) +
    theme_classic() +
    facet_wrap(~ Q, nrow=1, scales='free_x') +
    expand_limits(y=-3) +
    geom_text(
      label=tally$values, y=-3, color='red', fontface='italic', size=4.5, family="Calibri") +
    labs(x='Year', 
         y='    Number of Plants\n\n',
         title=plotTitles[[j]],
         subtitle = plotSubTitles[[j]]) +
    scale_y_continuous(breaks = c(-3, 0, 5, 10, 15, 20, 25),
                       labels=expression("Mean", "0", "5", "10", "15", "20", "25")) +
    theme(axis.text.y=element_text(face=c("italic", "plain", "plain", "plain", "plain", "plain", "plain"), 
                                   colour=c('red','black','black','black','black','black','black'))) +
    theme(axis.ticks=element_line(colour=c('white','black','black','black','black','black','black'))) + 
    theme(plot.title = element_text(size=20, face="bold", family="Calibri"),
        plot.subtitle = element_text(size=20, family="Calibri")) +
    theme(text=element_text(size=20, family="Calibri")) +
    theme(axis.title.y=element_text(margin=margin(l=20)))
  
  plot(stacked)
  plotList[[dfName]] <- stacked
  
  j = j + 1
示例图

qgzx9mmu

qgzx9mmu1#

问题是你把标签 Package 在了expression中。删除它应该会给予你想要的结果,但你仍然会得到关于Vectorized input is not officially supported的警告:
使用基于mtcars的最小可重现示例:

library(ggplot2)

ggplot(mtcars, aes(hp, mpg)) +
  geom_point() +
  scale_y_continuous(
    breaks = c(-3, 0, 5, 10, 15, 20, 25),
    labels = c("Mean", "0", "5", "10", "15", "20", "25"),
    limits = c(-3, NA)
  ) +
  theme(axis.text.y = element_text(
    face = c("italic", rep("plain", 6)),
    colour = c("red", rep("black", 6))
  ))
#> Warning: Vectorized input to `element_text()` is not officially supported.
#> ℹ Results may be unexpected or may change in future versions of ggplot2.

fd3cxomn

fd3cxomn2#

使用{ggtext}包可能会更好。纯斜体与简单的markdown格式非常容易(图1),但如果你想使用不同的颜色,你将需要一些CSS格式(图2)- ggtext对CSS的支持有限。
然而,如果我可以提出一个建议的话。你把一个连续的轴和一个分类变量结合在一起是很笨拙的,分类变量实际上是一个聚合的度量。我认为最简洁的方法是创建两个图,然后把它们结合起来,例如拼凑(图3)。这可能看起来更冗长,但从长远来看,它不太容易出错。
另一种选择,也许是我的首选,是在绘图面板外添加聚合行作为自定义注解(图4)。

library(ggplot2)
df <- data.frame(
  q = rep(paste0("Q", 1:9), each = 3),
  Year = rep(c(19, 20, 22), 9),
  n_plant = rnorm(27, 20),
  mean = sample(c(.1, .2), 27, rep = T)
)

p <- ggplot(df, aes(Year, n_plant)) +
  geom_col() +
  geom_text(aes(y = -5, label = mean)) +
  facet_wrap(~q, ncol = 9)

斜体字非常简单。

p + scale_y_continuous(
  breaks = seq(-5, 20, 5),
  labels = c("*mean*", seq(0, 20, 5))
) +
  theme(axis.text.y = ggtext::element_markdown())

如果你想用不同的颜色,你需要一些基本的CSS格式。

p + scale_y_continuous(
  breaks = seq(-5, 20, 5),
  labels = c("<i><span style = 'color:red;'>mean</span></i>", seq(0, 20, 5))
) +
  theme(axis.text.y = ggtext::element_markdown())

我的建议是分别创建这些情节。

library(patchwork)

p <- ggplot(df, aes(Year, n_plant)) +
  geom_col() +
  facet_wrap(~q, ncol = 9)

p_ann <-
  ggplot(df, aes(Year, n_plant)) +
  geom_text(aes(y = "mean", label = mean), color = "red",
            size = 6*5/14) +
  facet_wrap(~q, ncol = 9) +
  ## this is the default expansion with bar graphs, you need this for alignment
  scale_x_continuous(expand = c(0, 0.6)) +
  theme_void() +
  theme(axis.text.y = element_text(color = "red", face = "italic", size = 10), 
        strip.text = element_blank())

p/p_ann +
  plot_layout(heights = c(1, .1))

或者,在地块区域外进行注解

## make data frame for your single facet annotation
annot_df <- data.frame(Year = 18, n_plant = -5, q = "Q1", label = "mean")

ggplot(df, aes(Year, n_plant)) +
  geom_col() +
  facet_wrap(~q, ncol = 9) +
## now the mean bit
  geom_text(data = annot_df, aes(label = label), fontface = "italic", color = "red", hjust = 1) +
  geom_text(aes(y = -5, label = mean), color = "red", size = 6*5/14) +
  coord_cartesian(xlim = range(df$Year), ylim = c(0, max(df$n_plant)), clip = "off") +
## add plot margins
  theme(plot.margin = margin(b = 1, l = 1, unit = "in"))

相关问题