【问题标题】:How to change font color within axislabels如何更改轴标签中的字体颜色
【发布时间】:2018-08-02 23:32:01
【问题描述】:
library(data.table)
library(ggplot2)
d <- data.table(setvalue = c("1.c , 1.d , 1.f , 2.b ", "1.b , 1.d , 1.f , 2.f ", "1.c , 1.d , 2.f , 2.h ", "1.b , 1.d , 1.f , 2.i ","1.c , 1.d , 2.f , 3.j "),
                    pct = c(0.06, 0.04, 0.028, 0.026, 0.017),
                    cumpct = c(0.06, 0.10, 0.128, 0.156, 0.173))

break_at_comma <- function(x) {gsub(",", "\n", x)}

ggplot(d, aes(x=reorder(setvalue, cumpct, sum), y=pct))+geom_bar(stat="identity")+
  theme_bw()+
  scale_y_continuous(labels=scales::percent, name="Procent of all combinations")+
  scale_x_discrete(name="chosen combinations", labels=break_at_comma)

产生这个情节:

但是 - x 轴上列出的“组合”是有意义的。所以我想以“1.[az]{1}”开头的文本为绿色,以“2.[az]{1}”开头的文本,黄色,以及以“3.[az”开头的文本]{1}" 红色。

我希望这是有道理的。最终结果应该是这样的(标签是重复的,所以只看颜色):

【问题讨论】:

  • 您可以使用ifelse 单独为轴标签着色,如下所示:stackoverflow.com/a/38862452/4421870,但是,我不知道是否存在在单个标签中具有多种颜色的功能。您可能必须分别绘制每一行轴标签,即绘制 1 的第一个,2 的第二个
  • 您可能可以通过在grid 级别进行编辑来实现,但要找到您正在寻找的特定元素可能会很棘手:stackoverflow.com/a/25105646/4421870

标签: r ggplot2


【解决方案1】:

我选择了另一种方法,而不是格式化标签。

基本上我在 xaxis 下方创建另一个图,并将这两个图与 cowplot 包合并(我也使用 stringr 包进行数据处理)。

希望这可以帮助那里的人:-)

最终结果如下所示:

library(data.table)
library(ggplot2)
d <- data.table(setvalue = c("1.c , 1.d , 1.f , 2.b ", "1.b , 1.d , 1.f , 2.f ", "1.c , 1.d , 2.f , 2.h ", "1.b , 1.d , 1.f , 2.i ","1.c , 1.d , 2.f , 3.j "),
                pct = c(0.06, 0.04, 0.028, 0.026, 0.017),
                cumpct = c(0.06, 0.10, 0.128, 0.156, 0.173))

break_at_comma <- function(x) {gsub(",", "\n", x)}

bars <- ggplot(d, aes(x=reorder(setvalue, cumpct, sum), y=pct))+geom_bar(stat="identity")+
  theme_bw()+
  scale_y_continuous(labels=scales::percent, name="Percent of all combinations")+
  scale_x_discrete(name="chosen combinations", labels=break_at_comma)+
  theme(panel.border = element_blank())

# Solution

d[, c("one", "two", "three", "four") := list(stringr::str_count(setvalue, "1.[a-z]{1}"),
                                                         stringr::str_count(setvalue, "2.[a-z]{1}"),
                                                         stringr::str_count(setvalue, "3.[a-z]{1}"),
                                                         stringr::str_count(setvalue, "4.[a-z]{1}")), by=setvalue]

mds <- melt(d, id.vars=c("setvalue"), measure.vars = c('one', 'two', 'three', 'four'))

levs <- ggplot(mds, aes(x=setvalue, y=value, fill=variable, label=setvalue))+geom_bar(stat="identity")+
  scale_fill_manual(values = c("two"= "#308762", "one"="#30647D" , "three"="#FFA135", 'four'='#f0f0f0'))+
  theme_minimal()+labs(x="", y="Comb. Levels")+
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        panel.grid = element_blank(),
        legend.position = "none")


cowplot::plot_grid(bars,levs, ncol = 1, align = 'v', nrow=2, rel_heights = c(0.8, 0.2)) + 
  annotate("rect", xmin = 0.1, xmax = 0.99, ymin = 0.03, ymax = 0.99, color = "black", fill = NA)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-23
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多