mike shkrela
mike shkrela

Reputation: 11

How can I add a black outline to my chart?

I am trying to make a chart that is very similar to the one here I can't seem to figure out how to outline the bars and make sure the fill is in the right order.

enter image description here

This is the current code ive compiled so far and make a few comments on what I've been trying to fix

 #what is a factor in R? It is a categorical variable
library(ggplot2)
df <- read.csv("nyplots Rstudio.csv", stringsAsFactors = FALSE)

# the fill is not in the right order
# the line colors not good
# no y axis
# facets per Country
#bonus: TOOLTIPS
ggplot( df, aes(x = (Year), 
                y = Percentage, 
                col = Party,
                fill = (Ideology) )  ) +
  geom_col (position = "fill") +
  scale_fill_manual(values = c("red", "white", "grey", "light grey")) +
  theme_classic() 
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank()) 

dput of data

structure(list(Country = c("Hungary", "Hungary", "Hungary", "Hungary", 
"Hungary", "Hungary", "Hungary", "Hungary", "Hungary", "Hungary", 
"Hungary", "Hungary", "Italy", "Italy", "Italy", "Italy", "Italy", 
"Italy", "Italy", "Italy", "Italy", "Italy", "Italy", "Italy"
), Year = c(1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 
2002L, 2002L, 2002L, 2002L, 2002L, 1996L, 1996L, 1996L, 1996L, 
1996L, 1996L, 1996L, 2000L, 2000L, 2000L, 2000L, 2000L), Party = c("FIDESZ", 
"MIEP", "FKgP", "Other", "SZDSZ", "Munklaspart", "Socialist Party ", 
"FIDESZ", "MIEP", "Other", "SZDSZ", "Socialist Party ", "FIDESZ", 
"MIEP", "FKgP", "Other", "SZDSZ", "Munklaspart", "Socialist Party ", 
"FIDESZ", "MIEP", "Other", "SZDSZ", "Socialist Party "), Percentage = c(28.2, 
5.6, 13.8, 8.3, 7.9, 4.1, 32.3, 41.1, 4.4, 6.9, 5.6, 42.1, 28.2, 
5.6, 13.8, 8.3, 7.9, 4.1, 32.3, 41.1, 4.4, 6.9, 5.6, 42.1), Ideology = c("far-right", 
"far-right", "far-right", "other", "center", "center", "center", 
"far-right", "far-right", "other", "center", "center", "far-right", 
"far-right", "far-right", "other", "center", "center", "center", 
"far-right", "far-right", "other", "center", "center")), class = "data.frame", row.names = c(NA, 
-24L))

Upvotes: 1

Views: 412

Answers (1)

Sinh Nguyen
Sinh Nguyen

Reputation: 4497

Here is the code that output the graph with detail explaination

library(ggplot2)
library(dplyr)
library(forcats)
# Using colors vector with name to ensure the right color was asigned to right one
iodeology_color <- c("grey", "white", "red")
names(iodeology_color) <- c("other", "center", "far-right")
# Convert character to factors with levels -
# ggplot2 automatically order the bar by factor levels
df$Ideology <- factor(df$Ideology, levels = c("other", "center", "far-right"))
df <- df %>% arrange(Ideology, Country)
df$Party <- factor(df$Party, levels = unique(df$Party))
# using forcats::fct_relevel to order "Other before every thing else
# (the 1st one appear to be on top of the stacked bar plot)
df$Party <- fct_relevel(df$Party, "Other", after = 0L)

ggplot(df, aes(x = (Year), 
  y = Percentage, 
  # instead of using col (color) for Party - group as party
  group = Party,
  fill = (Ideology) )  ) +
  # then here color is define as black so it outline the bar with black line
  geom_col(color = "black", position = "fill", size = .5) +
  scale_fill_manual(values = iodeology_color) +
  theme(axis.title.y=element_blank(),
    axis.text.y=element_blank(),
    axis.ticks.y=element_blank(),
    # add legend background for easier to read the white fill legend.
    legend.background = element_rect(fill="lightblue", 
      size=0.5, linetype="solid"))

Output graph

enter image description here

Upvotes: 2

Related Questions