如何解决在ggplot2
给出如下数据框dd
:
dd <- structure(list(city = c("bj","cd","cq","dg","fs","gz","hz","jh","ks","xm","sh","tj","wz","wx","xy","yw"),nums = c(3,1,2,3,1),longitude = c(116.413383697123,104.081533510425,106.558434155377,113.758420457876,113.128512195497,113.27143134446,120.215511803722,114.423558016582,119.653436190529,120.98745249795,118.096435499767,121.480538860177,117.209523214671,120.706476890356,120.318583288106,108.715422451433,120.081580993059),latitude = c(39.9109245472996,30.6558218784164,29.5689962453389,23.0273084116434,23.0277587507889,23.135336306695,30.2530829816935,23.1163588547256,29.0846393855137,31.3908634250819,24.4854066051763,31.235929042252,39.093667843404,28.0010854044722,31.4988097326857,34.3354762933686,29.3111498754101
)),row.names = c(NA,-17L),class = "data.frame")
我用以下代码绘制了地图:
library(mapdata)
china <- map("china",plot = F)
ggplot() +
geom_path(data = china,aes(long,lat,group = group),color = 'grey80',show.legend = T) +
geom_point(data = dd,aes(x = longitude,y = latitude,size = nums),alpha = 0.8,color = '#FD9FA4') +
coord_map("polyconic") +
xlab('') +
ylab('') +
theme_economist_white(gray_bg = FALSE) +
geom_text_repel(data = dd,label = city),size = 5,family = "STKaiti") +
theme_bw(base_family = "STKaiti") +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
theme(panel.grid = element_blank(),panel.border = element_blank(),panel.background = element_blank(),axis.text = element_blank(),axis.ticks = element_blank(),axis.title = element_blank(),legend.position = "right",legend.title = element_text(size = 18),legend.text = element_text(size = 18),strip.text = element_text(size = 18),plot.title = element_text(size = 25,hjust = 0.5,face = "bold",margin = margin(0,10,0)),plot.subtitle = element_text(size = 20,color="black",20,plot.caption = element_text(size = 20,color = "black")
)
legend.text
情节:
但是您可能会注意到,legend.text
不是原始数据的整数,如何将legend.text
转换为1,3
而不是1,1.5,2.5,3
?
我尝试过使用dd$nums= as.numeric(as.character(dd$nums))
,但没有成功。
谢谢。
解决方法
我敢肯定有很多方法可以解决此问题,但是这种方法非常简单,只需要将nums
参数包装在factor
中即可,即:
ggplot() +
geom_path(data = china,aes(long,lat,group = group),color = 'grey80',show.legend = T) +
geom_point(data = dd,aes(x = longitude,y = latitude,size = factor(nums)),alpha = 0.8,color = '#FD9FA4') +
guides(size = guide_legend(title = "nums"))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。