Spaces:
Running
Running
Update app.R
Browse files
app.R
CHANGED
|
@@ -28,60 +28,61 @@ plot_factor <- function(pi_star_list,
|
|
| 28 |
)
|
| 29 |
}))
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Plot with ggplot2
|
| 32 |
-
p <- ggplot(df, aes(x =
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
geom_segment(
|
| 35 |
-
aes(
|
| 36 |
-
|
| 37 |
size = 0.3
|
| 38 |
) +
|
| 39 |
# Point at the probability
|
| 40 |
geom_point(
|
| 41 |
-
position = position_dodge(width = 0.7),
|
| 42 |
size = 2.5
|
| 43 |
) +
|
| 44 |
-
#
|
| 45 |
geom_text(
|
| 46 |
-
aes(label = sprintf("%.2f", Probability)),
|
| 47 |
-
position = position_dodge(width = 0.7),
|
| 48 |
vjust = -0.7,
|
| 49 |
size = 3
|
| 50 |
) +
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
# size = 0.3
|
| 57 |
-
# ) +
|
| 58 |
-
|
| 59 |
# Labels
|
| 60 |
labs(
|
| 61 |
title = sprintf("Optimal Distribution for *%s*",
|
| 62 |
-
gsub(factor_name,pattern = "\\.",replace=" ")),
|
| 63 |
x = "Level",
|
| 64 |
y = "Probability"
|
| 65 |
) +
|
| 66 |
-
|
| 67 |
# Apply Tufte's minimalistic theme
|
| 68 |
-
theme_minimal(base_size = 16,
|
| 69 |
-
base_line_size = 0) +
|
| 70 |
theme(
|
| 71 |
legend.position = "none",
|
| 72 |
-
legend.title
|
| 73 |
-
# Remove or soften grid lines
|
| 74 |
panel.grid.major = element_blank(),
|
| 75 |
panel.grid.minor = element_blank(),
|
| 76 |
-
# Keep a minimal axis line
|
| 77 |
axis.line = element_line(color = "black", size = 0.3),
|
| 78 |
axis.text.x = element_text(angle = 45, hjust = 1)
|
| 79 |
) +
|
| 80 |
-
|
| 81 |
# Manual color scale for different strategies
|
| 82 |
-
scale_color_manual(values = c("Democrat"
|
| 83 |
"Republican" = "red",
|
| 84 |
-
"Optimal"
|
| 85 |
|
| 86 |
return(p)
|
| 87 |
}
|
|
|
|
| 28 |
)
|
| 29 |
}))
|
| 30 |
|
| 31 |
+
# Manual dodging: Create numeric x-positions with offsets
|
| 32 |
+
df$Level_num <- as.numeric(as.factor(df$Level)) # Convert Level to numeric (1, 2, ...)
|
| 33 |
+
if (n_strategies == 1) {
|
| 34 |
+
df$x_dodged <- df$Level_num # No dodging for single strategy
|
| 35 |
+
} else {
|
| 36 |
+
# Apply ±offset for Democrat/Republican
|
| 37 |
+
df$x_dodged <- df$Level_num + ifelse(df$Strategy == "Democrat", -0.05, 0.05)
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
# Plot with ggplot2
|
| 41 |
+
p <- ggplot(df, aes(x = x_dodged,
|
| 42 |
+
y = Probability,
|
| 43 |
+
color = Strategy)) +
|
| 44 |
+
# Segment from y=0 to y=Probability
|
| 45 |
geom_segment(
|
| 46 |
+
aes(x = x_dodged, xend = x_dodged,
|
| 47 |
+
y = 0, yend = Probability),
|
| 48 |
size = 0.3
|
| 49 |
) +
|
| 50 |
# Point at the probability
|
| 51 |
geom_point(
|
|
|
|
| 52 |
size = 2.5
|
| 53 |
) +
|
| 54 |
+
# Text label above the point
|
| 55 |
geom_text(
|
| 56 |
+
aes(x = x_dodged, label = sprintf("%.2f", Probability)),
|
|
|
|
| 57 |
vjust = -0.7,
|
| 58 |
size = 3
|
| 59 |
) +
|
| 60 |
+
# Set x-axis with original Level labels
|
| 61 |
+
scale_x_continuous(
|
| 62 |
+
breaks = unique(df$Level_num),
|
| 63 |
+
labels = unique(df$Level)
|
| 64 |
+
) +
|
|
|
|
|
|
|
|
|
|
| 65 |
# Labels
|
| 66 |
labs(
|
| 67 |
title = sprintf("Optimal Distribution for *%s*",
|
| 68 |
+
gsub(factor_name, pattern = "\\.", replace = " ")),
|
| 69 |
x = "Level",
|
| 70 |
y = "Probability"
|
| 71 |
) +
|
|
|
|
| 72 |
# Apply Tufte's minimalistic theme
|
| 73 |
+
theme_minimal(base_size = 16, base_line_size = 0) +
|
|
|
|
| 74 |
theme(
|
| 75 |
legend.position = "none",
|
| 76 |
+
legend.title = element_blank(),
|
|
|
|
| 77 |
panel.grid.major = element_blank(),
|
| 78 |
panel.grid.minor = element_blank(),
|
|
|
|
| 79 |
axis.line = element_line(color = "black", size = 0.3),
|
| 80 |
axis.text.x = element_text(angle = 45, hjust = 1)
|
| 81 |
) +
|
|
|
|
| 82 |
# Manual color scale for different strategies
|
| 83 |
+
scale_color_manual(values = c("Democrat" = "#89cff0",
|
| 84 |
"Republican" = "red",
|
| 85 |
+
"Optimal" = "black"))
|
| 86 |
|
| 87 |
return(p)
|
| 88 |
}
|