geom_point color is not correct

爱⌒轻易说出口 提交于 2021-01-29 03:36:52

问题


I'm trying to plot points from a csv using ggmap. The input csv has latitude, longitude, and hex color value (as well as a seed used to create the color value). However, the hex for the point and the actual color of the point don't match. Why is that?

The current output

My code:

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)

Example subsection of the dataset:

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

回答1:


If you map color to a hex value, ggplot will by default interpret that as a character string. To make it parse that as a color, add + 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()




回答2:


You need to add it with scale_colour_manual I assume the color is supposed to represent 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')



来源:https://stackoverflow.com/questions/58228096/geom-point-color-is-not-correct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!