本文介绍了折线的传单颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用基本的data.frame来获得三条折线,每条折线的颜色不同

I would like to use a basic data.frame to get three polylines each of different colours

我目前正在使用以下代码作为示例.

I am currently using the below code as an example.

require(leaflet)
df <- data.frame(lat=c(rnorm(20,50),rnorm(40,0),rnorm(40,-30)),
                 lon=rnorm(100),
                 group=rep(c("a","b","c"),times=c(20,40,40)),
                 col=rep(rainbow(3,alpha=NULL),times=c(20,40,40)))
leaflet(df) %>% addTiles() %>% addPolylines(lng=~lon,lat=~lat,color=~col)

但是我得到一个连续的行,而不是每组三个单独的行.

But I get one continous line and and not the three separate lines for each group.

我可以将行分隔开,但是会做一些非常细腻的技巧:

I can separate the lines but doing an very inelegant trick of:

df_a <- rbind(df[df$group=="a",],data.frame(lat=NA,lon=NA,group="a",col=NA))
df_b <- rbind(df[df$group=="b",],data.frame(lat=NA,lon=NA,group="b",col=NA))
df_c <- rbind(df[df$group=="c",],data.frame(lat=NA,lon=NA,group="c",col=NA))
df <- rbind(df_a,df_b)
df <- rbind(df,df_c)

在使用传单功能之前,但仍不能解决颜色问题.

before the leaflet function, but it still does not solve the colour problem.

任何帮助获得三种折线不同颜色的方法将不胜感激.

Any help to get the three polylines different colours would be much appreciated.

推荐答案

以下是一种自动处理数据集中每个组的方法:

Here is a way to automate for each group in your dataset:

map <-  leaflet(df)
map <- addTiles(map)
for( group in levels(df$group)){
        map <- addPolylines(map, lng=~lon,lat=~lat,data=df[df$group==group,], color=~col)
        }
map

这篇关于折线的传单颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 13:23