0

我正在使用带有 ggplot 的 geom_point() 来基本上创建一个 -coefplot- 的等价物,在一个绘图上具有 5 个不同模型的系数。

我可以只更改一个点(和相应的 CI 条)的颜色吗?颜色/字体/等呢?单个轴刻度标签?例如,如果我只想将 Model 3 标签的字体设为粗体,并将 Model 3 的系数点(+ CI 条)设为红色(或任何其他颜色来区分它)。我试过scale_fill_manual了,但这似乎没有奏效。

我当前的代码(使用 scale_x_discrete,模型的名称只是模型 1、2、3 等,我将它们按顺序排列在那里):

ggplot(d, aes(x = var, y = coef)) + 
  geom_point() + 
  geom_pointrange(aes(ymin = cilow, ymax = ciupper)) + 
  scale_x_discrete(limits = model_order, labels = model_label) + 
  theme_minimal()
4

1 回答 1

0

您可以使用条件仅对所需的点和 CI 着色。ifelse(test to select your group, color name if yes, color name if not).

library(tidyverse)

df=iris %>% 
  group_by(Species) %>% 
    summarise(min=min(Petal.Length),
              max=max(Sepal.Length),
              mean=(min+max)/2)  
ggplot(df,aes(Species, mean,color=Species)) +
  geom_point() +
  geom_pointrange(aes(ymin = min, ymax = max))+
  scale_color_manual(values = ifelse(df$Species=="setosa","red","black"))

要更改轴标签,您可以ggtext按照本文中的说明使用

library(ggtext)
library(glue)

highlight = function(x, pat, color="black", family="") {
  ifelse(grepl(pat, x), glue("<b style='font-family:{family}; color:{color}'>{x}</b>"), x)
}
ggplot(df,aes(Species, mean,color=Species)) +
  geom_point() +
  geom_pointrange(aes(ymin = min, ymax = max))+
  scale_x_discrete(labels=function(x) highlight(x, "setosa", "purple")) +
  scale_color_manual(values = ifelse(df$Species=="setosa","red","black"))
  theme_bw()+
  theme(axis.text.x=element_markdown(size=15))

它不适theme_minimal用于 ,所以我使用了不同的。

阴谋

于 2021-08-19T03:32:20.290 回答