Skip to content

Commit

Permalink
A better way to use colors and types : no argument pre-set in functio…
Browse files Browse the repository at this point in the history
…n. Simpler for the user.

TO DO : finish lcz_explore_alter vignette.
  • Loading branch information
MGousseff committed Jul 3, 2023
1 parent c5707ee commit e2672a8
Show file tree
Hide file tree
Showing 6 changed files with 4,423 additions and 75 deletions.
26 changes: 9 additions & 17 deletions R/levCol.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ levCol<-function(sf,column,drop=FALSE,...){
"This package is not suited for classification with more than 36 levels or types.
You can use the function LCZgroup2 to group some levels.") }




argNames<-names(args)
indCol<-grep(x=argNames, pattern="cols")
if (length(indCol) != 0) {
Expand Down Expand Up @@ -67,7 +64,8 @@ levCol<-function(sf,column,drop=FALSE,...){
# Case : no arguments at all in (...), or cols and other arguments are NULL
if (length(args) == 0 ||
(is.null(args)) ||
(prod(unlist(args)=="")==1)){
(prod(unlist(args)=="")==1))
{
if (length(uniqueData) > 36)
{
case<-"Too many levels"
Expand All @@ -86,8 +84,8 @@ levCol<-function(sf,column,drop=FALSE,...){
}

# Case (...) contains only one argument (color OR levels)
if(length(args) == 1) {

if(length(args) == 1)
{
if (length(indCol) == 1 && prod(argCol!="")==1 && !is.null(argCol))
{
if (length(argCol) == length(uniqueData)){
Expand All @@ -98,7 +96,7 @@ levCol<-function(sf,column,drop=FALSE,...){
names(typeLevels)<-uniqueData
} else {
case<-"2.1 : No level vector, but a color vector which size covers the number of levels in the data.
Some of the specified colors are not recognized as colors and will be replaced by colors from a standard palette."
Some or all of the specified colors are not recognized as colors and will be replaced by colors from a standard palette."
colFalse<-!areColors(argCol)
typeLevels<-argCol
typeLevels[colFalse]<-palette.colors(
Expand All @@ -116,18 +114,14 @@ levCol<-function(sf,column,drop=FALSE,...){
}
if (is.null(argCol))
{
if (prod(areColors(argLev[[1]]))==1)
{

if(prod(uniqueData%in%names(argLev[[1]]))==1 & length(uniqueData)==length(argLev[[1]]))
{
if (prod(areColors(argLev[[1]]))==1) {
if(prod(uniqueData%in%names(argLev[[1]]))==1 & length(uniqueData)==length(argLev[[1]])) {
case<-"4: A single vector was provided, whose names cover the levels in the data
and whose values are colors."
typeLevels<-argLev[[1]]
names(typeLevels)<-names(argLev[[1]])
} else
{

if(length(uniqueData)<=length(argLev[[1]]))
{
case<-case<-"5: A single vector was provided, whose values are colors
Expand All @@ -148,10 +142,8 @@ levCol<-function(sf,column,drop=FALSE,...){
}
}
}
else
{
if (prod(uniqueData%in%argLev[[1]])==1)
{
else {
if (prod(uniqueData%in%argLev[[1]])==1) {
case<-"7: No color vector but a level vector whose names cover the levels in the data"
typeLevels<-palette.colors(n=length(argLev[[1]]), palette="Polychrome 36")
names(typeLevels)<-argLev[[1]]
Expand Down
29 changes: 8 additions & 21 deletions R/showLCZ.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
#' @param column is the column that contains the LCZ.
#' @param repr indicates if the sf dataset contains standarde LCZ levels or grouped LCZ.
#' If "standard" then an optimal set of cols is used to produce the plotted map. Else, colors can be specified with the cols argument.
#' @param cols is a vector of strings specifying the colors of each levels of \'typeLevels.\'
#' If cols is an empty string, or if the number of specified color is less than the number of levels in \'typeLevels\',
#' random colors will be chosen.
#' @param LCZlevels allows you to set the grouped LCZ types.
#' The values must at least cover the values of the column in the dataset, or it will be ignored.
#' @param title allows the user to set the title of the plot
#' @param drop indicates if you want to show the levels present in no geometry.
#' @param useStandCol is set to TRUE implies that any levels detected as a standard LCZ level will receive the standard associated color
#' @param ... these dynamic dots allow you to pass arguments to specify levels expected in your dataset and colors associated to these levels
#' @param ... these dynamic dots allow you to pass arguments to specify levels expected
#' in your dataset and colors associated to these levels when not in the standard representation. You can pas your levels through a vector and you colors through another vector called cols.
#' For more details about this, read the "lcz_explore_alter" vignette.
#' @import sf ggplot2 dplyr cowplot forcats grDevices
#' @return no object is returned, but plots of the LCZ levels are produced
#' @export
Expand All @@ -26,12 +23,13 @@
#' urban=c("1","2","3","4","5","6","7","8","9"),
#' industry="10", vegetation=c("101","102","103","104"),
#' impervious="105",pervious="106",water="107")
#' # For repr="alter", you can specify colors and levels this way :
#' showLCZ(redonBDTgrouped,column="grouped",repr="alter",
#' LCZlevels=c("urban","industry","vegetation","impervious","pervious","water"),
#' cols=c("red","black","green","grey","burlywood","blue"),wf="BD TOPO")
#'
showLCZ<-function(sf, title="", wf="",column="LCZ_PRIMARY",
repr="standard", drop=FALSE, useStandCol=FALSE, cols="", LCZlevels="",...){
repr="standard", drop=FALSE, useStandCol=FALSE,...){

datasetName<-deparse(substitute(sf))

Expand Down Expand Up @@ -112,20 +110,9 @@ showLCZ<-function(sf, title="", wf="",column="LCZ_PRIMARY",
if (repr=="alter"){
print(datasetName)

typeLevels<-levCol(sf=sf,column=column, useStandCol=useStandCol, ...)$levelsColors
# print("output of levCol")
# print(typeLevels)

if(length(LCZlevels)==1 && LCZlevels[1]=="" && length(cols)==1){
typeLevels<-levCol(sf,column, drop=drop, ...)$levelsColors
} else if (length(LCZlevels)==1 & LCZlevels[1]==""){
typeLevels<-levCol(sf,column,cols=cols, drop=drop, ...)$levelsColors}
else if (length(cols)==1 & cols[1]==""){
typeLevels<-levCol(sf,column,levels=LCZlevels, drop=drop, ...)$levelsColors
}
else {typeLevels<-levCol(sf,column,levels=LCZlevels, cols=cols, drop=drop, ...)$levelsColors }

# IN CASE SOME STANDARD LEVELS ARE DETECTED, ONE MAY WANT STANDARD COLORS TO BE APPLIED
typeLevels<-levCol(sf=sf,column=column, drop=drop, ...)$levelsColors

# IN CASE SOME STANDARD LEVELS ARE DETECTED, ONE MAY WANT STANDARD COLORS TO BE APPLIED

if(useStandCol==TRUE){typeLevels<-standLevCol(levels=names(typeLevels),colors=typeLevels,useStandCol = TRUE)}

Expand Down
23 changes: 13 additions & 10 deletions inst/tinytest/test_showLCZ.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ expect_silent(showLCZ(redonBDT, drop=TRUE))

testCol <- palette.colors(n=17, palette="Polychrome 36")

showLCZ(redonBDT, title="Zones climatiques locales à Redon",repr="alter",
useStandCol=FALSE,
cols = testCol )

showLCZ(sf=redonOSM, wf="OSM", column="LCZ_PRIMARY", title="test", repr="alter", cols=testCol, useStandCol=FALSE)


showLCZ(redonBDT, title="Zones climatiques locales à Redon",repr="alter",
useStandCol=TRUE,
cols = testCol )
# showLCZ(redonBDT, title="Zones climatiques locales à Redon",repr="alter",
# useStandCol=FALSE,
# cols = testCol )
#
# showLCZ(sf=redonOSM, wf="OSM", column="LCZ_PRIMARY", title="test", repr="alter", cols=testCol, useStandCol=FALSE)
#
#
# showLCZ(redonBDT, title="Zones climatiques locales à Redon",repr="alter",
# useStandCol=TRUE,
# cols = testCol )
#levCol(sf=redonBDT, column="LCZ_PRIMARY",cols = testCol)

redonBDTgrouped<-LCZgroup2(redonBDT,column="LCZ_PRIMARY", urban=c("1","2","3","4","5","6","7","8","9"),
Expand All @@ -38,6 +38,9 @@ expect_message(
"9:"
)

# levCol(redonBDTgrouped,column="grouped",
# levels=c("urban","industry","vegetation","impervious","pervious","water"),
# cols=c("red","black","green","grey","burlywood","blue"))

expect_message(
showLCZ(redonBDTgrouped,column="grouped",repr="alter",
Expand Down
14 changes: 4 additions & 10 deletions man/showLCZ.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e2672a8

Please sign in to comment.