R - ggplot - 如何根据值自动设置点颜色? [英] R - ggplot - How to automatically set point color based on values?
问题描述
我的问题类似于
似乎颜色在地图上不是很好,因为值似乎趋于接近中值。我认为OP需要使用 cut()
创建一个新的分组变量,并为这些组分配颜色或使用另一个scale_color类型的函数。我用RColorBrewer软件包提出了以下内容。我认为OP需要考虑他想用颜色刷他的图形。
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)
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.
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")
这篇关于R - ggplot - 如何根据值自动设置点颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!