diff --git a/R/layers.R b/R/layers.R index 485b863..d2bec31 100644 --- a/R/layers.R +++ b/R/layers.R @@ -1017,7 +1017,7 @@ v_wordcloud <- function(vc, ) data <- get_data(vc, data) mapping <- get_mapping(vc, mapping) - mapdata <- eval_mapping(data, rename_aes_lvl(mapping)) + mapdata <- eval_mapping_(data, rename_aes_lvl(mapping)) vc$x$type <- c(vc$x$type, "wordcloud") if (is.null(name) & !is.null(mapping$word)) name <- rlang::as_label(mapping$word) diff --git a/examples/v_pie.R b/examples/v_pie.R index 7302472..d82e6fc 100644 --- a/examples/v_pie.R +++ b/examples/v_pie.R @@ -1,7 +1,7 @@ library(vchartr) -# Bvasic Pie Chart +# Basic Pie Chart subset(world_electricity, year == 2023 & type == "total") %>% vchart() %>% v_pie(aes(x = source, y = generation)) diff --git a/inst/examples/app-chart-types.R b/inst/examples/app-chart-types.R new file mode 100644 index 0000000..c6c48f0 --- /dev/null +++ b/inst/examples/app-chart-types.R @@ -0,0 +1,162 @@ + +library(shiny) +library(bslib) +library(vchartr) + + +ui <- page_fluid( + theme = bs_theme(preset = "bootstrap", bg = "#E6E7E7", fg = "#2E2E2E"), + + tags$h1("VChart types examples", class = "text-center"), + + tags$div( + class = "row row-cols-md-3 g-2 m-auto w-75 mb-5", + + lapply( + X = c( + "bar", "line", "area", "area_range", "pie", "hist", + "scatter", "smooth", "boxplot", "jitter", "heatmap", + "treemap", "circlepacking", "sankey", "wordcloud" + ), + FUN = function(type) { + tags$div( + class = "col", + tags$div( + class = "card", + vchartOutput(outputId = type, height = "250px") + ) + ) + } + ) + + ) + +) + + +server <- function(input, output, session) { + + output$bar <- renderVchart({ + electricity_mix %>% + subset(country %in% c("France", "South Korea")) %>% + vchart() %>% + v_bar(aes(country, generation, fill = source)) %>% + v_scale_color_manual(c( + "oil" = "#80549f", + "coal" = "#a68832", + "solar" = "#d66b0d", + "gas" = "#f20809", + "wind" = "#72cbb7", + "hydro" = "#2672b0", + "nuclear" = "#e4a701" + )) + }) + + output$line <- renderVchart({ + vchart(eco2mix) %>% + v_line(aes(date, solar)) %>% + v_specs_datazoom() + }) + + output$area <- renderVchart({ + vchart(eco2mix_long) %>% + v_area(aes(date, production, fill = source), stack = TRUE) %>% + v_scale_y_continuous(min = -2000) + }) + + output$area_range <- renderVchart({ + vchart(temperatures, aes(date)) %>% + v_area(aes(ymin = low, ymax = high)) + }) + + output$pie <- renderVchart({ + subset(world_electricity, year == 2023 & type == "total") %>% + vchart() %>% + v_pie(aes(x = source, y = generation)) + }) + + output$hist <- renderVchart({ + vchart(palmerpenguins::penguins) %>% + v_hist( + aes(flipper_length_mm), + bar = list( + style = list( + stroke = "white", + line_width = 1, + fill = "forestgreen" + ) + ) + ) + }) + + output$scatter <- renderVchart({ + vchart(palmerpenguins::penguins) %>% + v_scatter( + aes(x = flipper_length_mm, y = body_mass_g, color = species) + ) + }) + + output$smooth <- renderVchart({ + vchart(palmerpenguins::penguins) %>% + v_smooth( + aes(x = flipper_length_mm, y = body_mass_g, color = species) + ) + }) + + output$boxplot <- renderVchart({ + vchart(palmerpenguins::penguins) %>% + v_boxplot( + aes(x = species, y = body_mass_g) + ) + }) + + output$jitter <- renderVchart({ + vchart(palmerpenguins::penguins) %>% + v_jitter(aes(species, bill_length_mm)) + }) + + output$heatmap <- renderVchart({ + vchart(co2_emissions) %>% + v_heatmap(aes(x = year, y = country, fill = co2_per_capita)) + }) + + output$treemap <- renderVchart({ + vchart(countries_gdp) %>% + v_treemap( + aes(lvl1 = REGION_UN, lvl2 = ADMIN, value = GDP_MD), + label = list(visible = FALSE), + nonLeaf = list(visible = TRUE), + nonLeafLabel = list(visible = TRUE, position = "top") + ) + }) + + output$circlepacking <- renderVchart({ + vchart(countries_gdp) %>% + v_circlepacking( + aes(lvl1 = REGION_UN, lvl2 = SUBREGION, lvl3 = ADMIN, value = GDP_MD), + label_visible = FALSE + ) + }) + + output$sankey <- renderVchart({ + vchart(energy_sankey) %>% + v_sankey(aes(target, source, value = value)) + }) + + output$wordcloud <- renderVchart({ + vchart(top_cran_downloads) %>% + v_wordcloud( + aes(word = package, count = count, color = package), + wordCloudConfig = list( + zoomToFit = list( + shrink = TRUE, + fontSizeLimitMin = 7 + ) + ) + ) + }) + +} + + +shinyApp(ui = ui, server = server) diff --git a/vignettes/articles/charts.Rmd b/vignettes/articles/charts.Rmd new file mode 100644 index 0000000..dfe647c --- /dev/null +++ b/vignettes/articles/charts.Rmd @@ -0,0 +1,217 @@ +--- +title: "charts" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{charts} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) +``` + +```{r setup} +library(vchartr) +``` + + +## Bar Chart + +Create bar charts with `v_bar()`: + +```{r bar} +electricity_mix %>% + subset(country %in% c("France", "South Korea")) %>% + vchart() %>% + v_bar(aes(country, generation, fill = source)) %>% + v_scale_color_manual(c( + "oil" = "#80549f", + "coal" = "#a68832", + "solar" = "#d66b0d", + "gas" = "#f20809", + "wind" = "#72cbb7", + "hydro" = "#2672b0", + "nuclear" = "#e4a701" + )) +``` + + + +## Line Chart + +Create line charts with `v_line()`: + +```{r line} +vchart(eco2mix) %>% + v_line(aes(date, solar)) %>% + v_specs_datazoom() +``` + + + +## Area Chart + +Create area charts with `v_area()`: + +```{r area} +vchart(eco2mix_long) %>% + v_area(aes(date, production, fill = source), stack = TRUE) %>% + v_scale_y_continuous(min = -2000) +``` + + +Create area range charts with `v_area()` and providing `ymin` and `ymax` aesthetics: + +```{r area_range} +vchart(temperatures, aes(date)) %>% + v_area(aes(ymin = low, ymax = high)) +``` + + + +## Pie Chart + +Create pie or donut charts with `v_pie()`: + +```{r pie} +subset(world_electricity, year == 2023 & type == "total") %>% + vchart() %>% + v_pie(aes(x = source, y = generation)) +``` + + + +## Histogram Chart + +Create histograms wioth `v_hist()`: + +```{r hist} +vchart(palmerpenguins::penguins) %>% + v_hist( + aes(flipper_length_mm), + bar = list( + style = list( + stroke = "white", + line_width = 1, + fill = "forestgreen" + ) + ) + ) +``` + + + +## Scatter Chart + +Create scatter charts with `v_scatter()`: + +```{r scatter} +vchart(palmerpenguins::penguins) %>% + v_scatter( + aes(x = flipper_length_mm, y = body_mass_g, color = species) + ) +``` + + + +## Smooth Chart + +Create smooth charts with `v_smooth()`: + +```{r smooth} +vchart(palmerpenguins::penguins) %>% + v_smooth( + aes(x = flipper_length_mm, y = body_mass_g, color = species) + ) +``` + + + +## Boxplot + +Create boxplots with ``v_boxplot()`: + +```{r boxplot} +vchart(palmerpenguins::penguins) %>% + v_boxplot( + aes(x = species, y = body_mass_g) + ) +``` + + + +## Jittered points + +Create jittered points chart with `v_jitter()`: + +```{r jitter} +vchart(palmerpenguins::penguins) %>% + v_jitter(aes(species, bill_length_mm)) +``` + + + +## Heatmap + +Create heatmaps with `v_heatmap()`: + +```{r heatmap} +vchart(co2_emissions) %>% + v_heatmap(aes(x = year, y = country, fill = co2_per_capita)) +``` + + + +## Treemap + +Create treemaps with `v_treemap()`: + +```{r treemap} +vchart(countries_gdp) %>% + v_treemap( + aes(lvl1 = REGION_UN, lvl2 = ADMIN, value = GDP_MD), + label = list(visible = TRUE), + nonLeaf = list(visible = TRUE), + nonLeafLabel = list(visible = TRUE, position = "top") + ) +``` + + + +## Circlepacking + +Create circlepacking charts with `v_circlepacking()`: + +```{r circlepacking} +vchart(countries_gdp) %>% + v_circlepacking( + aes(lvl1 = REGION_UN, lvl2 = SUBREGION, lvl3 = ADMIN, value = GDP_MD) + ) +``` + + + +## Sankey Chart + +Create sankey charts with `v_sankey()`: + +```{r sankey} +vchart(energy_sankey) %>% + v_sankey(aes(target, source, value = value)) +``` + + + +## WordCloud + +Create wordclouds with `v_wordcloud()`: + +```{r wordcloud} +vchart(top_cran_downloads) %>% + v_wordcloud(aes(word = package, count = count, color = package)) +``` + diff --git a/vignettes/figures/vchart-types.png b/vignettes/figures/vchart-types.png new file mode 100644 index 0000000..8e36e71 Binary files /dev/null and b/vignettes/figures/vchart-types.png differ diff --git a/vignettes/vchartr.Rmd b/vignettes/vchartr.Rmd index 11459bc..7378afa 100644 --- a/vignettes/vchartr.Rmd +++ b/vignettes/vchartr.Rmd @@ -17,3 +17,35 @@ knitr::opts_chunk$set( ```{r setup} library(vchartr) ``` + + + +## Installation + +You can install the development version of vchartr from [GitHub](https://github.com/dreamRs/vchartr) with: + +```r +# install.packages("remotes") +remotes::install_github("dreamRs/vchartr") +``` + + +## Overview + +The following graphs (and more) can be produced: + +![charts-overview](figures/vchart-types.png) + + + +## Usage + +Start by initializing a chart with the `vchart()` function, then choose the appropriate function according to the type of chart you wish to make and specify the variables to be used as aesthetics: + +```r +vchart(eco2mix) %>% + v_line(aes(date, solar)) +``` + + +