1

I try to use ggplot2 to make a stacked bar chart. But here are some issues for the final output. The code is:

    Year <- c(rep(2010 , 2) , rep(2011 , 2) , rep(2012 , 2) , rep(2011 , 2) , rep(2014 , 2) , rep(2015 , 2) ,
                rep(2016 , 2) , rep(2017 , 2) , rep(2018 , 2) , rep(2019 , 2), rep(2020 , 2))
    
    Station <- rep(c("A" , "B") , 11)
    
    Word_count <- c(953571370424 
    ,809037391432 
    ,2075147269145 
    ,1218737285053 
    ,1603217393995 
    ,1540346123643 
    ,1371431865986 
    ,1609941622714 
    ,1572262756583 
    ,923392384249 
    ,1164562649251 
    ,776375099501 
    ,1956027215966 
    ,1304018143978 
    ,901078272828 
    ,763759731204 
    ,665935201613 
    ,998902802419 
    ,1359486135828 
    ,1359486135828 
    ,1157186353212 
    ,771457568808 
    )
    data <- data.frame(Year,Station,Word_count)
    
    print(data)
    
    p <- ggplot(data, aes(x = Year, y = Word_count))+
      geom_col(aes(fill = Station), width = 0.7)
    p

enter image description here

There is a problem with the spacing between these bar charts, the X coordinate I want to show each year, but the result is not (It skips some years and also has results like 2010.5). the stacked part of Y I want to show the absolute value of 'word_count' in the figure, but how should I show the specific word count value in the stacked bar chart?

I am learning visualization in R. I would appreciate and be grateful for any suggestions.

1
  • Is there a typo? - two lots of 2011 and none for 2013 Commented May 27, 2022 at 6:38

1 Answer 1

2

One way to solve would be to use as.factor() for the x axis and using geom_text to show the count:

ggplot(data, aes(x = factor(Year), y = Word_count, fill = Station))+
  geom_col(width = 0.9)+
  xlab("Year")+
  geom_text(aes(label = formatC(Word_count, format = "e")),
            position = position_stack(vjust = .5))

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.