英文:
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 <- 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()
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()
[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 <- 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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论