Giter Site home page Giter Site logo

points and line about apexcharter HOT 4 CLOSED

dreamrs avatar dreamrs commented on June 18, 2024
points and line

from apexcharter.

Comments (4)

meersel avatar meersel commented on June 18, 2024 1

Hi Victor,

Please find below reproducible examples:

title: "Apex markers"
author: "Miguel Eersel"
date: "2/11/2021"
output: html_document

knitr::opts_chunk$set(echo = TRUE, fig.width = 15, fig.height = 8.5)




library(tidyverse)
library(apexcharter)



x <- seq(0,2*pi, 0.4)


test_tbl <-  tribble( ~x, ~y,
                      x, sin(x)) %>%  
                   unnest() %>% 
             mutate(level = case_when(
               y > 0.8 ~ "high",
               y < -0.8 ~ "low",
               TRUE ~ "normal"
             ))

test_tbl



test_tbl %>% 
  
  ggplot(aes(x=x, y = y)) + 
  geom_point(aes(col = level), size = 6) +
  geom_line(color = "black") +
  theme_minimal(base_size = 15)

image

I want to reproduce the above ggplot using apexcharter.

  • Points are colored using the level category
  • The line needs to have the color black

1st try-out

  • line color is automatically set to red by apexcharter

test_tbl %>% 
  apexcharter::apex(mapping = aes(x=x,y=y, fill = level),type = "scatter") %>% 
  apexcharter::add_line(mapping = aes(x=x, y =y)) %>% 
  ax_stroke(width = 2)

image


2nd try-out

  • set line color to black: colors = "#000000"
    = chart is not displayed anymore

test_tbl %>% 
  apexcharter::apex(mapping = aes(x=x,y=y, fill = level),type = "scatter") %>% 
  apexcharter::add_line(mapping = aes(x=x, y =y)) %>% 
  ax_stroke(width = 2, colors = "#000000")

EMPTY NO CHART


3rd try-out

  • use ax_coolors() function
    • line and markers are all turned to black

test_tbl %>% 
  apexcharter::apex(mapping = aes(x=x,y=y, fill = level),type = "scatter") %>% 
  apexcharter::add_line(mapping = aes(x=x, y =y)) %>% 
  ax_colors("black")

image


4th try-out

  • first create a line chart
  • second add the markers to the line chart
    • Question: how to color markers according to the levels ?

test_tbl %>% 
  apexcharter::apex(mapping = aes(x=x,y=y),type = "line") %>% 
  ax_stroke(width = 2,colors = "#000000") %>% 
  ax_markers(size = 6) 


image

from apexcharter.

pvictor avatar pvictor commented on June 18, 2024

Hi,

Can you share a reproducible example please ? I'll look into it.

Victor

from apexcharter.

meersel avatar meersel commented on June 18, 2024

from apexcharter.

pvictor avatar pvictor commented on June 18, 2024

Thanks for the detailed example and the different tries.

There's a little bug in add_line causing incorrect markers size. You can obtain a correct a chart by specifying explicit markers size (three for the scatter, one for the line):

test_tbl %>% 
  apex(mapping = aes(x = x, y = y, fill = level),type = "scatter") %>% 
  add_line(mapping = aes(x = x, y = y)) %>% 
  ax_markers(size = c(6, 6, 6, 0))

image

For coloring the line, you can use ax_colors_manual like this:

test_tbl %>% 
  apex(mapping = aes(x = x, y = y, fill = level),type = "scatter") %>% 
  add_line(mapping = aes(x = x, y = y)) %>% 
  ax_markers(size = c(6, 6, 6, 0)) %>% 
  ax_colors_manual(
    list(
      normal = "#01DFD7",
      high = "#F7D358",
      low = "#FA58F4",
      y = "#848484"
    )
  )

image

You can change the name "y" using serie_name in add_line().

Or use ax_stroke, but putting the color in a list because JavaScript expect an array and in JS character and vector of length one are not the same thing.. :

test_tbl %>% 
  apex(mapping = aes(x = x, y = y, fill = level),type = "scatter") %>% 
  add_line(mapping = aes(x = x, y = y)) %>% 
  ax_markers(size = c(6, 6, 6, 0)) %>% 
  ax_stroke(width = 2, colors = list("#000000"))

image

But note that it doesn't affect the legend..

Hope it helps,

Victor

from apexcharter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.