Rodrigo Morales
Rodrigo Morales

Reputation: 295

Why echarts4r produces an empty plot when the data contain spaces?

The context

Let's suppose we have the following two files

File: 1.csv

labels,quantity
foo1,1
foo2,4
foo3,6

File: 2.csv

labels,quantity
foo with spaces 1,1
foo with spaces 2,4
foo with spaces 3,6

and we want to create a parallel coordinates plot for each of those two data sets.

The following code loads the data from 1.csv into df1 and plot it.

library('echarts4r')

df1 <- read.csv('1.csv')

df1 |>
  e_charts()  |>
  e_parallel(labels, quantity)

enter image description here

The following code loads the data from 2.csv into df2 and plot it.

library('echarts4r')

df2 <- read.csv('2.csv')

df2 |>
  e_charts()  |>
  e_parallel(labels, quantity)

enter image description here

The problem

As you can see above, df2 doesn't show the lines as occurs when plotting df1. I suppose this happens because of the spaces found in 2.csv, so why does this behavior occur when my data contain spaces?

Additional information

I executed both str and summary in both data sets to find the reason why this occur, but the information was the same.

summary(df1)
summary(df2)
    labels             quantity    
 Length:3           Min.   :1.000  
 Class :character   1st Qu.:2.500  
 Mode  :character   Median :4.000  
                    Mean   :3.667  
                    3rd Qu.:5.000  
                    Max.   :6.000
    labels             quantity    
 Length:3           Min.   :1.000  
 Class :character   1st Qu.:2.500  
 Mode  :character   Median :4.000  
                    Mean   :3.667  
                    3rd Qu.:5.000  
                    Max.   :6.000
str(df1)
str(df2)
'data.frame':   3 obs. of  2 variables:
 $ labels  : chr  "foo1" "foo2" "foo3"
 $ quantity: int  1 4 6
'data.frame':   3 obs. of  2 variables:
 $ labels  : chr  "foo 1" "foo 2" "foo 3"
 $ quantity: int  1 4 6

Upvotes: 1

Views: 147

Answers (1)

Quinten
Quinten

Reputation: 41235

Yes you are right, this happens because of the whitespace in the labels. You can easily remove or replace (by "_") the whitespace using gsub like this:

df2 <- read.table(text = "labels,quantity
foo with spaces 1,1
foo with spaces 2,4
foo with spaces 3,6", header = TRUE, sep = ",")

df2$labels <- gsub(" ", "_", df2$labels, fixed = TRUE)

library(dplyr)
library(echarts4r)
df2 |>
  e_charts()  |>
  e_parallel(labels, quantity)

![](https://i.imgur.com/cVuyYXa.png)

Created on 2022-07-28 by the reprex package (v2.0.1)

Upvotes: 2

Related Questions