geom_point颜色不正确

问题描述 投票:1回答:2

我正在尝试使用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
r ggmap
2个回答
2
投票

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

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

enter image description here

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

enter image description here


0
投票

您需要将其添加为scale_colour_manual我认为颜色应该代表种子

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.