GGPLOT仅更改图例中某些部分的字体

发布于 2025-02-11 20:23:38 字数 691 浏览 2 评论 0原文

是否可以将鱼的科学名称透露在X轴上和传说中,并在其余部分中使用正常字体?

就我而言,我希望这样做,例如barbatula barbatula(bachschmerle),只有barbatula barbatula在正常字体

这是现在的条形图

,这是数据IM的一部分使用

我的代码是:

ggplot(R_Sandbach, aes(x = fct_infreq (Species), fill=Species ))+
  geom_bar()+
  theme_minimal()+
  geom_text(aes(label=..count..), stat = "count", vjust = -.1, colour= "black")+
  theme(axis.text.x = element_text(angle = 90, size = 5))+
  xlab("Fischarten")+
  ylab("Individuenanzahl")

Is it possible to put the scientific names of the fish in italics on the x-axis and in the legend and to use normal font for the rest?

In my case I would like that for example Barbatula barbatula (Bachschmerle) only Barbatula barbatula is in italics and (Bachscmerle) in the normal font

This is the bar chart right now

And this is a part of the data im using

My code is:

ggplot(R_Sandbach, aes(x = fct_infreq (Species), fill=Species ))+
  geom_bar()+
  theme_minimal()+
  geom_text(aes(label=..count..), stat = "count", vjust = -.1, colour= "black")+
  theme(axis.text.x = element_text(angle = 90, size = 5))+
  xlab("Fischarten")+
  ylab("Individuenanzahl")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装不在乎 2025-02-18 20:23:38

ggtext绝对是一个不错的选择,尽管您可以像您在问题中所做的那样直接使用主题直接操纵图的各个方面。

请参阅下面的示例:

library(tidyverse)

ggplot() +
  geom_blank() +
  labs(x = "Fischarten", y = "Individuenanzahl", title = "My Super Awesome Title") +
  theme(axis.title.x  = element_text(face = "italic"),
        plot.title = element_text(face = "italic"))


这将产生一个带有斜面标题和X轴的空白图。您可以根据需要将尽可能多的更改添加到them>函数中。

编辑:

如果您需要斜体字标题的特定单词等。

根据我的个人经验,这有时会引起GGPLOT的一般主题的问题。

使用此软件包库(MDTHEMES)解决了HTML的问题未被读取。

例如:

ggplot() +
  geom_blank() +
  labs(x = paste("<i>Fischarten</i>", "Other Title Stuff"), y = "Individuenanzahl", title = paste("My Super", "<i>Awesome Title</i>")) +
mdthemes::md_theme_classic()

ggtext is definitely a good option, although you can just manipulate aspects of your plot directly using theme as you've done in your question.

See an example below:

library(tidyverse)

ggplot() +
  geom_blank() +
  labs(x = "Fischarten", y = "Individuenanzahl", title = "My Super Awesome Title") +
  theme(axis.title.x  = element_text(face = "italic"),
        plot.title = element_text(face = "italic"))


This produces a blank plot with the plot title and x axis both in italics. You can add as many changes into your theme() function as you like.

EDIT:

In the case of which you need specific words of your titles in italics, etc. you can manually incorporate html into your text and then format your plot to read this html.

In my personal experience this has sometimes caused issues with the general themes of ggplot.

Using this package library(mdthemes) solves the issues of html not being read as it should.

For example:

ggplot() +
  geom_blank() +
  labs(x = paste("<i>Fischarten</i>", "Other Title Stuff"), y = "Individuenanzahl", title = paste("My Super", "<i>Awesome Title</i>")) +
mdthemes::md_theme_classic()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文