简体   繁体   中英

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. 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. 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)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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