Conditional text colour based on background colour (High contrast text) for ggplot (geom_col, and geom_text)

huangapple go评论73阅读模式
英文:

Conditional text colour based on background colour (High contrast text) for ggplot (geom_col, and geom_text)

问题

你们聪明的R程序员们好!我正在尝试创建一个堆叠条形图,显示特定背景颜色的土壤厚度,然后在图上添加标签,这些标签的颜色会根据背景颜色而变化。我对R非常陌生,这是我第一次尝试这样的事情。我已经研究了几个例子,我觉得我离成功很近,但我似乎无法弄清楚如何根据背景颜色创建条件文本颜色。我想要类似于这个例子的效果:
https://lara-southard.medium.com/changing-geom-text-color-for-stacked-bar-graphs-in-ggplot-f0e45bfeaa56

我的代码很乱,因为它是从一个已有的例子修改而来的。
以下是我的代码:

soil_profile <- cbind.data.frame(
profile = "", # Name of the whole profile
from = c(0,0.41,0.76,1.00,1.45,1.58,1.88), # Beginning of each horizon
to = c(0.41,0.76,1.00,1.45,1.58,1.88,2.48), # End of each horizon
horizon=c('Peaty loam','Loamy peat', 'Humified peat (1)', 'Humified peat (2)', 'Fine sand',
'Pipe bedding and exclusion zone', 'Pipe'), # Name of the horizon
hex = c('#33CCCC', '#008080', '#003366', '#003366', '#F2F2F2', '#A9D18E', '#A9D18E')
)

soil_profile
soil_profile <- soil_profile%>%
mutate(
height = to - from
)

soil_profile
soil_plot<-ggplot(
data = soil_profile,
aes(
x=profile,y=-height, # specify x and y axis
fill=fct_reorder(horizon,to,.desc=TRUE)), # make sure that horizons are on the right order
)+
geom_col(
width=0.8, color= 'white' # Profile width
)

soil_plot
soil_plot<-soil_plot+
scale_fill_manual(
breaks=soil_profile$horizon,
values=soil_profile$hex)

soil_plot
soil_plot<-soil_plot+
guides(fill=FALSE)+ # Hide legend
geom_text(
aes(y=-(from+height/2),label=horizon))
soil_plot
soil_plot+
scale_x_discrete(position = 'top')+ # Move profile name to the top
labs(
title = "",
y = "Depth (meters)",
x=""
)+
theme_minimal()

这是我得到的图形
进入图像描述
这是我想要的图形
进入图像描述

我想要类似于这个例子的效果:
https://lara-southard.medium.com/changing-geom-text-color-for-stacked-bar-graphs-in-ggplot-f0e45bfeaa56

英文:

Hello you clever R people!
I'm trying to create a stack bar plot showing soil thicknesses with a specific background colour and then add labels to the plot that change colour depending on the background colour. I'm very new to R and this is the first time I've attempted something like this. I've worked through several examples and I think I'm getting close but I can't seem to work out how to create a conditional text colour based on the back ground colour. I want something like this example:-
https://lara-southard.medium.com/changing-geom-text-color-for-stacked-bar-graphs-in-ggplot-f0e45bfeaa56

My code is messy because it's modified from a worked example
Here is my code:-

soil_profile &lt;- cbind.data.frame(
  profile = &quot;&quot;,               # Name of the whole profile
  from = c(0,0.41,0.76,1.00,1.45,1.58,1.88),                    # Beginning of each horizon
  to = c(0.41,0.76,1.00,1.45,1.58,1.88,2.48),                    # End of each horizon
  horizon=c(&#39;Peaty loam&#39;,&#39;Loamy peat&#39;, &#39;Humified peat (1)&#39;, &#39;Humified peat (2)&#39;, &#39;Fine sand&#39;,
  &#39;Pipe bedding and exclusion zone&#39;, &#39;Pipe&#39;),       # Name of the horizon
  hex = c(&#39;#33CCCC&#39;, &#39;#008080&#39;, &#39;#003366&#39;, &#39;#003366&#39;, &#39;#F2F2F2&#39;, &#39;#A9D18E&#39;, &#39;#A9D18E&#39;)
)

soil_profile
soil_profile &lt;- soil_profile%&gt;%
 mutate(
    height = to - from 
 )

soil_profile
soil_plot&lt;-ggplot(
 data = soil_profile,
aes(
 x=profile,y=-height,                       # specify x and y axis
fill=fct_reorder(horizon,to,.desc=TRUE)), # make sure that horizons are on the right order
)+
  geom_col(
width=0.8, color= &#39;white&#39;                                  # Profile width
 )

soil_plot
soil_plot&lt;-soil_plot+
scale_fill_manual(
breaks=soil_profile$horizon,
values=soil_profile$hex)

soil_plot
soil_plot&lt;-soil_plot+
guides(fill=FALSE)+                      # Hide legend
 geom_text(
aes(y=-(from+height/2),label=horizon))
soil_plot
soil_plot+
  scale_x_discrete(position = &#39;top&#39;)+    # Move profile name to the top
labs(
title = &quot;&quot;,
y = &quot;Depth (meters)&quot;,
x=&quot;&quot;
 )+
 theme_minimal() 

Here is the graph I get
enter image description here
and here is the graph I want
enter image description here

I want something like this example:-
https://lara-southard.medium.com/changing-geom-text-color-for-stacked-bar-graphs-in-ggplot-f0e45bfeaa56

答案1

得分: 2

创建一个颜色调色板,为每个 horizon 分配您期望的文本颜色,就像您在填充调色板中所做的一样。然后在 color aes 上映射,并使用 scale_color_manual 设置您的颜色调色板:

注意:要自动设置文本颜色的方法,请参阅设置颜色以清楚显示数字

library(ggplot2)

pal_color <- c(rep("white", 4), rep("black", 3))
names(pal_color) <- soil_profile$horizon

ggplot(
  data = soil_profile,
  aes(
    x = profile, y = -height,
    fill = fct_reorder(horizon, to, .desc = TRUE)
  ),
) +
  geom_col(
    width = 0.8, color = "white"
  ) +
  scale_fill_manual(
    breaks = soil_profile$horizon,
    values = soil_profile$hex
  ) +
  scale_color_manual(
    values = pal_color
  ) +
  guides(fill = "none", color = "none") +
  geom_text(
    aes(
      y = -(from + height / 2), label = horizon,
      color = horizon
    )
  ) +
  scale_x_discrete(position = "top") +
  labs(
    title = NULL, y = "Depth (meters)", x = NULL
  ) +
  theme_minimal()

Conditional text colour based on background colour (High contrast text) for ggplot (geom_col, and geom_text)


[1]: https://i.stack.imgur.com/E1gIV.png
英文:

Create a color palette which assigns to each horizon your desired text color as you did with the fill palette. Afterwards map on the color aes and set your color palette using scale_color_manual:

Note: For an approach to automatically set the text colors see Set a color to show clear the numbers

library(ggplot2)

pal_color &lt;- c(rep(&quot;white&quot;, 4), rep(&quot;black&quot;, 3))
names(pal_color) &lt;- soil_profile$horizon

ggplot(
  data = soil_profile,
  aes(
    x = profile, y = -height,
    fill = fct_reorder(horizon, to, .desc = TRUE)
  ),
) +
  geom_col(
    width = 0.8, color = &quot;white&quot;
  ) +
  scale_fill_manual(
    breaks = soil_profile$horizon,
    values = soil_profile$hex
  ) +
  scale_color_manual(
    values = pal_color
  ) +
  guides(fill = &quot;none&quot;, color = &quot;none&quot;) +
  geom_text(
    aes(
      y = -(from + height / 2), label = horizon,
      color = horizon
    )
  ) +
  scale_x_discrete(position = &quot;top&quot;) +
  labs(
    title = NULL, y = &quot;Depth (meters)&quot;, x = NULL
  ) +
  theme_minimal()

Conditional text colour based on background colour (High contrast text) for ggplot (geom_col, and geom_text)

huangapple
  • 本文由 发表于 2023年6月5日 17:05:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定