Madhukar Jadhav
Madhukar Jadhav

Reputation: 21

ggplot Modification Theme change geom_area,

How can I change the fill "names" ie : psavert to "personal saving rate" and "uempmed" to "median unemployment duration in weeks" also i want to add % sign in the returns on the 7 y axis, unable to use scale_x_discrete() and modify it.

can someone please help?

attached is the image and below is the code

library(ggplot2)

library(lubridate)

theme_set(theme_bw())

df <- economics[, c("date", "psavert", "uempmed")]

df <- df[lubridate::year(df$date) %in% c(1967:1981), ]

# labels and breaks for X axis text
brks <- df$date[seq(1, length(df$date), 12)]

lbls <- lubridate::year(brks)

# plot
ggplot(df, aes(x=date)) + 

     geom_area(aes(y=psavert+uempmed, fill="psavert")) + 

     geom_area(aes(y=uempmed, fill="uempmed")) + 

     labs(title="Area Chart of Returns Percentage", 
          subtitle="From Wide Data format", 
          caption="Source: Economics", 
          y="Returns %") +  # title and caption

     scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and 

labels

     scale_fill_manual(name="", 
                    values = c("psavert"="#00ba38", "uempmed"="#f8766d")) +  # 
line color

  theme(panel.grid.minor = element_blank()) + # turn off minor grid

  annotate("text", x=as.Date("1975-04-01"), y=25, label="Year with highest returns") #annotation layer

enter image description here

Upvotes: 0

Views: 490

Answers (1)

eipi10
eipi10

Reputation: 93821

The first example below is how to do it with your original plot code. The second is a more "ggplot-like" approach that first reshapes the data to "long" format and recodes the names of the values.

library(tidyverse)
library(lubridate)

theme_set(theme_bw() + theme(panel.grid.minor=element_blank()))

ggplot(df, aes(x=date)) + 
  geom_area(aes(y=psavert+uempmed, fill="Personal Saving Rate")) + 
  geom_area(aes(y=uempmed, fill="Unemployment Rate")) + 
  labs(title="Area Chart of Returns Percentage", 
       subtitle="From Wide Data format", 
       caption="Source: Economics", 
       y="Returns %") +  # title and caption
  scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels
  scale_fill_manual(name="", 
                    values = c("Personal Saving Rate"="#00ba38", "Unemployment Rate"="#f8766d")) +  # line color
  scale_y_continuous(breaks=seq(0,30,10), labels=paste0(seq(0,30,10),"%")) +
  annotate("text", x=as.Date("1975-04-01"), y=25, label="Year with highest returns")

The alternative below also uses the date_breaks and date_labels arguments to scale_x_date so there's no need to create the breaks and label vectors before creating the plot.

ggplot(df %>% gather(key, value, -date) %>% 
         mutate(key=recode(key, "psavert"="Personal Saving Rate", "uempmed"="Unemployment Rate")), 
       aes(x=date, y=value, fill=key)) + 
  geom_area() + 
  labs(title="Area Chart of Returns Percentage", 
       subtitle="From Wide Data format", 
       caption="Source: Economics", 
       y="Returns %",
       fill="") + 
  scale_x_date(date_breaks="1 year", date_labels="%Y") +  
  scale_fill_manual(name="", values = c("#00ba38", "#f8766d")) +
  scale_y_continuous(breaks=seq(0,30,10), labels=paste0(seq(0,30,10),"%")) +
  annotate("text", x=as.Date("1975-04-01"), 
           y=df %>% mutate(sum=psavert+uempmed) %>% pull(sum) %>% max + 1, 
           label="Year with highest returns")

Upvotes: 1

Related Questions