Home > Enterprise >  ggplot2: Shaded area under line with categorical data on x-axis
ggplot2: Shaded area under line with categorical data on x-axis

Time:07-13

I would like to have a shaded area under the line connecting values of categorical variables on the x-axis.

Data:

t <- structure(list(Indicator = c("Performance", "Relationship", "Inter", 
"Culture", "Focused", "Profit", "Knowledge", "Customer", "Lead", 
"Leadership", "Competitiveness"), mean = c(5.11124203821656, 
5.38707537154989, 3.12898089171975, 5.70647558386412, 5.48805732484076, 
5.3343949044586, 5.77547770700637, 6.06488853503185, 5.1156050955414, 
4.97292993630573, 4.323703366697)), row.names = c(NA, -11L), class = c("tbl_df", 
"tbl", "data.frame"))

For now, I have a plot with a geom_line & geom_plot:

t %>% 
  ggplot(aes(x = Indicator, y = mean, group = 1)) 
  scale_x_discrete(limits = c("Relationship","Inter","Culture","Focused","Profit","Knowledge","Customer","Lead","Leadership","Competitiveness","Performance"))  
  geom_point()  
  geom_line()  
  geom_vline(xintercept = 10.5)  
  ylim(c(2,7))   
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
                       panel.background = element_rect(fill = "white",
                                                       colour = "lightblue",
                                                       size = 0.5, linetype = "solid"))

Is there a way to make do it?

CodePudding user response:

If you just want to shade the whole area under the line, you can do it easily with geom_area. You only need one small additional change (using coord_cartesian so that geom_area doesn't complain that you don't have y = 0 on your plot)

library(tidyverse)

t <- structure(list(Indicator = c("Performance", "Relationship", "Inter", 
                                  "Culture", "Focused", "Profit", "Knowledge", "Customer", "Lead", 
                                  "Leadership", "Competitiveness"), mean = c(5.11124203821656, 
                                                                             5.38707537154989, 3.12898089171975, 5.70647558386412, 5.48805732484076, 
                                                                             5.3343949044586, 5.77547770700637, 6.06488853503185, 5.1156050955414, 
                                                                             4.97292993630573, 4.323703366697)), row.names = c(NA, -11L), class = c("tbl_df", 
                                                                                                                                                    "tbl", "data.frame"))

t %>%
  ggplot(aes(x = Indicator, y = mean, group = 1))  
  scale_x_discrete(limits = c("Relationship","Inter","Culture","Focused","Profit","Knowledge","Customer","Lead","Leadership","Competitiveness","Performance"))  
  geom_point()  
  geom_area()  
  geom_vline(xintercept = 10.5)  
  coord_cartesian(ylim = c(2, 7))  
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
        panel.background = element_rect(fill = "white",
                                        colour = "lightblue",
                                        size = 0.5, linetype = "solid"))

Created on 2022-07-12 by the enter image description here

  • Related