问题描述
我有这样的数据框
zip状态用户经度纬度
00501 NY 1000 -72.63708 40.92233
00544 NY 1000 -72.63708 40.92233
00601 PR 2000 -66.74947 18.1801
00602 PR 2000 -67.18024 18.36329
我正在绘制使用ggmap和geom_point的用户数量。
map< -get_map(location ='united states',zoom = 4,maptype =terrain,
source ='google',color ='color')
ggmap(map)+ geom_point(
aes(x = longitude,y = latitude,show_guide = TRUE,color = users),
data = data,alpha = .5,na.rm = T)+
scale_color_gradient(low =red,high =green)
剧情就像这样
map< -get_map(location ='united states ',zoom = 4,maptype =terrain,
source ='google',color ='color')
ggmap(map)+ geom_point(
aes(x = longitude,y = latitude,show_guide = TRUE,color = users),
data = data,alpha = .5,na.rm = T)+
scale_color_gradient(low =red,high =green)+
geom_text(aes(x = longitude,y = latitude,label = as.character(state)),
data = data,inherit.aes = FALSE)
情节就是这样。
标签是为每一行创建的。如何为多行创建独特的标签?
编辑:执行此操作的一种方法是从数据本身删除重复的状态名称。是否有更有效的方法?
最简单的解决方案是先将数据聚合到一个新的数据框中:
agg.data< - aggregate(cbind(longitude,latitude)〜state,data = data,mean)
,然后使用汇总的数据包含文本标签:
ggmap(map)+
geom_point(data = data,
aes(x = longitude,y = latitude,show_guide = TRUE,color = users),
alpha = 0.5,na.rm = T)+
scale_color_gradient(low =red,high =green)+
geom_text(data = agg.data,
aes(x = longitude,y = latitude,label = as.character(state)))
I have a data frame like this
zip state users longitude latitude
00501 NY 1000 -72.63708 40.92233
00544 NY 1000 -72.63708 40.92233
00601 PR 2000 -66.74947 18.1801
00602 PR 2000 -67.18024 18.36329
I'm plotting number of users using ggmap and geom_point.
map<-get_map(location='united states', zoom=4, maptype = "terrain",
source='google',color='color')
ggmap(map) + geom_point(
aes(x=longitude, y=latitude, show_guide = TRUE, colour=users),
data=data, alpha=.5, na.rm = T) +
scale_color_gradient(low="red", high="green")
The plot comes out to be like this
Now I'm trying to create labels for all states using geom_text.
map<-get_map(location='united states', zoom=4, maptype = "terrain",
source='google',color='color')
ggmap(map) + geom_point(
aes(x=longitude, y=latitude, show_guide = TRUE, colour=users),
data=data, alpha=.5, na.rm = T) +
scale_color_gradient(low="red", high="green") +
geom_text(aes(x = longitude, y = latitude, label = as.character(state)),
data = data,inherit.aes = FALSE)
The plot comes out to be like this.
Labels are created for each row. How to create unique label for multiple rows?
Edit: One way to do this is to remove duplicate state names from the data itself. Is there a more efficient way?
The easiest solution is to aggregate your data into a new dataframe first:
agg.data <- aggregate(cbind(longitude,latitude) ~ state, data = data, mean)
and then use the aggregated data to include the text labels:
ggmap(map) +
geom_point(data = data,
aes(x = longitude, y = latitude, show_guide = TRUE, colour=users),
alpha = 0.5, na.rm = T) +
scale_color_gradient(low = "red", high = "green") +
geom_text(data = agg.data,
aes(x = longitude, y = latitude, label = as.character(state)))
这篇关于如何在geom_text中为多行创建唯一标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!