简体   繁体   English

scale_fill_manual()不会更改图例上的颜色吗?

[英]scale_fill_manual() will not change colors on legend?

When I use scale_fill_manual() with ggplot, and specify certain values, the values on the legend both represent black shapes instead of the values that I specified in the code. 当我对scale_fill_manual()使用scale_fill_manual()并指定某些值时,图例上的值都代表黑色形状,而不是我在代码中指定的值。 So for example, if I specified scale_fill_manual(values = c("red",NA)) , I would want the legend to show a red shape and a black unfilled one, but both shapes are black. 因此,例如,如果我指定scale_fill_manual(values = c("red",NA)) ,则希望图例显示红色形状和黑色未填充形状,但两个形状均为黑色。 Anyway to customize the legend in this way? 无论如何以这种方式自定义图例?

Dates data: 日期数据:

c(12/31/2020,12/31/2021,12/31/2022,12/31/2023,12/31/2024,12/31/2025,12/31/2026
12/31/2027,12/31/2028,12/31/2029,12/31/2030,12/31/2031,12/31/2032,12/31/2033
12/31/2034,12/31/2035,12/31/2036,12/31/2037,12/31/2038,12/31/2039,12/31/2040
12/31/2041,12/31/2042,12/31/2043,12/31/2044,12/31/2045,12/31/2046,12/31/2047
12/31/2048,12/31/2049,12/31/2050,12/31/2051,12/31/2052,12/31/2053,12/31/2054
12/31/2055,12/31/2056,12/31/2057,12/31/2058,12/31/2059,12/31/2060,12/31/2061
12/31/2062,12/31/2063,12/31/2064,12/31/2065,12/31/2066,12/31/2067,12/31/2068
12/31/2069)

Example code and output: 示例代码和输出:

dat <- read.csv("Dates.csv")
a <- data.frame(date = dat[1:15,1],income = c(rep(10000,5),rep(40000,5),rep(100000,5)),
                family = c(rep("true",5),rep("false",5),rep("NA",5)),
                status = c(rep("low",5),rep("medium",5),rep("high",5)),
                state = c(rep("TX",5),rep("PA",5),rep("CA",5)))
a$date <- as.Date(a$date,format = "%m/%d/%Y")
ggplot(a,aes(x = date,y = income)) +
  geom_point(aes(color = state,fill = status,shape = family),size = 1) +
  scale_fill_manual(values = c("red","green",NA)) +
  scale_color_manual(values = c("red","green","brown")) + 
  scale_shape_manual(values = c(22,21,25))

RESULT: 结果:

在此处输入图片说明

PROBLEM: 问题:

Why is the fill section (status) of the legend black for all shapes? 为什么所有形状的图例的填充部分(状态)都是黑色的? How to fix/customize that part? 如何修复/定制该零件?

The legend looks to still be using the default point shape. 图例看起来仍在使用默认的点形状。 Add this to the end of your code to override it: 将其添加到代码末尾以覆盖它:

+  guides(fill = guide_legend(override.aes = list(shape = 21)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM