R - ggplot - How to automatically set point color based on values?

不想你离开。 提交于 2019-12-13 23:07:35

问题


My question is similar to this question.

But I can't transfer it to my own data.

I have a dataframe like this (over 1400 rows):

     Code                  Stationsname Startdatum      LAT      LON Höhe  Area     Mean
1 AT0ENK1       Enzenkirchen im Sauwald 03.06.1998 48.39167 13.67111  525 rural 55.76619
2 AT0ILL1                       Illmitz 01.05.1978 47.77000 16.76640  117 rural 58.98511
3 AT0PIL1          Pillersdorf bei Retz 01.02.1992 48.72111 15.94223  315 rural 59.47489
4 AT0SON1                     Sonnblick 01.09.1986 47.05444 12.95834 3106 rural 97.23856
5 AT0VOR1 Vorhegg bei K”tschach-Mauthen 04.12.1990 46.67972 12.97195 1020 rural 70.65373
6 AT0ZIL1             Ried im Zillertal 08.08.2008 47.30667 11.86389  555 rural 36.76401

Now I want to create a map with ggplot and display the points in different colors based on the value in the Mean column, it reaches from 18 to 98.

Also I would like to change the symbols from a dot to a triangle if the value in the column Höhe is over 700.

Until now I did this:

library(ggmap)
library(ggplot2)

Europe <- get_map(location = "Europe", zoom = 3)

p = ggmap(Europe)

p = p + geom_point(data = Cluster, aes(LON, LAT, color = Mean), 
                   size = 1.5, pch = ifelse(Höhe < 700,'19','17')) +
    scale_x_continuous(limits = c(-25.0, 40.00), expand = c(0, 0)) +
    scale_y_continuous(limits = c(34.00, 71.0), expand = c(0, 0)) +
    scale_colour_gradient ---??

But I don't know how to go on and assign the colors.


回答1:


I had a discussion with the OP using his data. One of his issues was to make scale_colour_gradient2() work. The solution was to set up a midpoint value. By default, it is set at 0 in the function. In his case, he has a continuous variable that has about 50 as median.

library(ggmap)
library(ggplot2)
library(RColorBrewer)

Europe2 <- get_map(maptype = "toner-2011", location = "Europe", zoom = 4) 

ggmap(Europe2) +
geom_point(data = Cluster, aes(x = LON, y = LAT, color = Mean, shape = Höhe > 700), size = 1.5, alpha = 0.4) + 
scale_shape_manual(name = "Altitude", values = c(19, 17)) + 
scale_colour_gradient2(low = "#3288bd", mid = "#fee08b", high = "#d53e4f",
                       midpoint = median(Cluster$Mean, rm.na = TRUE))

It seems that the colors are not that good in the map given values seem to tend to stay close to the median value. I think the OP needs to create a new grouping variable with cut() and assign colors to the groups or to use another scale_color type of function. I came up with the following with the RColorBrewer package. I think the OP needs to consider how he wanna use colors to brush up his graphic.

ggmap(Europe2) +
geom_point(data = Cluster, aes(x = LON, y = LAT, color = Mean, shape = Höhe > 700), size = 1.5, alpha = 0.4) + 
scale_shape_manual(name = "Altitude", values = c(19, 17)) + 
scale_colour_distiller(palette = "Set1")  



来源:https://stackoverflow.com/questions/48614328/r-ggplot-how-to-automatically-set-point-color-based-on-values

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