gpt4 book ai didi

r - 仅对 geom_bar 中的选定栏使用不同的颜色

转载 作者:行者123 更新时间:2023-12-05 04:02:52 25 4
gpt4 key购买 nike

我想用不同的颜色突出显示中间的栏(名为 SSA)。我已经尝试了可用的示例,但没有得到想要的结果。

看来我无法正确使用 ifelse 元素。感谢您帮助检测错误。

“填充”不是我数据中的一个因素,这与此处提供的解决方案不同:change color of only one bar in ggplot

数据:

structure(list(yield = c(48, 33, 46, 44, 79, 20, 21, 8, 40, 72, 
12, 31, 65, 10, 71, 36, 20, 60, 69, 59, 58, 49, 75, 28, 71, 61,
34, 39, 42, 64, 47, 36, 78, 73, 51, 46, 3, 55, 70, 80, 29, 45,
70, 72, 32, 42, 48), df = c(2, 13, 0, 9, -3, 3, 2, 0, 2, 11,
0, 0, 5, -2, -1, -15, 0, 2, 14, 1, 6, 2, -1, 2, 8, 16, 8, 0,
-13, 3, 0, 10, 10, -3, 7, 0, -6, 16, 0, 1, -23, 9, 11, 12, 4,
8, 28), country = c("Angola", "Benin", "Botswana", "Burkina Faso",
"Burundi", "Cabo Verde", "Cameroon", "Central African Republic",
"Chad", "Comoros", "Congo, Dem. Rep.", "Congo, Rep.", "Cote d'Ivoire",
"Equatorial Guinea", "Eswatini", "Ethiopia", "Gabon", "Gambia, The",
"Ghana", "Guinea", "Guinea-Bissau", "Kenya", "Lesotho", "Liberia",
"Madagascar", "Malawi", "Mali", "Mauritania", "Mauritius", "Mozambique",
"Namibia", "Niger", "Nigeria", "Rwanda", "Sao Tome and Principe",
"Senegal", "Seychelles", "Sierra Leone", "Somalia", "South Africa",
"Sudan", "Tanzania", "Togo", "Uganda", "Zambia", "Zimbabwe",
"SSA")), row.names = c(NA, -47L), class = c("tbl_df", "tbl",
"data.frame"))

我的代码:

ggplot(arble.land, 
aes(x = reorder(country, yield), y = yield),
col = ifelse(country = "SSA", "Highlighted", "Normal")) +
geom_bar(stat = "identity") +
coord_flip()

enter image description here

最佳答案

您的代码存在一些问题:

  • 正如@Z_Lin 提到的,颜色部分应该在对aes 的调用中。
  • if_else 语句中,您应该使用像 == 这样的逻辑运算符>
  • col 参数影响条形的线条颜色,您可能更喜欢 fill 参数

以下代码:

ggplot(arble.land, 
aes(x = reorder(country, yield),
y = yield,
fill = ifelse(country == "SSA", "Highlighted", "Normal") )) +
geom_bar(stat = "identity") +
## drop legend and Y-axis title
theme(legend.position = "none", axis.title.y = element_blank()) +
coord_flip()

产生这张图:

enter image description here

请告诉我这是否是您想要的。

更新

如果你想去除左右两边的“多余空间”,你可以像这样在coord_flip中使用expand参数

ggplot(arble.land, 
aes(x = reorder(country, yield),
y = yield,
fill = ifelse(country == "SSA", "Highlighted", "Normal") )) +
geom_bar(stat = "identity") +
## drop legend and Y-axis title
theme(legend.position = "none", axis.title.y = element_blank()) +
coord_flip(expand = FALSE) ########## SMALL UPDATE

产生以下情节:

enter image description here

更新 2:填充颜色

您可以使用 scale_fill_manual() 手动设置颜色

ggplot(arble.land, 
aes(x = reorder(country, yield),
y = yield,
fill = ifelse(country == "SSA", "Highlighted", "Normal") )) +
geom_bar(stat = "identity") +
## add manual color scale
scale_fill_manual("legend", ###### in this call: colour change
values = c("Highlighted" = "black", "Normal" = "orange")) +
## drop legend and Y-axis title
theme(legend.position = "none", axis.title.y = element_blank()) +
coord_flip(expand = FALSE)

制作这个情节:

enter image description here

关于r - 仅对 geom_bar 中的选定栏使用不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54103496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com