geom_point 颜色不正确

geom_point color is not correct

我正在尝试使用 ggmap 从 csv 中绘制点。输入的 csv 具有纬度、经度和十六进制颜色值(以及用于创建颜色值的种子)。但是,点的十六进制和点的实际颜色不匹配。这是为什么?

The current output

我的代码:

library(ggmap)

stores <- data.frame((read.csv(file="./mapData")

# Fetch the map
madison = get_map(location = location, source = "osm")

# Draw the map
madisonMap = ggmap(madison)


# Add the points layer
madisonMap = madisonMap + 
  geom_point(data = stores, 
             aes(x = Longitude, y = Latitude, colour = Color), 
             size = 5)

数据集的示例子部分:

 Latitude, Longitude, Seed, Color
 45.508785, -122.632101 , 8, #22DD00
 45.515093, -122.642574, 11, #55AA00
 45.485144, -122.596184, 15.3, #9F6000

如果将颜色映射为十六进制值,ggplot 默认会将其解释为字符串。要使其将其解析为颜色,请添加 + scale_color_identity().

ggplot(mtcars[1:30,] %>% 
         mutate(color = rep(c("#22DD00", "#55AA00", "#9F6000"), times = 10)), 
       aes(wt, mpg, color = color)) +
  geom_point()

ggplot(mtcars[1:30,] %>% 
         mutate(color = rep(c("#22DD00", "#55AA00", "#9F6000"), times = 10)), 
       aes(wt, mpg, color = color)) +
  geom_point() +
  scale_color_identity()

你需要用scale_colour_manual添加它 我假设颜色应该代表 Seed

madisonMap = madisonMap + geom_point(data = stores, aes(x = Longitude, y = Latitude,colour=as.factor(Seed)), size = 5) + 
                      scale_colour_manual(values=stores$Color,labels=as.factor(stores$Seed),name='Seed')