一个包含多个数据集的 ggplot
One ggplot with multiple data sets
我尝试在地图上放置点,所以我正在使用 ggmap
。对于积分,我有两个独立的数据集,示例如下。
dta1 = data.frame(storename = c(letters[1:5]),
storesize = c("small","small","medium","large","large"),
lon = c(10,11.2,15,19,22),
lat = c(5,5.8,5.6,6.1,3.4))
dta2 = data.frame(population = sample(100,100,replace = T),
lon = runif(100,10,22),
lat = runif(100,3.5,6))
这是情节的示例代码。在我的真实数据集中,我有 ggmap(map)
而不是 ggplot()
。为什么下面的代码不起作用?
ggplot() + geom_point(data=dta1,
aes(x=lon, y=lat,size = storesize), shape = 23,fill="blue") +
scale_size_manual(values = c(1,2,3)) +
geom_point(data=dta2,
aes(x=lon, y=lat,size = population), shape = 21,fill="orange")
如果我运行像这样分开,就可以了
ggplot() + geom_point(data=dta1,
aes(x=lon, y=lat,size = storesize), shape = 23,fill="blue") +
scale_size_manual(values = c(1,2,3))
ggplot() + geom_point(data=dta2,
aes(x=lon, y=lat,size = population), shape = 21,fill="orange")
我想要一个带有两个单独图例的图,每个单独的图都显示。
此外,如果我需要用不同的形状指向一个更特殊的地方(比方说shape = 11
),我应该怎么做?
描述了一个解决方案 here and here。
安装 ggnewscale
,然后使用 new_scale
:
添加新的秤
library(ggplot2)
library(ggnewscale)
dta1 = data.frame(storename = c(letters[1:5]),
storesize = c("small","small","medium","large","large"),
lon = c(10,11.2,15,19,22),
lat = c(5,5.8,5.6,6.1,3.4))
dta2 = data.frame(population = sample(100,100,replace = T),
lon = runif(100,10,22),
lat = runif(100,3.5,6))
ggplot() +
geom_point(data=dta1, aes(x=lon, y=lat,size=storesize), shape=23, fill="blue") +
scale_size_manual(values = c(1,2,3)) +
new_scale("size") +
geom_point(data=dta2, aes(x=lon, y=lat, size=population), shape=21, fill="orange")
我尝试在地图上放置点,所以我正在使用 ggmap
。对于积分,我有两个独立的数据集,示例如下。
dta1 = data.frame(storename = c(letters[1:5]),
storesize = c("small","small","medium","large","large"),
lon = c(10,11.2,15,19,22),
lat = c(5,5.8,5.6,6.1,3.4))
dta2 = data.frame(population = sample(100,100,replace = T),
lon = runif(100,10,22),
lat = runif(100,3.5,6))
这是情节的示例代码。在我的真实数据集中,我有 ggmap(map)
而不是 ggplot()
。为什么下面的代码不起作用?
ggplot() + geom_point(data=dta1,
aes(x=lon, y=lat,size = storesize), shape = 23,fill="blue") +
scale_size_manual(values = c(1,2,3)) +
geom_point(data=dta2,
aes(x=lon, y=lat,size = population), shape = 21,fill="orange")
如果我运行像这样分开,就可以了
ggplot() + geom_point(data=dta1,
aes(x=lon, y=lat,size = storesize), shape = 23,fill="blue") +
scale_size_manual(values = c(1,2,3))
ggplot() + geom_point(data=dta2,
aes(x=lon, y=lat,size = population), shape = 21,fill="orange")
我想要一个带有两个单独图例的图,每个单独的图都显示。
此外,如果我需要用不同的形状指向一个更特殊的地方(比方说shape = 11
),我应该怎么做?
描述了一个解决方案 here and here。
安装 ggnewscale
,然后使用 new_scale
:
library(ggplot2)
library(ggnewscale)
dta1 = data.frame(storename = c(letters[1:5]),
storesize = c("small","small","medium","large","large"),
lon = c(10,11.2,15,19,22),
lat = c(5,5.8,5.6,6.1,3.4))
dta2 = data.frame(population = sample(100,100,replace = T),
lon = runif(100,10,22),
lat = runif(100,3.5,6))
ggplot() +
geom_point(data=dta1, aes(x=lon, y=lat,size=storesize), shape=23, fill="blue") +
scale_size_manual(values = c(1,2,3)) +
new_scale("size") +
geom_point(data=dta2, aes(x=lon, y=lat, size=population), shape=21, fill="orange")