Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 根据值设置点的颜色_R_Ggplot2_Coordinates_Conditional Statements_Ggmap - Fatal编程技术网

R 根据值设置点的颜色

R 根据值设置点的颜色,r,ggplot2,coordinates,conditional-statements,ggmap,R,Ggplot2,Coordinates,Conditional Statements,Ggmap,我有一个如下所示的数据帧: x1= c("Station 1", "Station 2", "Station 3", "Station 4", "Station 5", "Station 6") x2= c(58.73, 57.20, 41.90, 38.00, 47.10, 67.30) x3= c(16.55, -2.10, 8.80, 23.70, 24.50, 14.40) x4= c(342, 1900, 283, 832, 212, 1533) x5= c("rual", "rual

我有一个如下所示的数据帧:

x1= c("Station 1", "Station 2", "Station 3", "Station 4", "Station 5", "Station 6")
x2= c(58.73, 57.20, 41.90, 38.00, 47.10, 67.30)
x3= c(16.55, -2.10, 8.80, 23.70, 24.50, 14.40)
x4= c(342, 1900, 283, 832, 212, 1533)
x5= c("rual", "rual", "urban", "suburban", "rual", "urban")

testframe = data.frame(Station=x1, LAT=x2, LON=x3, ALT=x4, AREA=x5) 
我想用3种不同的颜色显示这些点。绿色代表乡村,黄色代表郊区,红色代表城市

但直到现在,我只设法把它们全部显示成一种颜色。我没有这样做:

library(ggmap)
library(ggplot2)

Europe = get_map(location = "Europe", zoom = 4)

p = ggmap(Europe)
p = p + geom_point(data=testframe, aes(x=testframe$LON, y=testframe$LAT), color = "red", size=1)
p

有人能帮我吗

您可以尝试以下方法:

p +
  geom_point(data = testframe, aes(LON, LAT, color = AREA), size = 10) +
  scale_color_manual(name = "AREA", values = cols)

或复制/粘贴此代码块:

library(ggmap)
library(ggplot2)

x1 <- c("Station 1", "Station 2", "Station 3", "Station 4", "Station 5", "Station 6")
x2 <- c(58.73, 57.20, 41.90, 38.00, 47.10, 67.30)
x3 <- c(16.55, -2.10, 8.80, 23.70, 24.50, 14.40)
x4 <- c(342, 1900, 283, 832, 212, 1533)
x5 <- c("rual", "rual", "urban", "suburban", "rual", "urban")

testframe <- data.frame(
  Station = x1,
  LAT = x2,
  LON = x3,
  ALT = x4,
  AREA = x5
)

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

cols <- c(
  "rual" = "darkgreen",
  "suburban" = "yellow",
  "urban" = "red"
)

p <- ggmap(Europe)

p +
  geom_point(data = testframe, aes(LON, LAT, color = AREA), size = 10) +
  scale_color_manual(name = "AREA", values = cols)
库(ggmap)
图书馆(GG2)

x1您可以尝试以下方法:

p +
  geom_point(data = testframe, aes(LON, LAT, color = AREA), size = 10) +
  scale_color_manual(name = "AREA", values = cols)

或复制/粘贴此代码块:

library(ggmap)
library(ggplot2)

x1 <- c("Station 1", "Station 2", "Station 3", "Station 4", "Station 5", "Station 6")
x2 <- c(58.73, 57.20, 41.90, 38.00, 47.10, 67.30)
x3 <- c(16.55, -2.10, 8.80, 23.70, 24.50, 14.40)
x4 <- c(342, 1900, 283, 832, 212, 1533)
x5 <- c("rual", "rual", "urban", "suburban", "rual", "urban")

testframe <- data.frame(
  Station = x1,
  LAT = x2,
  LON = x3,
  ALT = x4,
  AREA = x5
)

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

cols <- c(
  "rual" = "darkgreen",
  "suburban" = "yellow",
  "urban" = "red"
)

p <- ggmap(Europe)

p +
  geom_point(data = testframe, aes(LON, LAT, color = AREA), size = 10) +
  scale_color_manual(name = "AREA", values = cols)
库(ggmap)
图书馆(GG2)
x1