Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use jaspgraphs in custom frequency plots #244

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 24 additions & 40 deletions R/descriptives.R
Original file line number Diff line number Diff line change
Expand Up @@ -2416,52 +2416,36 @@ DescriptivesInternal <- function(jaspResults, dataset, options) {
}

.descriptivesDensityPlotsFill <- function(container, data, plotName, axeName, position, options) {
data$split <- NULL

if (options[["densityPlotSeparate"]] != "") {

trans <- 1 - (options[["densityPlotTransparency"]] / 100)
p <- ggplot2::ggplot(data, ggplot2::aes(x = variable, fill = separator))
scale_fill <- jaspGraphs::scale_JASPfill_discrete(
palette = options[["colorPalette"]],
name = options[["densityPlotSeparate"]])
if (options[["densityPlotType"]] == "histogram" && (options[["customHistogramPosition"]] != "identity")) {
trans <- 1
}

} else {
p <- ggplot2::ggplot(data, ggplot2::aes(x = variable, fill = factor(1)))
scale_fill <- jaspGraphs::scale_JASPfill_discrete(palette = options[["colorPalette"]])
trans <- 1
}

jaspGraphs::graphOptions(palette = options[["colorPalette"]])

densPlot <- createJaspPlot(title = plotName, width = 480, height = 320, position = position)
if (options[["densityPlotSeparate"]] != "" && any(table(data$separator) == 1)) {
densPlot$setError(gettext("Levels within variable require at least two or more data points!"))
} else {
# p <- p + ggplot2::geom_histogram(position = "identity", alpha = trans)
if (options[["densityPlotType"]] == "density") {
p <- p + ggplot2::geom_density(alpha = trans)
thisYlab = "Density"
} else if (options[["densityPlotType"]] == "histogram") {
p <- p + ggplot2::geom_histogram(position = options[["customHistogramPosition"]], alpha = trans)
thisYlab = "Frequency"
}

# Determine range of axes to generate pretty breaks
yRange <- ggplot2::ggplot_build(p)$layout$panel_scales_y[[1]]$range$range
xRange <- ggplot2::ggplot_build(p)$layout$panel_scales_x[[1]]$range$range
yBreaks <- jaspGraphs::getPrettyAxisBreaks(yRange)
xBreaks <- jaspGraphs::getPrettyAxisBreaks(xRange)
} else if (options[["densityPlotType"]] == "density") {

p <- p + ggplot2::scale_y_continuous(name = thisYlab, breaks = yBreaks, limits = range(yBreaks)) +
ggplot2::scale_x_continuous(name = axeName, breaks = xBreaks, limits = range(xBreaks)) +
scale_fill +
jaspGraphs::geom_rangeframe() +
jaspGraphs::themeJaspRaw(legend.position = if (options[["densityPlotSeparate"]] != "") "right" else "none")
densPlot$plotObject <- jaspGraphs::jaspHistogram(x = data[["variable"]],
histogram = FALSE,
density = TRUE,
densityColor = TRUE,
densityShade = TRUE,
densityShadeAlpha = 1 - (options[["densityPlotTransparency"]]/100),
xName = axeName,
groupingVariableName = options[["densityPlotSeparate"]],
groupingVariable = data[["separator"]],
histogramPosition = options[["customHistogramPosition"]])

densPlot$plotObject <- p

} else if (options[["densityPlotType"]] == "histogram") {

densPlot$plotObject <- jaspGraphs::jaspHistogram(x = data[["variable"]],
xName = axeName,
groupingVariableName = options[["densityPlotSeparate"]],
groupingVariable = data[["separator"]],
histogramPosition = options[["customHistogramPosition"]])
if (options[["customHistogramPosition"]] == "identity")
densPlot$plotObject$layers[[1]]$aes_params$alpha <- 1 - (options[["densityPlotTransparency"]]/100)
}


container[[plotName]] <- densPlot
}
11 changes: 7 additions & 4 deletions inst/help/Descriptives.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,17 @@ Descriptives allows the user to obtain basic descriptive statistics, histograms
#### Likert plots
- Displays a horizontally stacked bar chart showing the contribution of levels within a variable in percent. Order of levels depends on defined order in the JASP data table. A legend below the graph provides an overview of levels and their respective colors in the graph.
- The y-axis represents the variables used, the x-axis represents the percentages. Percentage contribution of all lower-order (below the middle level) and higher-order (above the middle level) levels are displayed on their respective side of the graph.
- The graph displays percentages on the x-axis as positive in both directions. Reason for the chosen display (in two directions) is the graphs usefulness in survey research where levels often follow a likert based order (e.g., high - low, likely - unlikely, agreement - disagreement). Therefore, the graph contains a split between levels at their median.
- The graph displays percentages on the x-axis as positive in both directions. Reason for the chosen display (in two directions) is the graphs usefulness in survey research where levels often follow a Likert based order (e.g., high - low, likely - unlikely, agreement - disagreement). Therefore, the graph contains a split between levels at their median.
- The number of variable levels determines the number of layers displayed. Layers represent the percentage distribution of the levels of the variable under investigation.
- If the variables contain an uneven amount of levels, the middle level is displayed as a grey block in the middle of the stacked bar with its percentage contribution on top.
- Available font sizes: normal, small, medium, large.

#### Density plots
- Diplays the distribution of a numeric variable. If other nominal or ordinal variables are included, different distributions representing different values of the other variable are displayed in the same plot.
- The y-axis represents the probability density for the kernel density estimation (probability per unit on the x-axis), the x-axis represents the variables used.
#### Frequency plots
- Displays the distribution of a numeric variable by either a density or histogram plot.
- Density: The y-axis represents the probability density for the kernel density estimation (probability per unit on the x-axis).
- Histogram: The y-axis represents the frequency
- When separate frequencies are specified, the bars can be combined in various ways: Stack (stacked), Identify (behind each other), Dodge (next to each other).
- The x-axis represents the variables used.
- Appearance can be manipulated by adjusting color palette and transparency.

#### Frequency Tables (nominal and ordinal variables)
Expand Down
Loading