Skip to content

Commit

Permalink
add hist to density plots, listen to bruno
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyDoorn committed Feb 19, 2024
1 parent 2025090 commit 6d2095e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
37 changes: 25 additions & 12 deletions R/descriptives.R
Original file line number Diff line number Diff line change
Expand Up @@ -308,21 +308,21 @@ DescriptivesInternal <- function(jaspResults, dataset, options) {
}
}
}

# Density plots
if (options[["densityPlot"]]) {
if (is.null(jaspResults[["densityPlot"]])) {
jaspResults[["densityPlot"]] <- createJaspContainer(gettext("Density Plots"))
jaspResults[["densityPlot"]]$dependOn(c(
"densityPlot", "densityPlotSeparate",
"densityPlot", "densityPlotSeparate", "densityPlotType", "customHistogramPosition",
"colorPalette", "splitBy", "variables", "densityPlotTransparency"
))
jaspResults[["densityPlot"]]$position <- 17
}

.descriptivesDensityPlots(jaspResults[["densityPlot"]], dataset.factors, variables, options)
}

# Likert plots
if (options[["likertPlot"]] && !all(lapply(dataset.factors[variables], is.double))) {
if (is.null(jaspResults[["likertPlot"]])) {
Expand Down Expand Up @@ -2417,38 +2417,51 @@ DescriptivesInternal <- function(jaspResults, dataset, options) {

.descriptivesDensityPlotsFill <- function(container, data, plotName, axeName, position, options) {
data$split <- NULL
trans <- 1 - (options[["densityPlotTransparency"]] / 100)

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"]]
)
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
}

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_density(alpha = trans)
# 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)

p <- p + ggplot2::scale_y_continuous(name = "Density", breaks = yBreaks, limits = range(yBreaks)) +
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 <- p
}

container[[plotName]] <- densPlot
}
31 changes: 26 additions & 5 deletions inst/qml/Descriptives.qml
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ Form
{

name: "densityPlot"
label: qsTr("Density plots")
label: qsTr("Frequency plots")
columns: 2

VariablesForm
Expand All @@ -396,19 +396,41 @@ Form
AvailableVariablesList
{
name: "densityPlotVariables"
label: qsTr("")
source: [{ name: "allVariablesList", discard: ["variables", "splitBy"], use: "type=ordinal|nominal|nominalText"}]
}

AssignedVariablesList
{
name: "densityPlotSeparate"
id: densityPlotSeparate
singleVariable: true
title: qsTr("Separate densities:")
title: qsTr("Separate frequencies:")
suggestedColumns: ["ordinal", "nominal"]
}
}

RadioButtonGroup
{
name: "densityPlotType"
id: densityPlotType
title: qsTr("Type:")
RadioButton { value: "density"; label: qsTr("Density"); checked: true }
RadioButton
{
value: "histogram"
label: qsTr("Histogram")
RadioButtonGroup
{
name: "customHistogramPosition";
id: customHistogramPosition
title: qsTr("How to combine separate frequencies")
RadioButton { value: "stack"; label: qsTr("Stack"); checked: true }
RadioButton { value: "identity"; label: qsTr("Identity") }
RadioButton { value: "dodge"; label: qsTr("Dodge") }
}
}
}

DoubleField
{
name: "densityPlotTransparency"
Expand All @@ -417,8 +439,8 @@ Form
defaultValue: 20
min: 0
max: 100
enabled: densityPlotSeparate.count > 0 && ((densityPlotType.value === "density") || (densityPlotType.value === "histogram" && customHistogramPosition.value === "identity"))
}

}
CheckBox
{
Expand All @@ -433,7 +455,6 @@ Form
AvailableVariablesList
{
name: "heatmapVariables"
label: qsTr("")
source: [{ name: "allVariablesList", discard: ["variables", "splitBy"], use: "type=ordinal|nominal|nominalText"}]
}
AssignedVariablesList
Expand Down

0 comments on commit 6d2095e

Please sign in to comment.