Home > Mobile >  Adding line with specific df value to each bar in ggplot
Adding line with specific df value to each bar in ggplot

Time:09-30

My df is organized this way for example:

OCCURED_COUNTRY_DESC | a | b  | c | d  | flagged | type | MedDRA_PT| **E** |
__________________________________________________________________________
UNITED STATES        |403|1243|473|4077|   yes   | disp |  Seizure |144.208|
__________________________________________________________________________

My data:

 structure(list(OCCURED_COUNTRY_DESC = c("AUSTRALIA", "AUSTRIA", 
"BELGIUM", "BRAZIL", "CANADA"), a = c(4L, 7L, 20L, 5L, 11L), 
    b = c(31, 27, 100, 51, 125), c = c(872, 869, 856, 871, 865
    ), d = c(5289, 5293, 5220, 5269, 5195), w = c(876, 876, 876, 
    876, 876), x = c(5320, 5320, 5320, 5320, 5320), y = c(35L, 
    34L, 120L, 56L, 136L), z = c(6161, 6162, 6076, 6140, 6060
    ), N = c(6196, 6196, 6196, 6196, 6196), k = c("0.5", "0.5", 
    "0.5", "0.5", "0.5"), SOR = c(0.80199821407511, 1.52042360060514, 
    1.21312776329214, 0.615857869962066, 0.539569644832803), 
    log = c(-0.318329070860348, 0.604473324558599, 0.278731499148795, 
    -0.699330656240263, -0.890118907611227), LL99 = c(-0.695969674426877, 
    0.382102954188229, 0.198127619344382, -1.00534117464748, 
    -1.03425468471322), UL99 = c(-0.0544058884186467, 0.763880731966007, 
    0.337239065783058, -0.482651467660248, -0.785935460582379
    ), flagged = c("no", "no", "no", "no", "yes"), type = c(NA, 
    NA, NA, NA, "under"), MedDRA_PT = c("Seizure", "Seizure", 
    "Seizure", "Seizure", "Seizure"), E = c(5.11098506333901, 
    4.43283582089552, 16.3984674329502, 8.43063199848168, 20.8132820019249
    )), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
))

I am using ggplot2 to create a bar chart using the following piece of code:

test2 %>% #using test2 as the df
  ggplot(aes(a, OCCURED_COUNTRY_DESC, fill=type))  
  geom_bar(stat="identity") 
  scale_fill_manual(values = c("disp" = "#FF8C00",
                                "under" = "#7EC0EE",
                                "NA"="#EEE9E9")) 
  theme_classic() 
  labs(title = "Seizure",
       x = "Count",
       y = "")

What I would like to do is to add a black line in each bar correspondent to the E value, from the dataframe, for that country. However I haven't been successful. Can someone kindly guide me on how to achieve this?

enter image description here

Thanks!

CodePudding user response:

One option to achieve your desired result would be via a geom_segment, where you map your E column on both the x and the xend position. "Tricky" part are the y positions. However, as a categorical axis is still a numeric axis we could add a helper column to your data which contains the numeric positions of your categorical OCCURED_COUNTRY_DESC column. This helper column could then be mapped on the y and the yend aes needed by geom_segment where we also take into account the width of the bars:

library(ggplot2)

test2$OCCURED_COUNTRY_DESC_num <- as.numeric(factor(test2$OCCURED_COUNTRY_DESC))
width <- .9 # defautlt width of bars

ggplot(test2, aes(a, OCCURED_COUNTRY_DESC, fill = type))  
  geom_bar(stat = "identity")  
  geom_segment(aes(x = E, xend = E, 
                   y = OCCURED_COUNTRY_DESC_num - width / 2, 
                   yend = OCCURED_COUNTRY_DESC_num   width / 2), 
               color = "black", size = 1)  
  scale_fill_manual(values = c(
    "disp" = "#FF8C00",
    "under" = "#7EC0EE",
    "NA" = "#EEE9E9"
  ))  
  theme_classic()  
  labs(
    title = "Seizure",
    x = "Count",
    y = ""
  )

enter image description here

  • Related