Home > Software engineering >  Secondary axis line graph ggplot
Secondary axis line graph ggplot

Time:03-20

I managed to create the following chart in ggplot2

plolt

by using the following code:

                                         "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", 
                                         "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", 
                                         "A", "B", "C", "A", "B", "C", "A", "B", "C"), Year = c(2008, 
                                                                                                2008, 2008, 2009, 2009, 2009, 2010, 2010, 2010, 2011, 2011, 2011, 
                                                                                                2012, 2012, 2012, 2013, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 
                                                                                                2015, 2016, 2016, 2016, 2017, 2017, 2017, 2018, 2018, 2018, 2019, 
                                                                                                2019, 2019, 2020, 2020, 2020, 2021, 2021, 2021), Value = c(13, 
                                                                                                                                                           0, 0, 23, 1, 0, 24, 5, 2, 19, 13, 4, 3, 27, 10, 0, 14, 34, 0, 
                                                                                                                                                           4, 44, 0, 2, 49, 0, 1, 53, 0, 1, 55, 0, 1, 57, 0, 1, 57, 0, 1, 
                                                                                                                                                           57, 0, 1, 57), Share = c(64.3873493612449, 64.3873493612449, 
                                                                                                                                                                                    64.3873493612449, 60.3873493612449, 60.3873493612449, 60.3873493612449, 
                                                                                                                                                                                    52.1568314927171, 52.1568314927171, 52.1568314927171, 47.1706943758496, 
                                                                                                                                                                                    47.1706943758496, 47.1706943758496, 42.1444027058278, 42.1444027058278, 
                                                                                                                                                                                    42.1444027058278, 37.1718223783312, 37.1718223783312, 37.1718223783312, 
                                                                                                                                                                                    32.4750630408817, 32.4750630408817, 32.4750630408817, 28.3433373848839, 
                                                                                                                                                                                    28.3433373848839, 28.3433373848839, 24.5584081133868, 24.5584081133868, 
                                                                                                                                                                                    24.5584081133868, 21.1966663593617, 21.1966663593617, 21.1966663593617, 
                                                                                                                                                                                    18.1282210145941, 18.1282210145941, 18.1282210145941, 16.6527237518679, 
                                                                                                                                                                                    16.6527237518679, 16.6527237518679, 14.2045390727677, 14.2045390727677, 
                                                                                                                                                                                    14.2045390727677, 12.3492805575519, 12.3492805575519, 12.3492805575519
                                                                                                                                                           )), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                                                                                                                   -42L))

# Packages
library(ggplot2)
library(dplyr)
library(tidyr)
library(readxl)

# stacked area chart
p <- ggplot(data, aes(x=Year, y=Value, fill=`Category`))   
  geom_area(alpha = 0.5)

p <- p   theme(panel.background = element_rect(fill = NA),
              panel.grid.major = element_line(colour = "gray95"), 
              panel.border = element_rect(colour = "gray20", fill=NA, size=0.5))

# customized plot
p <- p geom_area(colour = NA, alpha = .5)  
  scale_fill_brewer(palette = "Spectral")  
  geom_line(position = "stack", size = .1, color = "gray25")   
  scale_x_continuous(breaks = seq(2008, 2021, by = 1),
  expand = c(0,.1))   scale_y_continuous(breaks = seq(0, 60, by = 5), expand = c(0,0))  
  labs(x = "Year", y = "Category")  
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)))  
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0)))   
  theme(legend.position='bottom')   theme(text=element_text(size=14,  family="serif"))

# add second axis and line
p <- p   scale_y_continuous(sec.axis = sec_axis(~.*1, name = 'Share (%)'))
p <- p   theme(axis.title.y.right = element_text(margin = margin(t = 0, r = 0, b = 0, l = 10)))
p <- p   geom_line(aes(y = `Share`/1, linetype = "Share", color="Share"), size=0.8, linetype = "solid") 
p

Now I would like to make two changes, which I couldn't figure out yet:

  1. Remove the term "colour" in the legend and make the gray box around the line disappear
  2. Add points only to the red line "share". I tried using geom_point but it added points to every line on the chart

Thank you for your help!

CodePudding user response:

For the fist part: Just add two lines at the end: labs(color="") (removes the term colour in the legend), theme(legend.key = element_rect(fill = "white")) (changes background around the line to white).

p <- p   labs(color="")  
  theme(legend.key = element_rect(fill = "white"))
p

For the second part: specify the aes() within geom_point to only plot points along the red line:

p   geom_point(aes(x=Year, y=Share), color = "red")

However, this gives you points in the geom_area legend too. To overwrite this:

p   guides(fill = guide_legend(override.aes = list(shape = NA)))
p

Which gives the following plot:

enter image description here

CodePudding user response:

You can use this code to first remove the term colour in the legend and second give the red line of share points:

# Packages
library(ggplot2)
library(dplyr)
library(tidyr)
library(readxl)

# stacked area chart
p <- ggplot(data, aes(x=Year, y=Value, fill=`Category`))   
  geom_area(alpha = 0.5)   
  theme(panel.background = element_rect(fill = NA),
               panel.grid.major = element_line(colour = "gray95"), 
               panel.border = element_rect(colour = "gray20", fill=NA, size=0.5))

# customized plot
p <- p geom_area(colour = NA, alpha = .5)  
  scale_fill_brewer(palette = "Spectral")  
  geom_line(position = "stack", size = .1, color = "gray25")   
  scale_x_continuous(breaks = seq(2008, 2021, by = 1),
                     expand = c(0,.1))   scale_y_continuous(breaks = seq(0, 60, by = 5), expand = c(0,0))  
  labs(x = "Year", y = "Category")  
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)))  
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0)))   
  theme(legend.position='bottom')   theme(text=element_text(size=14,  family="serif"))

# add second axis and line
p <- p   scale_y_continuous(sec.axis = sec_axis(~.*1, name = 'Share (%)'))
p <- p   theme(axis.title.y.right = element_text(margin = margin(t = 0, r = 0, b = 0, l = 10)))
p <- p   geom_line(aes(y = `Share`/1, linetype = "Share", color="Share"), size=0.8, linetype = "solid")   geom_point(aes(x=Year,y=Share))   labs(color = "")   theme(legend.key = element_rect(fill = "white")) 
p

Output:

enter image description here

  • Related